Home » Javascript » Is is possible to include a source file in nodejs+puppeteer to add functions?

Is is possible to include a source file in nodejs+puppeteer to add functions?

Posted by: admin August 19, 2018 Leave a comment

Questions:

I know

await page.evaluateOnNewDocument(fs.readFileSync('./helperFunctions.js', 'utf8'));

to add functions to evaluate() context, that is very handy.

But is anyone can provide any example to have the same on the main context ? By example, said I want to add a

page.existsText()

or

existsText()

function with this code from a file to be included/sourced :

existsText = (string) => {
    // code goes here
}

What is the way to go ?

Answers: