In this blog we will be utilizing Playwright Java to handle javascript dialogs (alerts, popups, prompts).

Topics that we will cover:

  • Handle Javascript dialogs (example 1)
  • Handle Javascript dialogs (example 2)
  • Source code (example 1)
  • Source code (example 2)

Handle Javascript dialogs (example 1)

Go to https://the-internet.herokuapp.com/javascript_alerts 

To handle this, we can add “onDialog” listener

 Whenever the javascript dialog/alert appears, this particular “onDialog” listener will be invoked.

In line#18, we have passed the lambda function inside the method parameter.

In line#26, we are clicking the ‘Ok’ button by calling the accept method. 

In line#28, we are printing the message on the dialog box.

In line#31, we are clicking the button to open the alert dialog. Notice that we have placed this after the listener code. So it does not matter where we keep the ‘click’ method. The listener keeps listening for any alert.

Execute.

Notice below that alert comes up

We add the lines#32 and 33. 

Rest of the logic would remain same

Handle Javascript dialogs (example 2)

Let us see another example, go to https://mail.rediff.com/cgi-bin/login.cgi 

The below code will click the ‘Ok’ button and will capture the text of this alert box

 Execute.

The popup comes up

 Source code (example 1)

package com.rsa.playwrightjava;


import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;


public class Blog26_JSDialogs_Example1 {

public static void main(String[] args) {

Playwright playwright = Playwright.create(); 

Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));


Page page = browser.newPage();

page.navigate("https://the-internet.herokuapp.com/javascript_alerts");

//JS Alert, confirm, prompt

page.onDialog(dialog -> {

try {

Thread.sleep(3000);

}catch (InterruptedException e) {

e.printStackTrace();

}

dialog.accept();


System.out.println(dialog.message());

});


page.locator("text=Click for JS Alert").click();

page.locator("text=Click for JS Confirm").click();

page.locator("text=Click for JS Prompt").click();


}

}


Source code (example 2)

package com.rsa.playwrightjava;


import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;


public class Blog26_JSDialogs_Example2 {

public static void main(String[] args) {

Playwright playwright = Playwright.create(); 

Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));


Page page = browser.newPage();

page.navigate("https://mail.rediff.com/cgi-bin/login.cgi");

//JS Alert

page.onDialog(dialog -> {

try {

Thread.sleep(3000);

}catch (InterruptedException e) {

e.printStackTrace();

}

dialog.accept();

System.out.println(dialog.message());

});

page.locator("text=Sign in").click();


}

}


Thanks.


Tags


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}