TestCafé测试中注入的injectScripts中的脚本在哪里? [英] Where are scripts in injectScripts injected in TestCafé tests?

查看:67
本文介绍了TestCafé测试中注入的injectScripts中的脚本在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am setting up TestCafé tests programmatically and I use the injectScripts config on the Runner class to inject functions. According to the documentation, these scripts are added to the header of the tested page. Is it possible to invoke the functions from the test itself? I haven't found a way to do it. I can see that the scripts map is accessible inside the test and I can log out the content by doing

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

但是解析此地图并评估脚本将是非常丑陋的... 我该如何准确地调用测试中的注入函数?

But it would be utterly ugly to parse this map and eval the scripts... How can I, or can I be precise, invoke an injected function from the test?

推荐答案

您可以使用

You can use the ClientFunction or eval APIs to address injected scripts or any other client script from a test. Please take a look at the following example:

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

这篇关于TestCafé测试中注入的injectScripts中的脚本在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆