Skip to content

Commit 22217ba

Browse files
committed
Minor enhancements.
- Employ more strategies to get label text - Remove redundant equal sign in Robot Framework template - Bug fixes and follow static analysis results - Add more unit tests - Bump version to 1.2.2
1 parent 3feb48e commit 22217ba

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
### 1.2.2 (2015.08.15)
2+
3+
* Employ more strategies to get label text
4+
* Remove redundant equal sign in Robot Framework template
5+
* Bug fixes and follow static analysis results
6+
* Add more unit tests
7+
18
### 1.2.1 (2015.07.30)
29

310
* Add rating link in share section

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "selenium-page-object-generator",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "A nimble and flexible Selenium Page Object Model generator to improve agile testing process velocity.",
55
"dependencies": {},
66
"devDependencies": {
@@ -18,7 +18,9 @@
1818
"gulp-minify-html": "^1.0.3",
1919
"gulp-replace": "^0.5.3",
2020
"gulp-uglify": "^1.2.0",
21-
"gulp-zip": "^3.0.2"
21+
"gulp-zip": "^3.0.2",
22+
"jquery": "^2.1.4",
23+
"jsdom": ">= 0.1.23 < 4.0.0"
2224
},
2325
"keywords": [
2426
"Selenium",

src/chrome/assets/js/generator.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,27 @@ window.POG=(function() {
2525

2626
function getClosestSibling(node, siblings) {
2727
var copies = siblings.slice(0);
28-
copies.push(node);
29-
var closest = copies.length - 1;
30-
var nodeIndex = [].indexOf.call(copies, node);
28+
copies.unshift(node);
29+
var copiesLength = copies.length;
30+
var closest = 1;
31+
32+
if (closest > copiesLength) {
33+
closest = 0;
34+
}
35+
36+
var nodeIndex = 0;
3137
var siblingIndex = closest;
3238

33-
for (var i = 0, j = copies.length; i < j; i++) {
39+
for (var i = 0; i < copiesLength; i++) {
3440
var delta = Math.abs(nodeIndex - i);
3541

36-
if (delta < closest) {
42+
if (delta > closest) {
3743
closest = delta;
3844
siblingIndex = i;
3945
}
4046
}
4147

42-
return (siblingIndex === (copies.length - 1)) ? null : copies[siblingIndex];
48+
return (siblingIndex === 0) ? null : copies[siblingIndex];
4349
}
4450

4551
function getComments(root) {
@@ -154,22 +160,18 @@ window.POG=(function() {
154160
var text = '';
155161

156162
if (node.id) {
157-
text = getLabelTextFor(node.id);
163+
text = getLabelTextFor(node, 'id');
158164
}
159165

160166
if (text === '' && node.name) {
161167
// non-standard, but it happens
162-
text = getLabelTextFor(node.name);
168+
text = getLabelTextFor(node, 'name');
163169
}
164170

165171
if (text === '') {
166172
// find label from siblings
167173
// TODO: should use more aggressive collector
168-
labels = Array.filter([].slice.call(node.parentNode.children),
169-
function(item, index) {
170-
return item.nodeName === 'LABEL';
171-
});
172-
174+
var labels = node.parentNode.querySelectorAll('label');
173175
var label = getClosestSibling(node, labels);
174176

175177
if (label) {
@@ -181,17 +183,31 @@ window.POG=(function() {
181183
return text;
182184
}
183185

184-
function getLabelTextFor(identifier) {
185-
var label = null;
186+
function getLabelTextFor(node, attribute) {
187+
var identifier = node.getAttribute(attribute) || node[attribute] || '';
186188
var text = '';
187189

188190
if (identifier) {
189-
label = document.querySelector('label[for="' + identifier + '"]');
191+
var label = document.querySelector('label[for="' + identifier + '"]');
190192

191193
if (label) {
192194
text = label.textContent || label.innerText || '';
193195
text = text.trim();
194196
}
197+
198+
if (text === '') {
199+
var identifierLowered = identifier.toLowerCase();
200+
var labels = Array.filter(document.querySelectorAll('label[for]'), function(item) {
201+
return item.getAttribute('for').toLowerCase() === identifierLowered;
202+
});
203+
204+
label = getClosestSibling(node, labels);
205+
206+
if (label) {
207+
text = label.textContent || label.innerText || '';
208+
text = text.trim();
209+
}
210+
}
195211
}
196212

197213
return text;

0 commit comments

Comments
 (0)