Asynchronous 使用async/await进行异步mocha测试时遇到问题
我正在使用mocha和selenium webdriver进行E2E测试。大多数测试都是异步的,我使用async/await函数来处理这个问题。不幸的是,现在我一件事也做不成。下面是我的代码的样子:Asynchronous 使用async/await进行异步mocha测试时遇到问题,asynchronous,selenium-webdriver,async-await,mocha.js,Asynchronous,Selenium Webdriver,Async Await,Mocha.js,我正在使用mocha和selenium webdriver进行E2E测试。大多数测试都是异步的,我使用async/await函数来处理这个问题。不幸的是,现在我一件事也做不成。下面是我的代码的样子: describe('Some test', function () { before(function () { driver.navigate().to('http://localhost:3000') }) after(function () { driver.qu
describe('Some test', function () {
before(function () {
driver.navigate().to('http://localhost:3000')
})
after(function () {
driver.quit()
})
it('should display element', async function () {
let elementFound = false
try {
await driver.wait(until.elementIsVisible(driver.findElement(By.className('element'))), 1000)
assessForm = await driver.findElement(By.className('element')).isDisplayed()
assert.ok(elementFound)
console.log('elementFound', elementFound)
} catch (err) {
console.log(err)
assert.fail(err)
}
})
})
出现的问题似乎是在测试完成之前调用了after
函数。以下是错误日志:
错误:超过2000毫秒的超时时间。对于异步测试和挂钩,请确保
调用“done()”;如果返回承诺,请确保它已解决
{NoSuchSessionError:没有此类会话(驱动程序信息:
chromedriver=2.36.540469
(1881fd7f8641508feb5166b7cae561d87723cfa8),平台=Mac OS X 10.13.3
x86_64)
在Object.checkLegacyResponse(/Users/me./myproject/node_modules/selenium webdriver/lib/error.js:585:15)
在parseHttpResponse(/Users/me./myproject/node_modules/selenium webdriver/lib/http.js:533:13)
在Executor.execute(/Users/me./myproject/node_modules/selenium webdriver/lib/http.js:468:26)
在
在进程中。_tickCallback(internal/process/next_tick.js:188:7)名称:'NoSuchSessionError',remoteStacktrace:'}
如果我删除after()
函数,我仍然会得到
错误:超过2000毫秒的超时时间。对于异步测试和挂钩,请确保
调用“done()”;如果返回承诺,请确保它已解决
但是,my console.log显示已找到我的元素
如果我尝试使after()
异步,如下所示:
after(async function () {
await driver.quit()
})
我得到了与第一个相同的错误
还需要注意的是,我已经读到,在执行异步/等待时,我不必使用done()
。那到底是怎么回事?即使我这样做了,我也会不断地犯同样的错误
我如何解决这个问题?看起来一切都井然有序,但我似乎无法让测试正常运行而不互相碰撞。而不是使用:
await driver.wait(until.elementIsVisible(driver.findElement(By.className('element'))), 1000)
尝试:
await driver.wait(until.elementLocated(By.className('element'))).isDisplayed()
您的console.log何时显示已找到您的元素?超时错误之前或之后?可能是您的
Before
函数也应该是异步的,这样测试只能在导航完成后启动。