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

Commit 33fa4a4

Browse files
committed
feat(locators): by model works for anything with a model, not just input
Notably, by.model will now find selects and textareas. Closes #321.
1 parent d44ef01 commit 33fa4a4

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

lib/clientsidescripts.js

+22
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ clientSideScripts.findRepeaterColumn = function() {
211211

212212
/**
213213
* Find an input elements by model name.
214+
* DEPRECATED - use findByModel
214215
*
215216
* arguments[0] {Element} The scope of the search.
216217
* arguments[1] {string} The model name.
@@ -230,6 +231,27 @@ clientSideScripts.findInputs = function() {
230231
}
231232
};
232233

234+
/**
235+
* Find a elements by model name.
236+
*
237+
* arguments[0] {Element} The scope of the search.
238+
* arguments[1] {string} The model name.
239+
*
240+
* @return {Array.<Element>} The matching elements.
241+
*/
242+
clientSideScripts.findByModel = function() {
243+
var using = arguments[0] || document;
244+
var model = arguments[1];
245+
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
246+
for (var p = 0; p < prefixes.length; ++p) {
247+
var selector = '[' + prefixes[p] + 'model="' + model + '"]';
248+
var elements = using.querySelectorAll(selector);
249+
if (elements.length) {
250+
return elements;
251+
}
252+
}
253+
};
254+
233255
/**
234256
* Find multiple select elements by model name.
235257
*

lib/locators.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ ProtractorBy.prototype.binding = function(bindingDescriptor) {
6060

6161
/**
6262
* Usage:
63+
* @DEPRECATED - use 'model' instead.
6364
* <select ng-model="user" ng-options="user.name for user in users"></select>
6465
* element(by.select("user"));
6566
*/
@@ -113,13 +114,14 @@ ProtractorBy.prototype.model = function(model) {
113114
return {
114115
findElementsOverride: function(driver, using) {
115116
return driver.findElements(
116-
webdriver.By.js(clientSideScripts.findInputs), using, model);
117+
webdriver.By.js(clientSideScripts.findByModel), using, model);
117118
},
118119
message: 'by.model("' + model + '")'
119120
};
120121
};
121122

122123
/**
124+
* @DEPRECATED - use 'model' instead.
123125
* Usage:
124126
* <textarea ng-model="user"></textarea>
125127
* element(by.textarea("user"));

spec/basic/findelements_spec.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,19 @@ describe('locators', function() {
5454
toBe(false);
5555
});
5656

57+
it('should find a textarea by model', function() {
58+
var about = element(by.model('aboutbox'));
59+
expect(about.getAttribute('value')).toEqual('This is a text box');
60+
61+
about.clear();
62+
about.sendKeys('Something else to write about');
63+
64+
expect(about.getAttribute('value')).
65+
toEqual('Something else to write about');
66+
});
67+
5768
it('should find an element by textarea model', function() {
69+
// Note: deprecated API.
5870
var about = element(by.textarea('aboutbox'));
5971
expect(about.getAttribute('value')).toEqual('This is a text box');
6072

@@ -65,6 +77,11 @@ describe('locators', function() {
6577
toEqual('Something else to write about');
6678
});
6779

80+
it('should find multiple selects by model', function() {
81+
var selects = element.all(by.model('dayColor.color'));
82+
expect(selects.count()).toEqual(3);
83+
});
84+
6885
it('should find inputs with alternate attribute forms', function() {
6986
var letterList = element(by.id('letterlist'));
7087
expect(letterList.getText()).toBe('');
@@ -91,22 +108,18 @@ describe('locators', function() {
91108

92109
describe('by select', function() {
93110
it('should find multiple selects', function() {
111+
// Note: deprecated API.
94112
browser.findElements(by.select('dayColor.color')).then(function(arr) {
95113
expect(arr.length).toEqual(3);
96114
});
97115
});
98116

99-
it('should find the select and concat its options', function() {
100-
expect(element(by.select('fruit')).getText()).
101-
toEqual('applepearpeachbanana');
102-
});
103-
104117
it('should find the selected option', function() {
105118
expect(element(by.selectedOption('fruit')).getText()).toEqual('apple');
106119
});
107120

108121
it('should find multiple selected options', function() {
109-
browser.findElements(
122+
element.all(
110123
by.selectedOption('dayColor.color')).then(function(arr) {
111124
expect(arr.length).toEqual(3);
112125
expect(arr[0].getText()).toBe('red');

0 commit comments

Comments
 (0)