Why can’t I access ‘window’ in an exposeFunction() function with Puppeteer?

evaluate the function You can pass the dynamic script using evaluate. (async function(){ var puppeteer = require(‘puppeteer’); const browser = await puppeteer.launch(); const page = await browser.newPage(); var functionToInject = function(){ return window.navigator.appName; } var data = await page.evaluate(functionToInject); // <– Just pass the function console.log(data); // outputs: Netscape await browser.close(); })() addScriptTag and readFileSync … Read more

Message “Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout”

The timeout you specify here needs to be shorter than the default timeout. The default timeout is 5000 and the framework by default is jasmine in case of jest. You can specify the timeout inside the test by adding jest.setTimeout(30000); But this would be specific to the test. Or you can set up the configuration … Read more

Headless browser detection

There is a headless browser detection test which tests for the following: Does the User-Agent contain the string “HeadlessChrome”? Is navigator.webdriver set? Is window.chrome unset? Does the browser skip asking for permissions (like notifications)? Are browser plugins unavailable? Is navigator.languages unset? If your browser answers any of these questions with yes, then you fail the … Read more

How do you click on an element with text in Puppeteer?

Short answer This XPath expression will query a button which contains the text “Button text”: const [button] = await page.$x(“//button[contains(., ‘Button text’)]”); if (button) { await button.click(); } To also respect the <div class=”elements”> surrounding the buttons, use the following code: const [button] = await page.$x(“//div[@class=”elements”]/button[contains(., ‘Button text’)]”); Explanation To explain why using the text … Read more