1

当我在通过量角器的元素对象返回的 web 元素上使用预期条件时,我看到了以下问题。我也试过 $ ,结果是一样的。我在节点 4.2.4、Chrome V47 上使用 Protractor 3.1.1。*

“类型错误:无法读取未定义的属性‘绑定’”

在询问之前,我搜索了论坛,并了解使用 driver.findElement 使用带有硒元素的预期条件存在一些已知问题。

但是,我在使用元素对象本身时无法遇到类似的问题。

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

我们有一个用于登录页面的非 Angular 应用程序,它将在登录后切换到 Angular。所以,我设置了ignoreSynchronization=true,后来计划在登录后将其重置为false。以下是示例代码,感谢社区的任何想法。

页面对象文件

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);
    },

下面是我的示例测试套件

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***");

    });

});

报告的堆栈跟踪如下所示:堆栈:

    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)
4

2 回答 2

2

您的页面对象应定义为一个函数:

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();
于 2016-03-23T13:20:59.017 回答
1

感谢您的建议,我同意它应该按照您提到的方式工作,但是,我们仍然遇到无法识别的类型问题,这很奇怪。

我们对节点 js 和这个堆栈有点陌生。我尝试了多种选择,包括您提到的那个,通过将所有内容包装到一个函数中,通过将元素/函数单独公开为模块导出等。

最后,我发现以下一个对我有用,使用原型。

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;

在测试文件中,这就是我能够导入并调用它的示例片段。

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

谢谢,普拉卡什

于 2016-03-24T12:52:23.347 回答