对于任何在使用 InternJS 4 并利用async
/await
进行功能测试时在这里找到自己的方式的人:这对我不起作用,但下面的模式可以timeout
。executeAsync
基本上我只是执行了一些逻辑,并sleep
以比setTimeout
. 请记住,其中的javascript
运行execute
是块范围的,因此您需要缓存以后要在window
对象上引用的任何内容。希望这可以节省其他人一些时间和挫败感......
test(`timer test`, async ({remote}) => {
await remote.execute(`
// run setup logic, keep in mind this is scoped and if you want to reference anything it should be window scoped
window.val = "foo";
window.setTimeout(function(){
window.val = "bar";
}, 50);
return true;
`);
await remote.sleep(51); // remote will chill for 51 ms
let data = await remote.execute(`
// now you can call the reference and the timeout has run
return window.val;
`);
assert.strictEqual(
data,
'bar',
`remote.sleep(51) should wait until the setTimeout has run, converting window.val from "foo" to "bar"`
);
});