I have some tests written using protractor for angular.js app. I am using Page Objects
design pattern and there i have some methods that navigate to other pages by clicking on links and buttons. and soon after that i am calling browser.waitForAngular()
.
我有一些使用protractor for angular.js应用程序编写的测试。我正在使用页面对象设计模式,我有一些方法可以通过单击链接和按钮导航到其他页面。不久之后,我正在调用browser.waitForAngular()。
Page Object
页面对象
module.exports = function () {
this.companyNameLink = element(by.id('viewCompany'));
this.newMeetingButton = element(by.id('newMeetingButton'));
this.createNewGeneralMeeting = function () {
this.newMeetingButton.click();
browser.waitForAngular();
};
this.goToCompanyPage = function () {
this.companyNameLink.click();
browser.waitForAngular();
};
};
And in some spec file i use this page object like this..
在一些spec文件中,我使用这样的页面对象..
var DashboardPage = require('../dashboardPageObject.js');
dashboardPage = new DashboardPage();
...
dashboardPage.goToCompanyPage();
But the problem is sometimes i get the angular could not be found on the window
error and my tests fail. Most of the time the tests run. This issue is random. My question is that should i remove the browser.waitForAngular()
from the page object method and call it after i make the method call like this...
但问题是有时我得到的窗口错误无法找到角度,我的测试失败。大多数时候测试运行。这个问题是随机的。我的问题是,我应该从页面对象方法中删除browser.waitForAngular()并在我进行方法调用之后调用它...
Modified Page Object
修改了页面对象
...
this.goToCompanyPage = function () {
this.companyNameLink.click();
};
...
Spec File
规格文件
dashboardPage.goToCompanyPage();
browser.waitForAngular();
Is calling browser.waitForAngular()
causing the issue? Where should i call waitForAngular
is there any best practice on how to use this?
调用browser.waitForAngular()会导致问题吗?我应该在哪里调用waitForAngular有关于如何使用它的最佳做法?
3 个解决方案
#1
24
From protractor's documentation:
从量角器的文档:
Instruct webdriver to wait until Angular has finished rendering and has no outstanding $http or $timeout calls before continuing. Note that Protractor automatically applies this command before every WebDriver action.
指示webdriver等待Angular完成渲染,并且在继续之前没有未完成的$ http或$ timeout调用。请注意,Protractor会在每次WebDriver操作之前自动应用此命令。
You shouldn't be calling this at all and I cannot think of a valid case where you should.
你根本不应该打电话给我,我想不出你应该做的有效案例。
#2
8
Instead of using waitForAngular
, you should handle the promise returned by click
.
您应该处理click返回的promise,而不是使用waitForAngular。
Thus, first of all, your page object methods should return those promises:
因此,首先,您的页面对象方法应该返回这些承诺:
this.goToCompanyPage = function () {
return this.companyNameLink.click();
};
Then, your actual use of this method can look like this:
然后,您实际使用此方法可能如下所示:
dashboardPage.goToCompanyPage().then(function() {
// this will be executed when the click is done.
// No need for any waitForAngular.
});
For some further examples, see my state/page-object version of the Angular PhoneCat Protractor test suite.
有关其他示例,请参阅Angular PhoneCat Protractor测试套件的状态/页面对象版本。
#3
0
I'm using browser.WaitForAngular() before navigating to a specific page via browser.Url Otherwise, I get errors in the browser log whose I'm asserting for in test cleanup.
我正在使用browser.WaitForAngular()之前通过browser.Url导航到特定页面。否则,我在浏览器日志中出现错误,我在测试清理中断言。
#1
24
From protractor's documentation:
从量角器的文档:
Instruct webdriver to wait until Angular has finished rendering and has no outstanding $http or $timeout calls before continuing. Note that Protractor automatically applies this command before every WebDriver action.
指示webdriver等待Angular完成渲染,并且在继续之前没有未完成的$ http或$ timeout调用。请注意,Protractor会在每次WebDriver操作之前自动应用此命令。
You shouldn't be calling this at all and I cannot think of a valid case where you should.
你根本不应该打电话给我,我想不出你应该做的有效案例。
#2
8
Instead of using waitForAngular
, you should handle the promise returned by click
.
您应该处理click返回的promise,而不是使用waitForAngular。
Thus, first of all, your page object methods should return those promises:
因此,首先,您的页面对象方法应该返回这些承诺:
this.goToCompanyPage = function () {
return this.companyNameLink.click();
};
Then, your actual use of this method can look like this:
然后,您实际使用此方法可能如下所示:
dashboardPage.goToCompanyPage().then(function() {
// this will be executed when the click is done.
// No need for any waitForAngular.
});
For some further examples, see my state/page-object version of the Angular PhoneCat Protractor test suite.
有关其他示例,请参阅Angular PhoneCat Protractor测试套件的状态/页面对象版本。
#3
0
I'm using browser.WaitForAngular() before navigating to a specific page via browser.Url Otherwise, I get errors in the browser log whose I'm asserting for in test cleanup.
我正在使用browser.WaitForAngular()之前通过browser.Url导航到特定页面。否则,我在浏览器日志中出现错误,我在测试清理中断言。