Skip to content

Instantly share code, notes, and snippets.

@GuyMograbi
Last active October 31, 2015 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuyMograbi/7a5f5e580bcf8d7da58a to your computer and use it in GitHub Desktop.
Save GuyMograbi/7a5f5e580bcf8d7da58a to your computer and use it in GitHub Desktop.
add a text locator to protractor
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();
});
});
// 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;
});
});
...
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