Skip to content

Instantly share code, notes, and snippets.

@elgalu
Created September 8, 2014 22:22
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 elgalu/bd7e323225aab4b167e6 to your computer and use it in GitHub Desktop.
Save elgalu/bd7e323225aab4b167e6 to your computer and use it in GitHub Desktop.
Dollar shortcuts for less typing, i.e. $model, $xpath, $linkText
/**
* Dollar shortcuts for less typing, i.e. $model, $xpath, $linkText...
*
* @example
* var nameElm = $model('user.name');
* // Equivalent to
* var nameElm = element(by.model('user.name'));
*
* @example
* var chainedElm =
* $model('container').$binding('user.name');
* // Equivalent to
* var chainedElm =
* element(by.model('container')).element(by.binding('user.name'));
*
* @example
* var chainedLastElm =
* $model('container').$$binding('user.name').last();
* // Equivalent to
* var chainedLastElm =
* element(by.model('container')).all(by.binding('user.name')).last();
*/
var LOCATORS = ['model', 'binding', 'exactBinding', 'repeater', 'options',
'linkText', 'partialLinkText', 'xpath', 'buttonText', 'partialButtonText',
'cssContainingText', 'name', 'id', 'js', 'tagName'];
/**
* Current workaround until https://github.com/angular/protractor/issues/1102
* @type {Function}
*/
var ElementFinder = $('').constructor;
var ElementArrayFinder = $$('').constructor;
LOCATORS.forEach(function(locatorName) {
global['$' + locatorName] = function() {
return element(By[locatorName].apply(By, arguments));
};
global['$$' + locatorName] = function() {
return element.all(By[locatorName].apply(By, arguments));
};
ElementFinder.prototype['$' + locatorName] = function() {
return this.element(By[locatorName].apply(By, arguments));
};
ElementArrayFinder.prototype['$$' + locatorName] = function() {
return this.all(By[locatorName].apply(By, arguments));
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment