diff --git a/CHANGELOG.md b/CHANGELOG.md index def360a..8013639 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 1.1.2 (2015.07.16) + +* Employ better and broader mechanism to get label text + ### 1.1.1 (2015.07.14) * Cancel default action on Options button click diff --git a/package.json b/package.json index 6e9886c..fd1a06c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "selenium-page-object-generator", - "version": "1.1.1", + "version": "1.1.2", "description": "A nimble and flexible Selenium Page Object Model generator to improve agile testing process velocity.", "dependencies": {}, "devDependencies": { diff --git a/src/chrome/assets/js/generator.js b/src/chrome/assets/js/generator.js index ad0b04e..1056e12 100644 --- a/src/chrome/assets/js/generator.js +++ b/src/chrome/assets/js/generator.js @@ -8,6 +8,25 @@ window.POG=(function() { // ======================================================================== // private functions + function getClosestSibling(node, siblings) { + var copies = siblings.slice(0); + copies.push(node); + var closest = copies.length - 1; + var nodeIndex = [].indexOf.call(copies, node); + var siblingIndex = closest; + + for (var i = 0, j = copies.length; i < j; i++) { + var delta = Math.abs(nodeIndex - i); + + if (delta < closest) { + closest = delta; + siblingIndex = i; + } + } + + return (siblingIndex === (copies.length - 1)) ? null : copies[siblingIndex]; + } + function getComments(root) { var comments = []; var index = -1; @@ -45,7 +64,7 @@ window.POG=(function() { if (nodeName === 'INPUT') { if (node.getAttribute('type')) { - currentSelector += '[type=\'' + node.getAttribute('type') + '\']'; + currentSelector += '[type=\'' + node.type + '\']'; } else if (node.getAttribute('data-type')) { currentSelector += '[data-type=\'' + node.getAttribute('data-type') + '\']'; @@ -121,23 +140,42 @@ window.POG=(function() { var text = ''; if (node.id) { - label = document.querySelector('label[for="' + node.id + '"]'); + text = getLabelTextFor(node.id); + } - if (label) { - text = label.textContent || label.innerText || ''; - text = text.trim(); - } + if (text === '' && node.name) { + // non-standard, but it happens + text = getLabelTextFor(node.name); } if (text === '') { // find label from siblings - label = Array.filter([].slice.call(node.parentNode.children), + // TODO: should use more aggressive collector + labels = Array.filter([].slice.call(node.parentNode.children), function(item, index) { return item.nodeName === 'LABEL'; }); - if (label.length) { - text = label[0].textContent || label[0].innerText || ''; + var label = getClosestSibling(node, labels); + + if (label) { + text = label.textContent || label.innerText || ''; + text = text.trim(); + } + } + + return text; + } + + function getLabelTextFor(identifier) { + var label = null; + var text = ''; + + if (identifier) { + label = document.querySelector('label[for="' + identifier + '"]'); + + if (label) { + text = label.textContent || label.innerText || ''; text = text.trim(); } }