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

Commit 64bee25

Browse files
bcaudanjuliemr
authored andcommitted
fix(locators): add locator with multiple arguments
When using a custom locator with multiple arguments, only the first argument was used when calling `webdriver.findElements`.
1 parent bfedc6a commit 64bee25

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

lib/locators.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,29 @@ util.inherits(ProtractorBy, WebdriverBy);
3030
* element scoping the search. It should return an array of elements.
3131
*/
3232
ProtractorBy.prototype.addLocator = function(name, script) {
33-
this[name] = function(varArgs) {
33+
this[name] = function() {
34+
var toArray = function(arguments) {
35+
var locatorArguments = [];
36+
for (var i = 0; i < arguments.length; i++) {
37+
locatorArguments.push(arguments[i]);
38+
}
39+
return locatorArguments;
40+
}
41+
var buildFindElementsArguments = function(
42+
locator, locatorArguments, scope) {
43+
var findArguments = locatorArguments.slice(0);
44+
findArguments.unshift(locator);
45+
findArguments.push(scope);
46+
return findArguments;
47+
}
48+
49+
var locatorArguments = toArray(arguments);
3450
return {
3551
findElementsOverride: function(driver, using) {
36-
return driver.findElements(
37-
webdriver.By.js(script), varArgs, using);
52+
return driver.findElements.apply(driver, buildFindElementsArguments(
53+
webdriver.By.js(script), locatorArguments, using));
3854
},
39-
message: 'by.' + name + '("' + varArgs + '")'
55+
message: 'by.' + name + '("' + locatorArguments + '")'
4056
};
4157
};
4258
};

spec/basic/lib_spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ describe('protractor library', function() {
6969
expect(element(by.menuItem('repeater')).getText()).toEqual('repeater');
7070
});
7171

72+
it('should allow adding custom varargs locators', function() {
73+
var findMenuItemWithName = function() {
74+
var css = arguments[0];
75+
var itemName = arguments[1];
76+
var using = arguments[2]; // unused
77+
var menu = document.querySelectorAll(css);
78+
for (var i = 0; i < menu.length; ++i) {
79+
if (menu[i].textContent == itemName) {
80+
return [menu[i]];
81+
}
82+
}
83+
};
84+
85+
by.addLocator('menuItemWithName', findMenuItemWithName);
86+
87+
expect(by.menuItemWithName).toBeDefined();
88+
89+
browser.get('index.html');
90+
expect(element(by.menuItemWithName('.menu li', 'repeater')).isPresent());
91+
expect(element(by.menuItemWithName('.menu li', 'repeater')).getText()).toEqual('repeater');
92+
});
93+
7294
describe('helper functions', function() {
7395
it('should get the absolute URL', function() {
7496
browser.get('index.html');

0 commit comments

Comments
 (0)