在 TestCafé 测试中注入的 injectScripts 中的脚本在哪里?



我正在以编程方式设置TestCafé测试,并使用Runner类上的injectScripts配置来注入函数。 根据文档,这些脚本将添加到测试页面的标题中。是否可以从测试本身调用函数?我还没有找到一种方法。 我可以看到脚本映射可以在测试中访问,我可以通过执行以下操作来注销内容

console.log(t.testRun.opts.clientScripts)

但是解析这张地图并评估脚本将是完全丑陋的...... 我怎样才能,或者我可以准确地从测试中调用注入的函数?

您可以使用ClientFunctionevalAPI 来处理测试中注入的脚本或任何其他客户端脚本。请看以下示例:

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!');
});

最新更新