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();
});
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();
});
Share
Improve this question
edited Aug 3, 2020 at 2:15
painotpi
6,9962 gold badges39 silver badges72 bronze badges
asked Aug 2, 2020 at 20:33
Roboman RoboRoboman Robo
6892 gold badges8 silver badges19 bronze badges
3
|
1 Answer
Reset to default 18You 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.)
alert
fn keep calling itself? – Dennis Hackethal Commented Aug 2, 2020 at 20:38await
andasync
keywords except for maybe the very firstawait
(and you're using them wrong anyway, since you're usingawait
on a function declaration). So basically try getting rid of all theasync
andawait
statements inside the call topage.evaluate
, run it again, and let me know what you see. – Dennis Hackethal Commented Aug 2, 2020 at 21:06