我正在和实习生一起编写一些功能测试,并遇到了以下部分文本...
"如果在测试超时内未履行承诺,测试也将失败(默认值为 30 秒;设置此超时以更改值)。
在。。。
https://github.com/theintern/intern/wiki/Writing-Tests-with-Intern#asynchronous-testing
如何设置功能测试的承诺超时?我尝试直接在承诺上调用 timeout(),但它不是一个有效的方法。
我已经设置了各种 WD 超时(页面加载超时、隐式等待等),但我遇到了承诺超时的问题。
通过建议的 API 在我的测试中设置超时不起作用。它远非理想,但我最终在我想要的超时时间内直接修改了 Test.js 和硬编码。在查看源代码时,我确实注意到超时代码上有一条评论,说//TODO 超时尚未正常工作
它似乎在最新版本上工作正常:
define([
'intern!object',
'intern/chai!assert',
'require',
'intern/dojo/node!leadfoot/helpers/pollUntil'
], function (registerSuite, assert, require, pollUntil) {
registerSuite(function(){
return {
name: 'index',
setup: function() {
},
'Test timeout': function () {
this.timeout = 90000;
return this.remote.sleep(45000);
}
}
});
});
您还可以将defaultTimeout: 90000
添加到配置文件(默认教程代码库中tests/intern.js
)以全局设置超时。这对我来说很有效。
测试的超时可以通过将超时作为第一个参数传递给 this.async
来设置,也可以通过设置this.timeout
(它是一个属性,而不是一个方法)来设置。
对于使用 InternJS 4 并使用 async
/await
进行功能测试的任何人:timeout
和executeAsync
对我不起作用,但下面的模式确实如此。基本上我只是执行了一些逻辑,并以比setTimeout
更长的间隔使用 sleep
方法。请记住,在 execute
内部运行的javascript
是块范围的,因此您需要稍后在 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"`
);
});