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

Commit 05eb42b

Browse files
owenmeadjuliemr
authored andcommitted
refactor(locators): moves scope in locators to last argument
scope defaults to document, and is an optional argument so now be moved to the end. Came up from debugging and trying to use window.clientSideScripts.findInputs('username'); which failed. Refactored to match original intent. BREAKING CHANGE: anything relying on clientsidescripts should no longer pass element scope as first argument. Before: window.clientSideScripts.findInputs(document, 'username'); After: window.clientSideScripts.findInputs(document, 'username'); Also, any custom locators using addLocator will now break since the arguments order has chnaged. To migrate the code follow the example below: Before: var findMenuItem = function() { var domScope = arguments[0]; var myArg = arguments[1]; // balh blah blah }; by.addLocator('menuItem', findMenuItem); After: var findMenuItem = function() { var myArg = arguments[0]; var domScope = arguments[1]; // balh blah blah }; by.addLocator('menuItem', findMenuItem); Closes #497
1 parent 8924bbc commit 05eb42b

File tree

3 files changed

+75
-76
lines changed

3 files changed

+75
-76
lines changed

lib/clientsidescripts.js

+57-57
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ clientSideScripts.waitForAngular = function() {
3030
/**
3131
* Find a list of elements in the page by their angular binding.
3232
*
33-
* arguments[0] {Element} The scope of the search.
34-
* arguments[1] {string} The binding, e.g. {{cat.name}}.
33+
* arguments[0] {string} The binding, e.g. {{cat.name}}.
34+
* arguments[1] {Element} The scope of the search.
3535
*
3636
* @return {Array.<Element>} The elements containing the binding.
3737
*/
3838
clientSideScripts.findBindings = function() {
39-
var using = arguments[0] || document;
40-
var binding = arguments[1];
39+
var binding = arguments[0];
40+
var using = arguments[1] || document;
4141
var bindings = using.getElementsByClassName('ng-binding');
4242
var matches = [];
4343
for (var i = 0; i < bindings.length; ++i) {
@@ -57,17 +57,17 @@ clientSideScripts.findBindings = function() {
5757
* Always returns an array of only one element for plain old ng-repeat.
5858
* Returns an array of all the elements in one segment for ng-repeat-start.
5959
*
60-
* arguments[0] {Element} The scope of the search.
61-
* arguments[1] {string} The text of the repeater, e.g. 'cat in cats'.
62-
* arguments[2] {number} The row index.
60+
* arguments[0] {string} The text of the repeater, e.g. 'cat in cats'.
61+
* arguments[1] {number} The row index.
62+
* arguments[2] {Element} The scope of the search.
6363
*
6464
* @return {Array.<Element>} The row of the repeater, or an array of elements
6565
* in the first row in the case of ng-repeat-start.
6666
*/
6767
clientSideScripts.findRepeaterRows = function() {
68-
var using = arguments[0] || document;
69-
var repeater = arguments[1];
70-
var index = arguments[2];
68+
var repeater = arguments[0];
69+
var index = arguments[1];
70+
var using = arguments[2] || document;
7171

7272
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
7373
var rows = [];
@@ -109,14 +109,14 @@ clientSideScripts.findBindings = function() {
109109
/**
110110
* Find all rows of an ng-repeat.
111111
*
112-
* arguments[0] {Element} The scope of the search.
113-
* arguments[1] {string} The text of the repeater, e.g. 'cat in cats'.
112+
* arguments[0] {string} The text of the repeater, e.g. 'cat in cats'.
113+
* arguments[1] {Element} The scope of the search.
114114
*
115115
* @return {Array.<Element>} All rows of the repeater.
116116
*/
117117
clientSideScripts.findAllRepeaterRows = function() {
118-
var using = arguments[0] || document;
119-
var repeater = arguments[1];
118+
var repeater = arguments[0];
119+
var using = arguments[1] || document;
120120

121121
var rows = [];
122122
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
@@ -152,19 +152,19 @@ clientSideScripts.findBindings = function() {
152152
/**
153153
* Find an element within an ng-repeat by its row and column.
154154
*
155-
* arguments[0] {Element} The scope of the search.
156-
* arguments[1] {string} The text of the repeater, e.g. 'cat in cats'.
157-
* arguments[2] {number} The row index.
158-
* arguments[3] {string} The column binding, e.g. '{{cat.name}}'.
155+
* arguments[0] {string} The text of the repeater, e.g. 'cat in cats'.
156+
* arguments[1] {number} The row index.
157+
* arguments[2] {string} The column binding, e.g. '{{cat.name}}'.
158+
* arguments[3] {Element} The scope of the search.
159159
*
160160
* @return {Array.<Element>} The element in an array.
161161
*/
162162
clientSideScripts.findRepeaterElement = function() {
163163
var matches = [];
164-
var using = arguments[0] || document;
165-
var repeater = arguments[1];
166-
var index = arguments[2];
167-
var binding = arguments[3];
164+
var repeater = arguments[0];
165+
var index = arguments[1];
166+
var binding = arguments[2];
167+
var using = arguments[3] || document;
168168

169169
var rows = [];
170170
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
@@ -239,17 +239,17 @@ clientSideScripts.findRepeaterElement = function() {
239239
/**
240240
* Find the elements in a column of an ng-repeat.
241241
*
242-
* arguments[0] {Element} The scope of the search.
243-
* arguments[1] {string} The text of the repeater, e.g. 'cat in cats'.
244-
* arguments[2] {string} The column binding, e.g. '{{cat.name}}'.
242+
* arguments[0] {string} The text of the repeater, e.g. 'cat in cats'.
243+
* arguments[1] {string} The column binding, e.g. '{{cat.name}}'.
244+
* arguments[2] {Element} The scope of the search.
245245
*
246246
* @return {Array.<Element>} The elements in the column.
247247
*/
248248
clientSideScripts.findRepeaterColumn = function() {
249249
var matches = [];
250-
var using = arguments[0] || document;
251-
var repeater = arguments[1];
252-
var binding = arguments[2];
250+
var repeater = arguments[0];
251+
var binding = arguments[1];
252+
var using = arguments[2] || document;
253253

254254
var rows = [];
255255
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
@@ -323,14 +323,14 @@ clientSideScripts.findRepeaterColumn = function() {
323323
* Find an input elements by model name.
324324
* DEPRECATED - use findByModel
325325
*
326-
* arguments[0] {Element} The scope of the search.
327-
* arguments[1] {string} The model name.
326+
* arguments[0] {string} The model name.
327+
* arguments[1] {Element} The scope of the search.
328328
*
329329
* @return {Array.<Element>} The matching input elements.
330330
*/
331331
clientSideScripts.findInputs = function() {
332-
var using = arguments[0] || document;
333-
var model = arguments[1];
332+
var model = arguments[0];
333+
var using = arguments[1] || document;
334334
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
335335
for (var p = 0; p < prefixes.length; ++p) {
336336
var selector = 'input[' + prefixes[p] + 'model="' + model + '"]';
@@ -344,14 +344,14 @@ clientSideScripts.findInputs = function() {
344344
/**
345345
* Find elements by model name.
346346
*
347-
* arguments[0] {Element} The scope of the search.
348-
* arguments[1] {string} The model name.
347+
* arguments[0] {string} The model name.
348+
* arguments[1] {Element} The scope of the search.
349349
*
350350
* @return {Array.<Element>} The matching elements.
351351
*/
352352
clientSideScripts.findByModel = function() {
353-
var using = arguments[0] || document;
354-
var model = arguments[1];
353+
var model = arguments[0];
354+
var using = arguments[1] || document;
355355
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
356356
for (var p = 0; p < prefixes.length; ++p) {
357357
var selector = '[' + prefixes[p] + 'model="' + model + '"]';
@@ -365,14 +365,14 @@ clientSideScripts.findByModel = function() {
365365
/**
366366
* Find buttons by textual content.
367367
*
368-
* arguments[0] {Element} The scope of the search.
369-
* arguments[1] {string} The exact text to match.
368+
* arguments[0] {string} The exact text to match.
369+
* arguments[1] {Element} The scope of the search.
370370
*
371371
* @return {Array.<Element>} The matching elements.
372372
*/
373373
clientSideScripts.findByButtonText = function() {
374-
var using = arguments[0] || document;
375-
var searchText = arguments[1];
374+
var searchText = arguments[0];
375+
var using = arguments[1] || document;
376376
var elements = using.querySelectorAll('button, input[type="button"], input[type="submit"]');
377377
var matches = [];
378378
for (var i = 0; i < elements.length; ++i) {
@@ -394,14 +394,14 @@ clientSideScripts.findByButtonText = function() {
394394
/**
395395
* Find buttons by textual content.
396396
*
397-
* arguments[0] {Element} The scope of the search.
398-
* arguments[1] {string} The exact text to match.
397+
* arguments[0] {string} The exact text to match.
398+
* arguments[1] {Element} The scope of the search.
399399
*
400400
* @return {Array.<Element>} The matching elements.
401401
*/
402402
clientSideScripts.findByPartialButtonText = function() {
403-
var using = arguments[0] || document;
404-
var searchText = arguments[1];
403+
var searchText = arguments[0];
404+
var using = arguments[1] || document;
405405
var elements = using.querySelectorAll('button, input[type="button"], input[type="submit"]');
406406
var matches = [];
407407
for (var i = 0; i < elements.length; ++i) {
@@ -421,17 +421,17 @@ clientSideScripts.findByPartialButtonText = function() {
421421
};
422422

423423

424-
/**
424+
/**
425425
* Find multiple select elements by model name.
426426
*
427-
* arguments[0] {Element} The scope of the search.
428-
* arguments[1] {string} The model name.
427+
* arguments[0] {string} The model name.
428+
* arguments[1] {Element} The scope of the search.
429429
*
430430
* @return {Array.<Element>} The matching select elements.
431431
*/
432432
clientSideScripts.findSelects = function() {
433-
var using = arguments[0] || document;
434-
var model = arguments[1];
433+
var model = arguments[0];
434+
var using = arguments[1] || document;
435435
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
436436
for (var p = 0; p < prefixes.length; ++p) {
437437
var selector = 'select[' + prefixes[p] + 'model="' + model + '"]';
@@ -445,14 +445,14 @@ clientSideScripts.findSelects = function() {
445445
/**
446446
* Find selected option elements by model name.
447447
*
448-
* arguments[0] {Element} The scope of the search.
449-
* arguments[1] {string} The model name.
448+
* arguments[0] {string} The model name.
449+
* arguments[1] {Element} The scope of the search.
450450
*
451451
* @return {Array.<Element>} The matching select elements.
452452
*/
453453
clientSideScripts.findSelectedOptions = function() {
454-
var using = arguments[0] || document;
455-
var model = arguments[1];
454+
var model = arguments[0];
455+
var using = arguments[1] || document;
456456
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
457457
for (var p = 0; p < prefixes.length; ++p) {
458458
var selector = 'select[' + prefixes[p] + 'model="' + model + '"] option:checked';
@@ -466,14 +466,14 @@ clientSideScripts.findSelectedOptions = function() {
466466
/**
467467
* Find textarea elements by model name.
468468
*
469-
* arguments[0] {Element} The scope of the search.
470-
* arguments[1] {String} The model name.
469+
* arguments[0] {String} The model name.
470+
* arguments[1] {Element} The scope of the search.
471471
*
472472
* @return {Array.<Element>} An array of matching textarea elements.
473473
*/
474474
clientSideScripts.findTextareas = function() {
475-
var using = arguments[0] || document;
476-
var model = arguments[1];
475+
var model = arguments[0];
476+
var using = arguments[1] || document;
477477

478478
var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\:'];
479479
for (var p = 0; p < prefixes.length; ++p) {

lib/locators.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ util.inherits(ProtractorBy, WebdriverBy);
2626
* @param {string} name
2727
* @param {function|string} script A script to be run in the context of
2828
* the browser. This script will be passed an array of arguments
29-
* that begins with the element scoping the search, and then
30-
* contains any args passed into the locator. It should return
31-
* an array of elements.
29+
* that contains any args passed into the locator followed by the
30+
* element scoping the search. It should return an array of elements.
3231
*/
3332
ProtractorBy.prototype.addLocator = function(name, script) {
3433
this[name] = function(varArgs) {
3534
return {
3635
findElementsOverride: function(driver, using) {
3736
return driver.findElements(
38-
webdriver.By.js(script), using, varArgs);
37+
webdriver.By.js(script), varArgs, using);
3938
},
4039
message: 'by.' + name + '("' + varArgs + '")'
4140
}
@@ -52,7 +51,7 @@ ProtractorBy.prototype.binding = function(bindingDescriptor) {
5251
findElementsOverride: function(driver, using) {
5352
return driver.findElements(
5453
webdriver.By.js(clientSideScripts.findBindings),
55-
using, bindingDescriptor);
54+
bindingDescriptor, using);
5655
},
5756
message: 'by.binding("' + bindingDescriptor + '")'
5857
};
@@ -68,7 +67,7 @@ ProtractorBy.prototype.select = function(model) {
6867
return {
6968
findElementsOverride: function(driver, using) {
7069
return driver.findElements(
71-
webdriver.By.js(clientSideScripts.findSelects), using, model);
70+
webdriver.By.js(clientSideScripts.findSelects), model, using);
7271
},
7372
message: 'by.select("' + model + '")'
7473
};
@@ -83,7 +82,7 @@ ProtractorBy.prototype.selectedOption = function(model) {
8382
return {
8483
findElementsOverride: function(driver, using) {
8584
return driver.findElements(
86-
webdriver.By.js(clientSideScripts.findSelectedOptions), using, model);
85+
webdriver.By.js(clientSideScripts.findSelectedOptions), model, using);
8786
},
8887
message: 'by.selectedOption("' + model + '")'
8988
};
@@ -99,7 +98,7 @@ ProtractorBy.prototype.input = function(model) {
9998
return {
10099
findElementsOverride: function(driver, using) {
101100
return driver.findElements(
102-
webdriver.By.js(clientSideScripts.findInputs), using, model);
101+
webdriver.By.js(clientSideScripts.findInputs), model, using);
103102
},
104103
message: 'by.input("' + model + '")'
105104
};
@@ -114,7 +113,7 @@ ProtractorBy.prototype.model = function(model) {
114113
return {
115114
findElementsOverride: function(driver, using) {
116115
return driver.findElements(
117-
webdriver.By.js(clientSideScripts.findByModel), using, model);
116+
webdriver.By.js(clientSideScripts.findByModel), model, using);
118117
},
119118
message: 'by.model("' + model + '")'
120119
};
@@ -129,7 +128,7 @@ ProtractorBy.prototype.buttonText = function(searchText) {
129128
return {
130129
findElementsOverride: function(driver, using) {
131130
return driver.findElements(
132-
webdriver.By.js(clientSideScripts.findByButtonText), using, searchText);
131+
webdriver.By.js(clientSideScripts.findByButtonText), searchText, using);
133132
},
134133
message: 'by.buttonText("' + searchText + '")'
135134
};
@@ -144,7 +143,7 @@ ProtractorBy.prototype.partialButtonText = function(searchText) {
144143
return {
145144
findElementsOverride: function(driver, using) {
146145
return driver.findElements(
147-
webdriver.By.js(clientSideScripts.findByPartialButtonText), using, searchText);
146+
webdriver.By.js(clientSideScripts.findByPartialButtonText), searchText, using);
148147
},
149148
message: 'by.partialButtonText("' + searchText + '")'
150149
};
@@ -161,7 +160,7 @@ ProtractorBy.prototype.textarea = function(model) {
161160
return {
162161
findElementsOverride: function(driver, using) {
163162
return driver.findElements(
164-
webdriver.By.js(clientSideScripts.findTextareas), using, model);
163+
webdriver.By.js(clientSideScripts.findTextareas), model, using);
165164
},
166165
message: 'by.textarea("' + model + '")'
167166
};
@@ -191,23 +190,23 @@ ProtractorBy.prototype.repeater = function(repeatDescriptor) {
191190
findElementsOverride: function(driver, using) {
192191
return driver.findElements(
193192
webdriver.By.js(clientSideScripts.findAllRepeaterRows),
194-
using, repeatDescriptor);
193+
repeatDescriptor, using);
195194
},
196195
message: 'by.repeater("' + repeatDescriptor + '")',
197196
row: function(index) {
198197
return {
199198
findElementsOverride: function(driver, using) {
200199
return driver.findElements(
201200
webdriver.By.js(clientSideScripts.findRepeaterRows),
202-
using, repeatDescriptor, index);
201+
repeatDescriptor, index, using);
203202
},
204203
message: 'by.repeater(' + repeatDescriptor + '").row("' + index + '")"',
205204
column: function(binding) {
206205
return {
207206
findElementsOverride: function(driver, using) {
208207
return driver.findElements(
209208
webdriver.By.js(clientSideScripts.findRepeaterElement),
210-
using, repeatDescriptor, index, binding);
209+
repeatDescriptor, index, binding, using);
211210
},
212211
message: 'by.repeater("' + repeatDescriptor + '").row("' + index +
213212
'").column("' + binding + '")'
@@ -220,7 +219,7 @@ ProtractorBy.prototype.repeater = function(repeatDescriptor) {
220219
findElementsOverride: function(driver, using) {
221220
return driver.findElements(
222221
webdriver.By.js(clientSideScripts.findRepeaterColumn),
223-
using, repeatDescriptor, binding);
222+
repeatDescriptor, binding, using);
224223
},
225224
message: 'by.repeater("' + repeatDescriptor + '").column("' + binding +
226225
'")',
@@ -229,7 +228,7 @@ ProtractorBy.prototype.repeater = function(repeatDescriptor) {
229228
findElementsOverride: function(driver, using) {
230229
return driver.findElements(
231230
webdriver.By.js(clientSideScripts.findRepeaterElement),
232-
using, repeatDescriptor, index, binding);
231+
repeatDescriptor, index, binding, using);
233232
},
234233
message: 'by.repeater("' + repeatDescriptor + '").column("' +
235234
binding + '").row("' + index + '")'

spec/basic/lib_spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ describe('protractor library', function() {
5050

5151
it('should allow adding custom locators', function() {
5252
var findMenuItem = function() {
53-
var using = arguments[0]; // unused
54-
var itemName = arguments[1];
53+
var itemName = arguments[0];
54+
var using = arguments[1]; // unused
5555
var menu = document.querySelectorAll('.menu li');
5656
for (var i = 0; i < menu.length; ++i) {
5757
if (menu[i].textContent == itemName) {

0 commit comments

Comments
 (0)