当我在通过Protractor的元素对象返回的webelements上使用预期条件时,我看到以下问题。我也试过$,这导致了同样的事情。我在Node 4.2.4,Chrome V47。*
上使用Protractor 3.1.1“类型错误:无法读取未定义的属性'bind'
在询问之前,我搜索了论坛,并了解使用driver.findElement将预期条件与selenium元素一起使用时存在一些已知问题。
但是,在使用元素对象本身时,我无法遇到类似的问题。
https://github.com/angular/protractor/issues/1853
我们有一个非角度的登录页面应用程序,登录后将切换到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)
答案 0 :(得分:2)
您的page object应定义为函数:
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();
答案 1 :(得分: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();
谢谢, 普拉卡什