How to run a custom js function in playwright

Viewed 9835

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();
});
1 Answers

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.)

Related