[英]"Failed: Error while waiting for Protractor to sync with the page" while executing Protractor tests
I try to execute some Protractor tests on a web application that consists of both Angular and non-angular components.我尝试在包含 Angular 和非 Angular 组件的 Web 应用程序上执行一些 Protractor 测试。 My code looks like so:
我的代码看起来像这样:
describe("Test Name", function() {
it("Test case", function() {
// first execute steps on a non-Angular component
browser.waitForAngularEnabled(false);
// some test steps
// then execute tests on an Angular component, which opens in a new browser tab
browser.waitForAngularEnabled(true);
// some more test steps
});
});
The problem is that after the above test is run, the browser launches and immedietaly closes with the following error:问题是上面的测试运行后,浏览器启动并立即关闭,出现以下错误:
Failed: Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined. This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping. See https://github.com/angular/protractor/issues/2643 for details"
失败:等待 Protractor 与页面同步时出错:“angularJS 可测试性和 Angular 可测试性都未定义。这可能是因为这是一个非角度页面,或者因为您的测试涉及客户端导航,这可能会干扰 Protractor 的引导。有关详细信息,请参阅https://github.com/angular/protractor/issues/2643 ”
When I remove browser.waitForAngularEnabled(true);
当我删除
browser.waitForAngularEnabled(true);
from the code, the steps for the non-Angular component are executed successfully, then the Angular component of the application is opened in the new browser tab, nothing happens for 10 seconds (no steps are executed) and the browser closes with the following error:从代码来看,非Angular组件的步骤执行成功,然后在新的浏览器选项卡中打开应用程序的Angular组件,10秒没有任何反应(没有执行任何步骤),浏览器关闭并出现以下错误:
Failed: Wait timed out after 10007ms
失败:等待 10007 毫秒后超时
You should probably account for asynchronous code and wait for promises to resolve.您可能应该考虑异步代码并等待承诺解决。 Also, add Jasmine's
done
parameter to the test function to let selenium know when the test is finished.另外,将Jasmine 的
done
参数添加到测试函数中,让 selenium 知道测试何时完成。
Another thing that might cause this is activating waitForAngularEnabled
before you're actually in an angular page.可能导致此问题的另一件事是在您实际进入角度页面之前激活
waitForAngularEnabled
。 I suggest you prefix that call with a call to check that something on the page already got loaded so you know angular is ready to be hooked by protractor before waiting for angular activities.我建议您在该调用前添加一个调用,以检查页面上的某些内容是否已加载,以便您知道 angular 已准备好在等待 angular 活动之前被量角器挂钩。
It's important to note that protractor waits for the next action after waitForAngularEnabled(true)
to trigger the check, relying on that might make the problem unclear if sometime later someone changes the code.重要的是要注意,量角器在
waitForAngularEnabled(true)
之后等待下一个动作来触发检查,如果稍后有人更改代码,依赖它可能会使问题变得不清楚。
describe("Test Name", function() {
it("Test case", function(done) {
// first execute steps on a non-Angular component
browser.waitForAngularEnabled(false)
.then(() => /* step1 */)
.then(() => /* step2 */)
// ...
// 👇 this one is very important to make sure
// we're in an angular page 👇
.then(() => ensurePageTitleIsVisible())
.then(() => browser.waitForAngularEnabled(true))
.then(() => done())
.catch((err) => done.fail(err));
});
});
function ensurePageTitleIsVisible() {
return browser.wait(ExpectedConditions.visibilityOf(PAGE_TITLE_SELECTOR), jasmine.DEFAULT_TIMEOUT_INTERVAL, 'Title does not exist after timeout');
}
This might give you a better error message as well.这也可能会为您提供更好的错误消息。
and of course, you can do the same with async \\ await syntax.当然,你可以用 async \\ await 语法做同样的事情。
describe("Test Name", function() {
it("Test case", function(done) {
try {
// first execute steps on a non-Angular component
await browser.waitForAngularEnabled(false)
await step1();
await step2();
// ...
await browser.waitForAngularEnabled(true);
done();
} catch(err) {
done.fail(err);
}
});
});
Basically, your problem happens because you continue with the test steps before the browser.waitForAngularEnabled
function actually finishes.基本上,您的问题发生是因为您在
browser.waitForAngularEnabled
函数实际完成之前继续执行测试步骤。
use browser.ignoreSynchronization = true;
使用
browser.ignoreSynchronization = true;
when interacting with non angular ui since it makes protractor not to wait for Angular promises与非角度 ui 交互时,因为它使量角器不等待 Angular 承诺
and browser.ignoreSynchronization = false;
和
browser.ignoreSynchronization = false;
followed by browser.waitForAngular();
其次是
browser.waitForAngular();
when interacting with angular ui与 Angular ui 交互时
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.