Javascript 量角器中的 browser.ignoreSynchronization 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28808463/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:27:28  来源:igfitidea点击:

What is browser.ignoreSynchronization in protractor?

javascriptangularjstestingprotractorend-to-end

提问by Priyanshu Shekhar

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?

但我不明白我们为什么需要它?

回答by Michal Charemza

The simple answer is that it makes protractor not wait for Angular promises, such as those from $httpor $timeoutto resolve, which you might want to do if you're testing behaviour during $httpor $timeout(e.g., a "loading" message), or testing non-Angular sites or pages, such as a separate login page.

简单的答案是,它使量角器不会等待 Angular 承诺,例如那些来自$http$timeout解决的承诺,如果您在$http或期间测试行为$timeout(例如,“加载”消息),或测试非- Angular 站点或页面,例如单独的登录页面。

For example, to test a button that sets a loading message during a request you can set it to truewhen fetching an element + checking its contents

例如,要测试在请求期间设置加载消息的按钮,您可以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'); 

A more involved answer is that setting it to truemeans 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.waitto test a multi-stage process, the function passed to waitis injected into to the control flow afterthe rest of the functions in the test have added to the control flow.

一个更复杂的答案是将其设置为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');

An alternative to using browser.ignoreSynchronizationis to access the standard webdriver API directly

使用的替代方法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'); 

Using the driver methods directly to find the elements means that the system will try to find them without waiting for any ongoing $httprequests to finish, much like setting browser.ignoreSynchronization = true.

直接使用驱动程序方法来查找元素意味着系统将尝试查找它们,而无需等待任何正在进行的$http请求完成,就像设置browser.ignoreSynchronization = true.

回答by alecxe

This setting controls whether protractor should wait for angular on a page or not. It is not properly documented, but here is the 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}
   */

In other words, if you are testing against a non-angular site - set ignoreSynchronizationsetting to true. As a real world example, see one of the challenges I had while opening a non-angular page from an angular page: Non-angular page opened after a click.

换句话说,如果您针对非角度站点进行测试,请将ignoreSynchronization设置设置为true. 作为一个真实世界的例子,看看我在从一个有角度的页面打开一个非角度页面时遇到的挑战之一:点击后打开非角度页面