-->

Use of Expected Conditions on Non Angular Site wit

2019-06-01 03:18发布

问题:

I am seeing following issue when I am using Expected Conditions on webelements returned through element object of Protractor. I tried with $ as well, that resulted in the same thing. I am using Protractor 3.1.1 on Node 4.2.4, Chrome V47.*

"Type Error: Cannot read property 'bind' of undefined"

Before asking, I searched the forums, and understood there are some known issues with using Expected Conditions with selenium elements using driver.findElement.

However, I could not come across a similar issues reported while using element object itself.

https://github.com/angular/protractor/issues/1853

We have a non angular app for login page, which will be switched to Angular, post login. So, I have set ignoreSynchronization=true and later planned to reset it to false after login. Below is the sample code, appreciate any thoughts from community.

Page Objects File

module.exports = {
    login: element(by.model('credentials.username')),
    password: element(by.model('credentials.password')),
    user: "Email",
    passwd: "Password",
    goButton: $('input.btn.btn-primary'),
    EC: protractor.ExpectedConditions,
    go: function() {

    browser.get("Application URL",30000);
    browser.wait(this.EC.elementToBeClickable(this.login),30000);
    },

Below is my Sample Test Suite

var VMPage = require('./LoginPage.js');


   describe('App Demo', function() {
    beforeEach(function() {
    console.log("Before Each Started");
    browser.driver.manage().timeouts().implicitlyWait(30000);
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 1800000;
    browser.ignoreSynchronization = true;
    VMPage.go();
    VMPage.login();
    });

    it('Test Case', function() {
       console.log("***Test Started***");

    });

});

Stack trace reported looks as follows: Stack:

    TypeError: Cannot read property 'bind' of undefined
        at [object Object].ExpectedConditions.presenceOf (C:\Users\PJ\Ap
pData\Roaming\npm\node_modules\protractor\lib\expectedConditions.js:289:33)
        at [object Object].ExpectedConditions.visibilityOf (C:\Users\PJ\
AppData\Roaming\npm\node_modules\protractor\lib\expectedConditions.js:328:10)
        at [object Object].ExpectedConditions.elementToBeClickable (C:\Users\PJ0
0366401\AppData\Roaming\npm\node_modules\protractor\lib\expectedConditions.js:17
8:12)
        at Object.module.exports.go (D:\protractor_git\Demo\\Log
inPage.js:14:24)
        at Object.<anonymous> (D:\protractor_git\Demo\\LoginTest
.js:9:10)
        at C:\Users\PJ\AppData\Roaming\npm\node_modules\protractor\node_
modules\jasminewd2\index.js:96:23
        at new wrappedCtr (C:\Users\PJ\AppData\Roaming\npm\node_modules\
protractor\node_modules\selenium-webdriver\lib\goog\base.js:2468:26)
        at controlFlowExecute (C:\Users\PJ\AppData\Roaming\npm\node_modu
les\protractor\node_modules\jasminewd2\index.js:82:18)
    From: Task: Run beforeEach in control flow
        at Object.<anonymous> (C:\Users\PJ\AppData\Roaming\npm\node_modu
les\protractor\node_modules\jasminewd2\index.js:81:14)
        at attemptAsync (C:\Users\PJ\AppData\Roaming\npm\node_modules\pr
otractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\jasmine
.js:1916:24)
        at QueueRunner.run (C:\Users\PJ\AppData\Roaming\npm\node_modules
\protractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\jasm
ine.js:1871:9)
        at QueueRunner.execute (C:\Users\PJ\AppData\Roaming\npm\node_mod
ules\protractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\
jasmine.js:1859:10)
        at Spec.Env.queueRunnerFactory (C:\Users\PJ\AppData\Roaming\npm\
node_modules\protractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmi
ne-core\jasmine.js:697:35)
        at Spec.execute (C:\Users\PJ\AppData\Roaming\npm\node_modules\pr
otractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\jasmine
.js:359:10)
        at Object.fn (C:\Users\PJ\AppData\Roaming\npm\node_modules\protr
actor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\jasmine.js
:2479:37)
        at attemptAsync (C:\Users\PJ\AppData\Roaming\npm\node_modules\pr
otractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\jasmine
.js:1916:24)
        at QueueRunner.run (C:\Users\PJ\AppData\Roaming\npm\node_modules
\protractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\jasm
ine.js:1871:9)
        at QueueRunner.execute (C:\Users\PJ\AppData\Roaming\npm\node_mod
ules\protractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\
jasmine.js:1859:10)
    From asynchronous test:
    Error
        at Suite.<anonymous> (D:\protractor_git\Demo\\LoginTest.
js:3:2)
        at addSpecsToSuite (C:\Users\PJ\AppData\Roaming\npm\node_modules
\protractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\jasm
ine.js:833:25)
        at Env.describe (C:\Users\PJ\AppData\Roaming\npm\node_modules\pr
otractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine-core\jasmine
.js:802:7)
        at jasmineInterface.describe (C:\Users\PJ\AppData\Roaming\npm\no
de_modules\protractor\node_modules\jasmine\node_modules\jasmine-core\lib\jasmine
-core\jasmine.js:3375:18)
        at Object.<anonymous> (D:\protractor_git\Demo\\LoginTest
.js:2:1)

回答1:

Your page object should be defined as a function:

var Page = function () {
    this.login = element(by.model('credentials.username'));
    this.password = element(by.model('credentials.password'));
    this.user = "Email";
    this.passwd = "Password";
    this.goButton = $('input.btn.btn-primary');

    this.EC = protractor.ExpectedConditions;

    this.go = function() {
        browser.get("Application URL", 30000);
        browser.wait(this.EC.elementToBeClickable(this.login), 30000);
    };
};

module.exports = new Page();


回答2:

Thanks for the suggestion, I agree it should work the way you mentioned, however, it’s strange that we still get into unrecognized types issue.

We are a bit new to the node js and this stack. I tried multiple options including the one you mentioned, by wrapping everything into a function, by individually exposing the elements/functions as module exports etc..

Finally, I found the following one to be working for me, using prototyping.

var AngularPage = function () {

  };
AngularPage.prototype.login     = element(by.model('credentials.username'));
AngularPage.prototype.password  = element(by.model('credentials.password'));
AngularPage.prototype.goButton  = $('input.btn.btn-primary');

AngularPage.prototype.user      = "username";
AngularPage.prototype.passwd    = "password";
AngularPage.prototype.EC        = protractor.ExpectedConditions;

AngularPage.prototype.go        = function(){
    browser.get("Application URL",30000)
                                    .then(browser.wait(this.EC.elementToBeClickable(this.login),30000));
                                    expect(browser.getTitle()).toContain(‘String’);
     };
AngularPage.prototype.loginMethod = function(){
    console.log("Login started");
     this.login.sendKeys(this.user);
this.password.sendKeys(this.passwd);
this.goButton.click(); 
    browser.wait(this.EC.elementToBeClickable(this.compute));
    };
module.exports = AngularPage;

In the test file, this is how, I was able to import and call it, a sample snippet.

var page = require('./LoginPage_Export_As_Prototype.js');
var LoginPage = new page();
LoginPage.go();
LoginPage.loginMethod();

Thanks, Prakash