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

Where are scripts in injectScripts injected in TestCafé tests?

我正在以编程方式设置 TestCafé 测试,我使用 Runner class 上的 injectScripts 配置来注入函数。 根据文档,这些脚本被添加到测试页面的 header 中。是否可以从测试本身调用函数?我还没有找到办法。 我可以看到脚本映射在测试中是可访问的,我可以通过执行

注销内容
console.log(t.testRun.opts.clientScripts)

但是解析这张地图并评估脚本会非常难看... 我如何,或者我能否准确地从测试中调用注入函数?

您可以使用 ClientFunction or eval 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!');
});