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

Pri*_*har 62 javascript testing end-to-end angularjs protractor

我已经看过很多次人们建议使用它:

browser.ignoreSynchronization=true;  // or false
Run Code Online (Sandbox Code Playgroud)

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

Mic*_*mza 80

简单的答案是,它使量角器不等待Angular承诺,例如那些来自$http$timeout要解决的承诺,如果您正在测试行为$http$timeout(例如,"加载"消息)或测试非行为,您可能想要做角度网站或页面,例如单独的登录页面.

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

更复杂的答案是将其设置为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');
Run Code Online (Sandbox Code Playgroud)

使用的另一种方法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'); 
Run Code Online (Sandbox Code Playgroud)

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


ale*_*cxe 18

此设置控制量角器是否应等待页面上的角度.它没有正确记录,但这里是代码中文档字符串:

/**
   * 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}
   */
Run Code Online (Sandbox Code Playgroud)

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