简体   繁体   中英

Is it possible to access af function defined on the website with Testcafé?

Lets say 'mywebsite.com' has a global function:

function sayHello(message){
console.log(message)
}

is it possible to run a test of that function with testcafé in nodejs?

So far i have this

import { Selector } from 'testcafe';

fixture `mywebsite test`
    .page `http://mywebsite.com`;

test('sayHelloFuncTest', async t => {

    sayHello('HELLO!')

});

This will give me an error saying: ReferenceError: sayHello is not defined

So is there a way to access the sayHello() function?

You can use the ClientFunction to execute some script in the browser.

For example:

import { Selector, ClientFunction } from 'testcafe';

fixture `Fixture`
    .page `example.com`;

test('Test', async t => {
    const callSayHello = ClientFunction(() => { sayHello('HELLO!'); });

    await callSayHello();
});

or

await t.eval(() => { sayHello('HELLO!'); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM