问题
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