我正在以编程方式设置 TestCafé 测试,并且使用injectScripts
配置上Runner
类来注入函数。
根据文档,这些脚本被添加到测试页面的标题中。是否可以从测试本身调用函数?我还没有找到办法做到这一点。
我可以看到脚本映射在测试中是可访问的,我可以通过执行以下操作来注销内容
console.log(t.testRun.opts.clientScripts)
但解析这张地图并评估脚本将是非常丑陋的......
我怎样才能,或者准确地说,从测试中调用注入的函数?
您可以使用ClientFunction https://devexpress.github.io/testcafe/documentation/test-api/obtaining-data-from-the-client/#creating-client-functions or eval https://devexpress.github.io/testcafe/documentation/test-api/obtaining-data-from-the-client/#one-time-client-code-execution用于处理测试中注入的脚本或任何其他客户端脚本的 API。请看下面的例子:
const scriptContent = `
function alertHelloWorld () {
alert('Hello world!');
}`;
fixture `My fixture`
.page `https://example.com`
.clientScripts({ content: scriptContent });
test('New Test', async t => {
await t.setNativeDialogHandler(() => true);
await t.eval(() => alertHelloWorld());
const history = await t.getNativeDialogHistory();
await t
.expect(history[0].type).eql('alert')
.expect(history[0].text).eql('Hello world!');
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)