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

Commit 7d90880

Browse files
committed
feat(locators): implement by.options
1 parent 0dc0421 commit 7d90880

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

lib/clientsidescripts.js

+21
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,27 @@ functions.findByModel = function(model, using) {
348348
}
349349
};
350350

351+
/**
352+
* Find elements by options.
353+
*
354+
* @param {string} optionsDescriptor The descriptor for the option
355+
* (i.e. fruit for fruit in fruits).
356+
* @param {Element} using The scope of the search. Defaults to 'document'.
357+
*
358+
* @return {Array.<Element>} The matching elements.
359+
*/
360+
functions.findByOptions = function(optionsDescriptor, using) {
361+
using = using || document;
362+
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
363+
for (var p = 0; p < prefixes.length; ++p) {
364+
var selector = '[' + prefixes[p] + 'options="' + optionsDescriptor + '"] option';
365+
var elements = using.querySelectorAll(selector);
366+
if (elements.length) {
367+
return elements;
368+
}
369+
}
370+
};
371+
351372
/**
352373
* Find buttons by textual content.
353374
*

lib/locators.js

+30
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,34 @@ ProtractorBy.prototype.cssContainingText = function(cssSelector, searchText) {
355355
};
356356
};
357357

358+
/**
359+
* Find an element by ng-options expression.
360+
*
361+
* @alias by.options(optionsDescriptor)
362+
* @view
363+
* <select ng-model="color" ng-options="c for c in colors">
364+
* <option value="0" selected="selected">red</option>
365+
* <option value="1">green</option>
366+
* </select>
367+
*
368+
* @example
369+
* var allOptions = element.all(by.options('c for c in colors'));
370+
* expect(allOptions.count()).toEqual(2);
371+
* var firstOption = allOptions.first();
372+
* expect(firstOption.getText()).toEqual('red');
373+
*
374+
* @param {string} optionsDescriptor ng-options expression.
375+
*/
376+
ProtractorBy.prototype.options = function(optionsDescriptor) {
377+
return {
378+
findElementsOverride: function(driver, using) {
379+
return driver.findElements(
380+
webdriver.By.js(clientSideScripts.findByOptions, optionsDescriptor, using));
381+
},
382+
toString: function toString() {
383+
return 'by.option("' + optionsDescriptor + '")';
384+
}
385+
};
386+
};
387+
358388
exports.ProtractorBy = ProtractorBy;

spec/basic/locators_spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,16 @@ describe('locators', function() {
355355
});
356356
});
357357

358+
describe('by options', function() {
359+
it('should find elements by options', function() {
360+
var allOptions = element.all(by.options('fruit for fruit in fruits'));
361+
expect(allOptions.count()).toEqual(4);
362+
363+
var firstOption = allOptions.first();
364+
expect(firstOption.getText()).toEqual('apple');
365+
});
366+
});
367+
358368
it('should determine if an element is present', function() {
359369
expect(browser.isElementPresent(by.binding('greet'))).toBe(true);
360370
expect(browser.isElementPresent(by.binding('nopenopenope'))).toBe(false);

0 commit comments

Comments
 (0)