简体   繁体   English

使用selenium-webdriver包的WebDriverJS for node编写e2e测试

[英]Writing e2e tests using the selenium-webdriver package for WebDriverJS for node

I'm working on porting tests from webdriver and java to webdriverjs and I'm wondering if someone could help me understand why this works: 我正在尝试将测试从webdriver和java移植到webdriverjs,我想知道是否有人可以帮助我理解为什么这样做:

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
  return driver.getTitle().then(function(title) {
    return assert.equal(title, 'webdriver - Google Search');
  });
}, 2000);

title correctly contains 'webdriver - Google Search' 标题正确包含“ webdriver-Google搜索”

And this fails: 这失败了:

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
  driver.getTitle().then(function(title) {
    assert.equal(title, 'webdriver - Google Search');
  });
}, 2000);

title contains the title from the homepage and not the search results page title包含首页的标题,而不是搜索结果页面的标题

Thanks 谢谢

The wait method requires the function return a boolean or a promise that resolves to a boolean in order to work properly. wait方法要求函数返回布尔值或解析为布尔值的promise才能正常工作。 In your first example returning the assert which is a boolean then makes the then call a promise that resolves to a boolean (which you are returning to wait) hence wait will work correctly. 在您的第一个示例中,返回断言(它是一个布尔值),然后使then调用一个promise,解析为一个布尔值(您将返回到等待状态),因此wait可以正常工作。 In the second example your function does not return anything which wait treats as immediate successful resolution, in other words it does not wait at all. 在第二个示例中,您的函数不返回等待被视为立即成功解决的任何内容,换句话说,它根本不等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM