Select -> option abstraction
Solution 1:
No such thing in Protractor, but we can write our own:
select-wrapper.js
'use strict';
var SelectWrapper = function(selector) {
this.webElement = element(selector);
};
SelectWrapper.prototype.getOptions = function() {
return this.webElement.all(by.tagName('option'));
};
SelectWrapper.prototype.getSelectedOptions = function() {
return this.webElement.all(by.css('option[selected="selected"]'));
};
SelectWrapper.prototype.selectByValue = function(value) {
return this.webElement.all(by.css('option[value="' + value + '"]')).click();
};
SelectWrapper.prototype.selectByPartialText = function(text) {
return this.webElement.all(by.cssContainingText('option', text)).click();
};
SelectWrapper.prototype.selectByText = function(text) {
return this.webElement.all(by.xpath('option[.="' + text + '"]')).click();
};
module.exports = SelectWrapper;
Usage
var SelectWrapper = require('select-wrapper');
var mySelect = new SelectWrapper(by.id('fruits'));
# select an option by value
mySelect.selectByValue('1');
# select by visible text
mySelect.selectByText('Mango');
Note that Select is a reserved word in JavaScript
Solution 2:
Starting Protractor v.0.22.0 you can just use
the new By.cssContainingText
locator:
element(by.cssContainingText('option', 'Mango'));
See the API reference.