Skip to content

Commit a5a6783

Browse files
committed
Bump version to 1.1.2
- Employ better and broader mechanism to get label text - Prepend change log file
1 parent 83bbf50 commit a5a6783

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 1.1.2 (2015.07.16)
2+
3+
* Employ better and broader mechanism to get label text
4+
15
### 1.1.1 (2015.07.14)
26

37
* Cancel default action on Options button click

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "selenium-page-object-generator",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "A nimble and flexible Selenium Page Object Model generator to improve agile testing process velocity.",
55
"dependencies": {},
66
"devDependencies": {

src/chrome/assets/js/generator.js

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ window.POG=(function() {
88
// ========================================================================
99
// private functions
1010

11+
function getClosestSibling(node, siblings) {
12+
var copies = siblings.slice(0);
13+
copies.push(node);
14+
var closest = copies.length - 1;
15+
var nodeIndex = [].indexOf.call(copies, node);
16+
var siblingIndex = closest;
17+
18+
for (var i = 0, j = copies.length; i < j; i++) {
19+
var delta = Math.abs(nodeIndex - i);
20+
21+
if (delta < closest) {
22+
closest = delta;
23+
siblingIndex = i;
24+
}
25+
}
26+
27+
return (siblingIndex === (copies.length - 1)) ? null : copies[siblingIndex];
28+
}
29+
1130
function getComments(root) {
1231
var comments = [];
1332
var index = -1;
@@ -45,7 +64,7 @@ window.POG=(function() {
4564

4665
if (nodeName === 'INPUT') {
4766
if (node.getAttribute('type')) {
48-
currentSelector += '[type=\'' + node.getAttribute('type') + '\']';
67+
currentSelector += '[type=\'' + node.type + '\']';
4968
}
5069
else if (node.getAttribute('data-type')) {
5170
currentSelector += '[data-type=\'' + node.getAttribute('data-type') + '\']';
@@ -121,23 +140,42 @@ window.POG=(function() {
121140
var text = '';
122141

123142
if (node.id) {
124-
label = document.querySelector('label[for="' + node.id + '"]');
143+
text = getLabelTextFor(node.id);
144+
}
125145

126-
if (label) {
127-
text = label.textContent || label.innerText || '';
128-
text = text.trim();
129-
}
146+
if (text === '' && node.name) {
147+
// non-standard, but it happens
148+
text = getLabelTextFor(node.name);
130149
}
131150

132151
if (text === '') {
133152
// find label from siblings
134-
label = Array.filter([].slice.call(node.parentNode.children),
153+
// TODO: should use more aggressive collector
154+
labels = Array.filter([].slice.call(node.parentNode.children),
135155
function(item, index) {
136156
return item.nodeName === 'LABEL';
137157
});
138158

139-
if (label.length) {
140-
text = label[0].textContent || label[0].innerText || '';
159+
var label = getClosestSibling(node, labels);
160+
161+
if (label) {
162+
text = label.textContent || label.innerText || '';
163+
text = text.trim();
164+
}
165+
}
166+
167+
return text;
168+
}
169+
170+
function getLabelTextFor(identifier) {
171+
var label = null;
172+
var text = '';
173+
174+
if (identifier) {
175+
label = document.querySelector('label[for="' + identifier + '"]');
176+
177+
if (label) {
178+
text = label.textContent || label.innerText || '';
141179
text = text.trim();
142180
}
143181
}

0 commit comments

Comments
 (0)