Javascript 为什么在编写量角器测试时必须使用 browser.sleep

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

Why must I use browser.sleep while writing protractor tests

javascriptangularjsseleniumselenium-webdriverprotractor

提问by Matthew Harwood

My first run at E2E tests. I'm trying to digest someone else's protractor tests.

我第一次参加 E2E 测试。我正在尝试消化其他人的量角器测试。

Problem:There are a lot of browser.driver.sleepand this seems fragile.

问题:有很多,browser.driver.sleep这似乎很脆弱。

Goal:not to use browser.driver.sleep

目标:不使用browser.driver.sleep

Question:What is a better approach to browser.driver.sleep? Something less fragile like a promise or something I dont know about lol?

问题:什么是更好的方法browser.driver.sleep?一些不那么脆弱的东西,比如承诺或我不知道的东西,哈哈?

var config = require('../../protractor.conf.js').config;
describe('this Homepage Body Tests', function(){
browser.driver.get(config.homepageUrl);

it("should open find a clinic page", function(){
  // page loads :: want to fix this random wait interval 
  browser.driver.sleep(2000);
  browser.ignoreSynchronization = true;

  var string = 'clinic';
  var main = '.search-large-text';
  var link = element(by.cssContainingText('.submenu li a', string));

  link.click().then(function() {
    // page reloads :: want to fix this random wait interval
    browser.driver.sleep(3000);
    var title = element(by.cssContainingText(main, string));
    expect(title.getText()).toBe(string);
  });
});
});

采纳答案by alecxe

Since there is an ignoreSynchronizationturned on, you cannot use waitForAngular(), which would be a solution in case of an angular-site testing.

由于ignoreSynchronization打开了,您不能使用waitForAngular(),这将是角度站点测试的解决方案。

A better solution here would be to set a page load timeout:

这里更好的解决方案是设置页面加载超时

browser.manage().timeouts().pageLoadTimeout(10000);  // 10 seconds

See also these relevant threads on explicit waits and timeouts:

另请参阅有关显式等待和超时的这些相关线程: