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

Commit

Permalink
fix(locators): add locator with multiple arguments
Browse files Browse the repository at this point in the history
When using a custom locator with multiple arguments, only the first argument was used when
calling `webdriver.findElements`.
  • Loading branch information
bcaudan authored and juliemr committed Feb 27, 2014
1 parent bfedc6a commit 64bee25
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/locators.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,29 @@ util.inherits(ProtractorBy, WebdriverBy);
* element scoping the search. It should return an array of elements.
*/
ProtractorBy.prototype.addLocator = function(name, script) {
this[name] = function(varArgs) {
this[name] = function() {
var toArray = function(arguments) {
var locatorArguments = [];
for (var i = 0; i < arguments.length; i++) {
locatorArguments.push(arguments[i]);
}
return locatorArguments;
}
var buildFindElementsArguments = function(
locator, locatorArguments, scope) {
var findArguments = locatorArguments.slice(0);
findArguments.unshift(locator);
findArguments.push(scope);
return findArguments;
}

var locatorArguments = toArray(arguments);
return {
findElementsOverride: function(driver, using) {
return driver.findElements(
webdriver.By.js(script), varArgs, using);
return driver.findElements.apply(driver, buildFindElementsArguments(
webdriver.By.js(script), locatorArguments, using));
},
message: 'by.' + name + '("' + varArgs + '")'
message: 'by.' + name + '("' + locatorArguments + '")'
};
};
};
Expand Down
22 changes: 22 additions & 0 deletions spec/basic/lib_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ describe('protractor library', function() {
expect(element(by.menuItem('repeater')).getText()).toEqual('repeater');
});

it('should allow adding custom varargs locators', function() {
var findMenuItemWithName = function() {
var css = arguments[0];
var itemName = arguments[1];
var using = arguments[2]; // unused
var menu = document.querySelectorAll(css);
for (var i = 0; i < menu.length; ++i) {
if (menu[i].textContent == itemName) {
return [menu[i]];
}
}
};

by.addLocator('menuItemWithName', findMenuItemWithName);

expect(by.menuItemWithName).toBeDefined();

browser.get('index.html');
expect(element(by.menuItemWithName('.menu li', 'repeater')).isPresent());
expect(element(by.menuItemWithName('.menu li', 'repeater')).getText()).toEqual('repeater');
});

describe('helper functions', function() {
it('should get the absolute URL', function() {
browser.get('index.html');
Expand Down

0 comments on commit 64bee25

Please sign in to comment.