javascript
I write a lot of tests for new features within Firefox DevTools. We have hundreds of "mochitests" which open the browser and perform synthetic actions like clicking, typing, and other user actions. I've previously written about waitForever
which essentially halts following actions without locking the browser. Another utility I enjoy is waitForTime
, an async JavaScript function that I can await
to give breathing time between two tasks.
我为Firefox DevTools中的新功能编写了许多测试。 我们有数百个“ mochitests”,它们可以打开浏览器并执行诸如单击,键入和其他用户操作之类的综合操作。 之前,我曾写过关于waitForever
,它实际上是在不锁定浏览器的情况下停止了后续操作。 我喜欢的另一个实用程序是waitForTime
,这是一个异步JavaScript函数,可以await
两个任务之间的喘息时间。
Whenever I want to wait a given amount of time between tasks, I employ this function:
每当我想在任务之间等待给定的时间时,我都会使用以下功能:
function waitForTime(ms) {
return new Promise(r => setTimeout(r, ms));
}
/* Usage */
await waitForTime(200);
// ...do other thing...
await waitForTime(200);
// ...do next thing ...
It's important to point out that most waitForTime
calls don't appear in the final test, since arbitrary timeouts lead to intermittent test failures, but they are helpful in knowing where I need to add polling for some other condition!
重要的是要指出,大多数的waitForTime
调用不会出现在最终测试中,因为任意超时都会导致间歇性的测试失败,但是它们对于了解在其他情况下需要在何处添加轮询很有帮助!
javascript