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

Commit

Permalink
refactor(locators): move findElementsOverrideHelper to a member of Pr…
Browse files Browse the repository at this point in the history
…otractor

Also fix a small change introduced in the last commit where the dialog was
never closed.
  • Loading branch information
juliemr committed Nov 13, 2013
1 parent 196c83a commit 60602ad
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
73 changes: 36 additions & 37 deletions lib/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ for (foo in webdriver) {
exports[foo] = webdriver[foo];
}

/**
* @type {ProtractorBy}
*/
exports.By = new ProtractorBy();

/**
* Mix a function from one object onto another. The function will still be
* called in the context of the original object.
Expand Down Expand Up @@ -140,35 +145,6 @@ var buildMultiCssHelper = function(ptor) {
};
};

/**
* Builds a single web element from a locator with a findElementsOverride.
* Throws an error if an element is not found, and issues a warning
* if more than one element is described by the selector.
*
* @param {Protractor} ptor The protractor instance.
* @param {webdriver.WebElement} using A WebElement to scope the find,
* or null.
* @param {webdriver.Locator} locator
* @return {webdriver.WebElement}
*/
var findElementsOverrideHelper = function(ptor, using, locator) {
// We need to return a WebElement, so we construct one using a promise
// which will resolve to a WebElement.
return new webdriver.WebElement(
ptor.driver,
locator.findElementsOverride(ptor.driver, using).then(function(arr) {
if (!arr.length) {
throw new Error('No element found using locator: ' + locator.message);
}
if (arr.length > 1) {
util.puts('warning: more than one element found for locator ' +
locator.message +
'- you may need to be more specific');
}
return arr[0];
}));
};

/**
* @param {webdriver.WebDriver} webdriver
* @param {string=} opt_baseUrl A base URL to run get requests against.
Expand Down Expand Up @@ -335,7 +311,7 @@ Protractor.prototype.wrapWebElement = function(element) {

var found;
if (locator.findElementsOverride) {
found = findElementsOverrideHelper(thisPtor, element, locator);
found = thisPtor.findElementsOverrideHelper_(element, locator);
} else {
found = originalFindElement.apply(element, arguments);
}
Expand Down Expand Up @@ -425,7 +401,7 @@ Protractor.prototype.findElement = function(locator, varArgs) {
this.waitForAngular();

if (locator.findElementsOverride) {
found = findElementsOverrideHelper(this, null, locator);
found = this.findElementsOverrideHelper_(null, locator);
} else {
found = this.driver.findElement(locator, varArgs);
}
Expand Down Expand Up @@ -597,6 +573,35 @@ Protractor.prototype.debugger = function() {
});
};

/**
* Builds a single web element from a locator with a findElementsOverride.
* Throws an error if an element is not found, and issues a warning
* if more than one element is described by the selector.
*
* @private
* @param {webdriver.WebElement} using A WebElement to scope the find,
* or null.
* @param {webdriver.Locator} locator
* @return {webdriver.WebElement}
*/
Protractor.prototype.findElementsOverrideHelper_ = function(using, locator) {
// We need to return a WebElement, so we construct one using a promise
// which will resolve to a WebElement.
return new webdriver.WebElement(
this.driver,
locator.findElementsOverride(this.driver, using).then(function(arr) {
if (!arr.length) {
throw new Error('No element found using locator: ' + locator.message);
}
if (arr.length > 1) {
util.puts('warning: more than one element found for locator ' +
locator.message +
'- you may need to be more specific');
}
return arr[0];
}));
};

/**
* Create a new instance of Protractor by wrapping a webdriver instance.
*
Expand Down Expand Up @@ -628,9 +633,3 @@ exports.setInstance = function(ptor) {
exports.getInstance = function() {
return instance;
}


/**
* @type {ProtractorBy}
*/
exports.By = new ProtractorBy();
2 changes: 2 additions & 0 deletions spec/basic/actions_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ describe('using an ActionSequence', function() {
var alertDialog = browser.switchTo().alert();

expect(alertDialog.getText()).toEqual('Hello');

alertDialog.accept();
});
});
5 changes: 5 additions & 0 deletions spec/basic/lib_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ describe('protractor library', function() {
expect(protractor.ActionSequence).toBeDefined();
expect(protractor.Key.RETURN).toEqual('\uE006');
});

// it('should allow adding custom locators', function() {
// by.addLocator();

// });
});

0 comments on commit 60602ad

Please sign in to comment.