Skip to content

Commit

Permalink
feat(browser): auto-unwrap ElementFinder into WebElement for selenium…
Browse files Browse the repository at this point in the history
… funtions (#3471)
  • Loading branch information
sjelin authored and cnishina committed Aug 22, 2016
1 parent a379b33 commit c5faf08
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/browser.ts
Expand Up @@ -51,16 +51,22 @@ export class Webdriver {

/**
* Mix a function from one object onto another. The function will still be
* called in the context of the original object.
* called in the context of the original object. Any arguments of type
* `ElementFinder` will be unwrapped to their underlying `WebElement` instance
*
* @private
* @param {Object} to
* @param {Object} from
* @param {string} fnName
* @param {function=} setupFn
*/
function mixin(to: any, from: any, fnName: string, setupFn?: Function) {
function ptorMixin(to: any, from: any, fnName: string, setupFn?: Function) {
to[fnName] = function() {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] instanceof ElementFinder) {
arguments[i] = arguments[i].getWebElement();
}
}
if (setupFn) {
setupFn();
}
Expand Down Expand Up @@ -265,11 +271,11 @@ export class ProtractorBrowser extends Webdriver {
.forEach((method: string) => {
if (!this[method] && typeof webdriverInstance[method] == 'function') {
if (methodsToSync.indexOf(method) !== -1) {
mixin(
ptorMixin(
this, webdriverInstance, method,
this.waitForAngular.bind(this));
} else {
mixin(this, webdriverInstance, method);
ptorMixin(this, webdriverInstance, method);
}
}
});
Expand Down Expand Up @@ -863,7 +869,7 @@ export class ProtractorBrowser extends Webdriver {
*/
navigate() {
let nav = this.driver.navigate();
mixin(nav, this, 'refresh');
ptorMixin(nav, this, 'refresh');
return nav;
}

Expand Down
6 changes: 6 additions & 0 deletions spec/basic/lib_spec.js
Expand Up @@ -44,6 +44,12 @@ describe('protractor library', function() {
expect(browser.driver.getCurrentUrl()).toMatch('#/form');
});

it('should unwrap WebElements', function() {
browser.get('index.html');
var ptorEl = element(by.binding('greet'));
browser.executeScript('', ptorEl); // Will crash if element isn't unwrapped
});

it('should have access to the processed config block', function() {
function containsMatching(arr, string) {
var contains = false;
Expand Down

0 comments on commit c5faf08

Please sign in to comment.