我正尝试在 Selenium 上开始 javascript 测试,但我卡在了开头。
MochaJS 从不等待测试结束,而是在 2 秒后抛出
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/pavel/code/gscc/test/test.js)
我的代码是
const {Builder, By, Key, until} = require('selenium-webdriver');
let assert = require('chai').assert;
describe('Test Suite', function() {
it('should do', async function(done) {
try {
let driver = new Builder().forBrowser('firefox').build();
await driver.get('http://www.google.com/ncr');
const title = await driver.getTitle();
assert.equal(title, 'Google');
} catch(err) {
console.log(err);
} finally {
await driver.quit();
}
done();
})
})
Mocha 说我的代码中有 Unresolved promise ,但真的有吗?
最佳答案
好的,在调查之后,我将在解决问题时尝试回答我自己的问题。
首先,错误:超时超过 2000 毫秒。 是一个常见问题,只是超时已超出,但测试没有运行(因为它们需要更多时间才能运行)。它记录在 stackoverflow 上 pretty good .
下一个问题是 Mocha 等待 promise 或 done 函数来完成测试。当我写的
it('should do', async function(done) {
try {
let driver = new Builder().forBrowser('firefox').build();
await driver.get('http://www.google.com/ncr');
const title = await driver.getTitle();
assert.equal(title, 'Google');
} catch(err) {
console.log(err);
} finally {
await driver.quit();
}
done();
})
它既没有得到 promise 也没有完成,因为它不适用于异步/等待机制,只能用于标准 promise 。
所以我删除了 done 并且完全删除了 try-catch block ,现在它终于可以工作了!
最终代码为
describe('Test Suite', function() {
this.timeout(0);
before(async function() {
this.driver = await new Builder().forBrowser('firefox').build();
});
it('should do', async function() {
await this.driver.get('http://www.google.com/ncr');
const title = await this.driver.getTitle();
assert.equal(title, 'Google1121312213');
})
after(async function() {
this.driver.quit();
})
});
关于javascript - 为什么我不断收到错误 : Timeout of 2000ms exceeded from Mocha on async?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53258510/