Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
feat(locators): implement by.options
Browse files Browse the repository at this point in the history
  • Loading branch information
hankduan committed Jun 23, 2014
1 parent 0dc0421 commit 7d90880
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/clientsidescripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,27 @@ functions.findByModel = function(model, using) {
}
};

/**
* Find elements by options.
*
* @param {string} optionsDescriptor The descriptor for the option
* (i.e. fruit for fruit in fruits).
* @param {Element} using The scope of the search. Defaults to 'document'.
*
* @return {Array.<Element>} The matching elements.
*/
functions.findByOptions = function(optionsDescriptor, using) {
using = using || document;
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
for (var p = 0; p < prefixes.length; ++p) {
var selector = '[' + prefixes[p] + 'options="' + optionsDescriptor + '"] option';
var elements = using.querySelectorAll(selector);
if (elements.length) {
return elements;
}
}
};

/**
* Find buttons by textual content.
*
Expand Down
30 changes: 30 additions & 0 deletions lib/locators.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,34 @@ ProtractorBy.prototype.cssContainingText = function(cssSelector, searchText) {
};
};

/**
* Find an element by ng-options expression.
*
* @alias by.options(optionsDescriptor)
* @view
* <select ng-model="color" ng-options="c for c in colors">
* <option value="0" selected="selected">red</option>
* <option value="1">green</option>
* </select>
*
* @example
* var allOptions = element.all(by.options('c for c in colors'));
* expect(allOptions.count()).toEqual(2);
* var firstOption = allOptions.first();
* expect(firstOption.getText()).toEqual('red');
*
* @param {string} optionsDescriptor ng-options expression.
*/
ProtractorBy.prototype.options = function(optionsDescriptor) {
return {
findElementsOverride: function(driver, using) {
return driver.findElements(
webdriver.By.js(clientSideScripts.findByOptions, optionsDescriptor, using));
},
toString: function toString() {
return 'by.option("' + optionsDescriptor + '")';
}
};
};

exports.ProtractorBy = ProtractorBy;
10 changes: 10 additions & 0 deletions spec/basic/locators_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,16 @@ describe('locators', function() {
});
});

describe('by options', function() {
it('should find elements by options', function() {
var allOptions = element.all(by.options('fruit for fruit in fruits'));
expect(allOptions.count()).toEqual(4);

var firstOption = allOptions.first();
expect(firstOption.getText()).toEqual('apple');
});
});

it('should determine if an element is present', function() {
expect(browser.isElementPresent(by.binding('greet'))).toBe(true);
expect(browser.isElementPresent(by.binding('nopenopenope'))).toBe(false);
Expand Down

0 comments on commit 7d90880

Please sign in to comment.