Last active
October 31, 2015 20:50
-
-
Save GuyMograbi/7a5f5e580bcf8d7da58a to your computer and use it in GitHub Desktop.
add a text locator to protractor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
by.addLocator('text', | |
/** | |
* | |
* @param {string} text - will be lowercased | |
* @param {string} selector - to get list of children, css | |
* @param {object|null} parent - protractor will provide this.. | |
*/ | |
function(text, selector, parent) { | |
return _.filter($(parent || 'body').find(selector), function(e){ | |
return $(e).is(':visible') && $(e).text().toLowerCase().trim() === text.toLowerCase().trim(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the same locator but only without lodash and jquery | |
by.addLocator('text', | |
/** | |
* | |
* @param text - will be lowercased | |
* @param selector - to get list of children | |
* @param _parent - protractor will provide this.. (use _parent and not parent which is window) | |
*/ | |
function(text, selector, _parent) { | |
//return (el.offsetParent === null) | |
return Array.prototype.filter.call( (_parent || document).querySelectorAll(selector), function(e){ | |
return e && !!(e.offsetWidth || e.offsetHeight || e.getClientRects().length) && e.textContent && e.textContent.toLowerCase().trim() === text.toLowerCase().trim(); | |
//return e.offsetParent !== null; | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
return $('table').all(by.text('my text', 'tr')).first().getText().then(function(text){...}) | |
... | |
return element(by.text('my text', 'tr')).getText().then(function(text){...}) | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment