Where are scripts in injectScripts injected in TestCafé tests?

假装没事ソ 提交于 2019-12-24 03:56:05

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/58012508/where-are-scripts-in-injectscripts-injected-in-testcaf%c3%a9-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!