Search code examples
javascriptpuppeteerwebautomationplaywrightchrome-devtools-protocol

How to run a custom js function in playwright


How can I run a custom js function in playwright? For example show an alert. I have already tried this way but it did not work.

var url = await page.evaluate(async() => {
  await function alert() {
    alert("alert");
  }

  await alert();
});

Solution

  • You would just do:

    await page.evaluate(() => alert("alert"))
    

    But keep in mind, that alert's are blocking in the JavaScript browser world and dismissed automatically by Playwright. See here how to interact with them (dismiss e.g.)