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

Commit c5faf08

Browse files
sjelincnishina
authored andcommitted
feat(browser): auto-unwrap ElementFinder into WebElement for selenium funtions (#3471)
1 parent a379b33 commit c5faf08

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lib/browser.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,22 @@ export class Webdriver {
5151

5252
/**
5353
* Mix a function from one object onto another. The function will still be
54-
* called in the context of the original object.
54+
* called in the context of the original object. Any arguments of type
55+
* `ElementFinder` will be unwrapped to their underlying `WebElement` instance
5556
*
5657
* @private
5758
* @param {Object} to
5859
* @param {Object} from
5960
* @param {string} fnName
6061
* @param {function=} setupFn
6162
*/
62-
function mixin(to: any, from: any, fnName: string, setupFn?: Function) {
63+
function ptorMixin(to: any, from: any, fnName: string, setupFn?: Function) {
6364
to[fnName] = function() {
65+
for (var i = 0; i < arguments.length; i++) {
66+
if (arguments[i] instanceof ElementFinder) {
67+
arguments[i] = arguments[i].getWebElement();
68+
}
69+
}
6470
if (setupFn) {
6571
setupFn();
6672
}
@@ -265,11 +271,11 @@ export class ProtractorBrowser extends Webdriver {
265271
.forEach((method: string) => {
266272
if (!this[method] && typeof webdriverInstance[method] == 'function') {
267273
if (methodsToSync.indexOf(method) !== -1) {
268-
mixin(
274+
ptorMixin(
269275
this, webdriverInstance, method,
270276
this.waitForAngular.bind(this));
271277
} else {
272-
mixin(this, webdriverInstance, method);
278+
ptorMixin(this, webdriverInstance, method);
273279
}
274280
}
275281
});
@@ -863,7 +869,7 @@ export class ProtractorBrowser extends Webdriver {
863869
*/
864870
navigate() {
865871
let nav = this.driver.navigate();
866-
mixin(nav, this, 'refresh');
872+
ptorMixin(nav, this, 'refresh');
867873
return nav;
868874
}
869875

spec/basic/lib_spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ describe('protractor library', function() {
4444
expect(browser.driver.getCurrentUrl()).toMatch('#/form');
4545
});
4646

47+
it('should unwrap WebElements', function() {
48+
browser.get('index.html');
49+
var ptorEl = element(by.binding('greet'));
50+
browser.executeScript('', ptorEl); // Will crash if element isn't unwrapped
51+
});
52+
4753
it('should have access to the processed config block', function() {
4854
function containsMatching(arr, string) {
4955
var contains = false;

0 commit comments

Comments
 (0)