Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
});
like image 490
Roboman Robo Avatar asked Aug 02 '20 20:08

Roboman Robo


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

like image 116
pguardiario Avatar answered Nov 02 '22 00:11

pguardiario