Javascript 量角器 - 在哪里使用 browser.waitForAngular()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30882671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protractor - Where to use browser.waitForAngular()
提问by Kasun Kodagoda
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()
.
我有一些使用量角器为 angular.js 应用程序编写的测试。我正在使用Page Objects
设计模式,并且有一些方法可以通过单击链接和按钮导航到其他页面。不久之后我就打电话了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..
在一些规范文件中,我像这样使用这个页面对象..
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...
但问题是有时我收到angular could not be found on the window
错误并且我的测试失败。大多数情况下,测试都会运行。这个问题是随机的。我的问题是我应该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
是否有关于如何使用它的最佳实践?
回答by Delian Mitankin
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 调用。请注意,量角器会在每个 WebDriver 操作之前自动应用此命令。
You shouldn't be calling this at all and I cannot think of a valid case where you should.
你根本不应该打电话给这个,我想不出一个你应该这样做的有效案例。
回答by avandeursen
Instead of using waitForAngular
, you should handle the promise returned by click
.
waitForAngular
您应该处理由 返回的承诺,而不是使用click
。
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 versionof the Angular PhoneCat Protractor test suite.
有关更多示例,请参阅我的 Angular PhoneCat Protractor 测试套件的状态/页面对象版本。
回答by Alexander Zhidkov
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.Url 导航到特定页面之前,我正在使用 browser.WaitForAngular() 否则,我会在浏览器日志中收到错误,我在测试清理中对其进行断言。