【问题标题】:Where are scripts in injectScripts injected in TestCafé tests?在 TestCafé 测试中注入的 injectScripts 中的脚本在哪里?
【发布时间】:2019-09-19 13:46:18
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript automated-tests e2e-testing inject testcafe


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 2020-01-24
    • 2014-02-28
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 2016-08-03
    相关资源
    最近更新 更多