【发布时间】:2015-05-02 17:26:09
【问题描述】:
我在人们建议使用的地方见过很多次:
browser.ignoreSynchronization=true; // or false
但我不明白我们为什么需要它?
【问题讨论】:
标签: javascript angularjs testing protractor end-to-end
我在人们建议使用的地方见过很多次:
browser.ignoreSynchronization=true; // or false
但我不明白我们为什么需要它?
【问题讨论】:
标签: javascript angularjs testing protractor end-to-end
此设置控制量角器是否应等待页面上的角度。它没有正确记录,但这里是documentation string from the code:
/**
* If true, Protractor will not attempt to synchronize with the page before
* performing actions. This can be harmful because Protractor will not wait
* until $timeouts and $http calls have been processed, which can cause
* tests to become flaky. This should be used only when necessary, such as
* when a page continuously polls an API using $timeout.
*
* @type {boolean}
*/
换句话说,如果您正在针对非 Angular 站点进行测试 - 将 ignoreSynchronization 设置设置为 true。作为一个真实的例子,看看我在从 Angular 页面打开非 Angular 页面时遇到的挑战之一:Non-angular page opened after a click。
【讨论】:
简单的答案是,它使量角器不等待 Angular 承诺,例如来自 $http 或 $timeout 的承诺来解决,如果您在 $http 或 @987654324 期间测试行为,您可能想要这样做@(例如,“加载”消息),或测试非 Angular 站点或页面,例如单独的登录页面。
例如,要测试在请求期间设置加载消息的按钮,您可以在获取元素并检查其内容时将其设置为 true
element(by.css('button[type="submit"]')).click();
browser.ignoreSynchronization = true;
expect(element(by.css('.message')).getText().toBe('Loading...');
browser.ignoreSynchronization = false;
expect(element(by.css('.message')).getText().toBe('Loaded');
一个更复杂的答案是,将其设置为true 意味着随后对控制流的添加/注入不会同时添加browser.waitForAngular。在某些情况下,了解控制流以及何时/如何将事物添加/注入其中很重要。例如,如果您使用browser.wait 测试多阶段流程,则传递给wait 的函数将被注入到控制流中在测试中的其余函数已添加到控制流。
element(by.css('button[type="submit"]')).click();
browser.ignoreSynchronization = true;
expect(element(by.css('.message')).getText().toBe('Stage 1');
browser.wait(function () {
// This function is added to the control flow after the final
// browser.ignoreSynchronization = false in the test
// so we need to set it again here
browser.ignoreSynchronization = true;
return element(by.cssContainingText('.message', 'Stage 2')).isPresent().then(function(isPresent) {
// Cleanup so later tests have the default value of false
browser.ignoreSynchronization = false;
return !isPresent;
});
});
expect(element(by.css('.message')).getText().toBe('Stage 2');
browser.ignoreSynchronization = false;
expect(element(by.css('.message')).getText().toBe('Stage 3');
使用browser.ignoreSynchronization 的替代方法是直接访问标准的webdriver API
element(by.css('button[type="submit"]')).click();
expect(browser.driver.findElement(by.css('.message')).getText().toBe('Loading...');
expect(element(by.css('.message')).getText().toBe('Loaded');
直接使用驱动程序方法来查找元素意味着系统将尝试查找它们而无需等待任何正在进行的$http 请求完成,就像设置browser.ignoreSynchronization = true 一样。
【讨论】:
browser.ignoreSynchronization 现在什么都不是。字面意思
此命令已于 2018 年 1 月 25 日量角器 v5.3.0 发布时弃用
应该改用browser.waitForAngularEnabled()
但它的作用和 ignoreSynchronization 过去所做的,它启用了 Protractor 的内置处理等待角度应用程序。想象一下,您单击登录按钮,并且不需要使用“睡眠 10 秒”命令,或“等待加载动画消失”等。所有这些理论上都应该由开箱即用的量角器处理
但在现实中,有太多的边缘情况你不能依赖这个,不得不禁用这个功能,因为它会让脚本挂起。这就是await browser.waitForAngularEnabled(false) 发挥作用的时候。或await browser.waitForAngularEnabled(true) 重新启用它
【讨论】: