量角器中的 browser.ignoreSynchronization 是什么? [英] What is browser.ignoreSynchronization in protractor?
问题描述
我已经多次看到人们建议使用它:
I have seen it so many times where people suggest to use:
browser.ignoreSynchronization=true; // or false
但我不明白我们为什么需要它?
But I do not understand why do we need it?
推荐答案
简单的答案是它让量角器不再等待 Angular 的承诺,例如来自 $http
或 $timeout 的承诺
解决,如果您在 $http
或 $timeout
期间测试行为(例如,加载"消息),您可能想要这样做,或测试非 Angular 网站或页面,例如单独的登录页面.
The simple answer is that it makes protractor not wait for Angular promises, such as those from $http
or $timeout
to resolve, which you might want to do if you're testing behaviour during $http
or $timeout
(e.g., a "loading" message), or testing non-Angular sites or pages, such as a separate login page.
例如,要测试在请求期间设置加载消息的按钮,您可以在获取元素并检查其内容时将其设置为 true
For example, to test a button that sets a loading message during a request you can set it to true
when fetching an element + checking its contents
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
的函数将被注入控制流 after 测试中的其余功能已添加到控制流中.
A more involved answer is that setting it to true
means that subsequent additions/injections to the control flow don't also add browser.waitForAngular
. There are cases when an understanding of the control flow, and when/how things are added/injected into it is important. For example if you're using browser.wait
to test a multi-stage process, the function passed to wait
is injected into to the control flow after the rest of the functions in the test have added to the control flow.
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
An alternative to using browser.ignoreSynchronization
is to access the standard webdriver API directly
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代码>.
Using the driver methods directly to find the elements means that the system will try to find them without waiting for any ongoing $http
requests to finish, much like setting browser.ignoreSynchronization = true
.
这篇关于量角器中的 browser.ignoreSynchronization 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!