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

Commit 60602ad

Browse files
committed
refactor(locators): move findElementsOverrideHelper to a member of Protractor
Also fix a small change introduced in the last commit where the dialog was never closed.
1 parent 196c83a commit 60602ad

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

Diff for: lib/protractor.js

+36-37
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ for (foo in webdriver) {
1919
exports[foo] = webdriver[foo];
2020
}
2121

22+
/**
23+
* @type {ProtractorBy}
24+
*/
25+
exports.By = new ProtractorBy();
26+
2227
/**
2328
* Mix a function from one object onto another. The function will still be
2429
* called in the context of the original object.
@@ -140,35 +145,6 @@ var buildMultiCssHelper = function(ptor) {
140145
};
141146
};
142147

143-
/**
144-
* Builds a single web element from a locator with a findElementsOverride.
145-
* Throws an error if an element is not found, and issues a warning
146-
* if more than one element is described by the selector.
147-
*
148-
* @param {Protractor} ptor The protractor instance.
149-
* @param {webdriver.WebElement} using A WebElement to scope the find,
150-
* or null.
151-
* @param {webdriver.Locator} locator
152-
* @return {webdriver.WebElement}
153-
*/
154-
var findElementsOverrideHelper = function(ptor, using, locator) {
155-
// We need to return a WebElement, so we construct one using a promise
156-
// which will resolve to a WebElement.
157-
return new webdriver.WebElement(
158-
ptor.driver,
159-
locator.findElementsOverride(ptor.driver, using).then(function(arr) {
160-
if (!arr.length) {
161-
throw new Error('No element found using locator: ' + locator.message);
162-
}
163-
if (arr.length > 1) {
164-
util.puts('warning: more than one element found for locator ' +
165-
locator.message +
166-
'- you may need to be more specific');
167-
}
168-
return arr[0];
169-
}));
170-
};
171-
172148
/**
173149
* @param {webdriver.WebDriver} webdriver
174150
* @param {string=} opt_baseUrl A base URL to run get requests against.
@@ -335,7 +311,7 @@ Protractor.prototype.wrapWebElement = function(element) {
335311

336312
var found;
337313
if (locator.findElementsOverride) {
338-
found = findElementsOverrideHelper(thisPtor, element, locator);
314+
found = thisPtor.findElementsOverrideHelper_(element, locator);
339315
} else {
340316
found = originalFindElement.apply(element, arguments);
341317
}
@@ -425,7 +401,7 @@ Protractor.prototype.findElement = function(locator, varArgs) {
425401
this.waitForAngular();
426402

427403
if (locator.findElementsOverride) {
428-
found = findElementsOverrideHelper(this, null, locator);
404+
found = this.findElementsOverrideHelper_(null, locator);
429405
} else {
430406
found = this.driver.findElement(locator, varArgs);
431407
}
@@ -597,6 +573,35 @@ Protractor.prototype.debugger = function() {
597573
});
598574
};
599575

576+
/**
577+
* Builds a single web element from a locator with a findElementsOverride.
578+
* Throws an error if an element is not found, and issues a warning
579+
* if more than one element is described by the selector.
580+
*
581+
* @private
582+
* @param {webdriver.WebElement} using A WebElement to scope the find,
583+
* or null.
584+
* @param {webdriver.Locator} locator
585+
* @return {webdriver.WebElement}
586+
*/
587+
Protractor.prototype.findElementsOverrideHelper_ = function(using, locator) {
588+
// We need to return a WebElement, so we construct one using a promise
589+
// which will resolve to a WebElement.
590+
return new webdriver.WebElement(
591+
this.driver,
592+
locator.findElementsOverride(this.driver, using).then(function(arr) {
593+
if (!arr.length) {
594+
throw new Error('No element found using locator: ' + locator.message);
595+
}
596+
if (arr.length > 1) {
597+
util.puts('warning: more than one element found for locator ' +
598+
locator.message +
599+
'- you may need to be more specific');
600+
}
601+
return arr[0];
602+
}));
603+
};
604+
600605
/**
601606
* Create a new instance of Protractor by wrapping a webdriver instance.
602607
*
@@ -628,9 +633,3 @@ exports.setInstance = function(ptor) {
628633
exports.getInstance = function() {
629634
return instance;
630635
}
631-
632-
633-
/**
634-
* @type {ProtractorBy}
635-
*/
636-
exports.By = new ProtractorBy();

Diff for: spec/basic/actions_spec.js

+2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ describe('using an ActionSequence', function() {
2121
var alertDialog = browser.switchTo().alert();
2222

2323
expect(alertDialog.getText()).toEqual('Hello');
24+
25+
alertDialog.accept();
2426
});
2527
});

Diff for: spec/basic/lib_spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ describe('protractor library', function() {
4646
expect(protractor.ActionSequence).toBeDefined();
4747
expect(protractor.Key.RETURN).toEqual('\uE006');
4848
});
49+
50+
// it('should allow adding custom locators', function() {
51+
// by.addLocator();
52+
53+
// });
4954
});

0 commit comments

Comments
 (0)