早上好
我目前正在学习如何使用 javascript(使用 mocha)驱动 Selenium。我创建了一个非常基本的测试,它在运行时给我带来了很多麻烦。每当我运行测试时,都会创建一个新的 chrome 实例并显示浏览器。当浏览器最初出现时,它会在 URL 框中放置“数据:”,然后继续访问 google.com。然后我收到以下错误:
$ Mocha 测试
Array
#indexOf()
✓ should return -1 when the value is not present!
Google Search
1) should work
1 passing (2s)
1 failing
1) Google Search should work:
Error: timeout of 2000ms exceeded
at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:157:19)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
这是测试本身:
var assert = require('assert'),
test = require('selenium-webdriver/testing'),
webdriver = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome');
test.describe('Google Search', function() {
test.it('should work', function() {
var chromeOptions = new chrome.Options();
chromeOptions.addArguments(['test-type']);
var driver = new webdriver.Builder().withCapabilities(chromeOptions.toCapabilities()).build();
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 title === 'webdriver - Google Search';
});
}, 1000);
driver.quit();
});
});
最佳答案
在我看来,您收到的错误消息看起来像是 Mocha 超时。在 Mocha 中设置超时的正常方法是:
it("foo", function () {
this.timeout(value);
...
});
其中 value
是您想要的任何值(以毫秒为单位)。值为 0 会关闭 Mocha 的超时。默认为 2000 毫秒。
关于javascript - 使用 Selenium 总是得到 'Error: timeout of 2000ms exceeded',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26141016/