Use Selenium WebDriver To Manage Javascript Popup

When we do manual test we always encounter javascript popup such as Alert, Prompt and Confirm in web pages. But how to handle those javascript popup automatically in selenium automation test? You can use Alerts API to process such javascript issues in Selenium WebDriver.

First you should get the Alert object in the web page by following java code.

/* First create Firefox driver object. */
 WebDriver ffDriver = new FirefoxDriver();
/* Get the Alert object triggered in the web page.
 * You can process javascript alert, confirm and prompt
 * with this object.
 * */
 Alert alert = ffDriver.switchTo().alert();

Then you can use below Alert methods to process the popup.

  1. dismiss(): Click Cancel button to dismiss the dialog.
  2. accept(): Click OK button to accept the dialog.
  3. getText(): Get the text presented on the dialog.
  4. sendkeys(String text): Send the text to prompt input text box.
  5. authenticateUsing(Credentials credentials): Use the credentials to perform authenticate in web browser.

All the examples in this article use following html web page.

Copy and Paste following html code into a text editor and save to file C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/webdriver/testAlertInSelenium.html.

There are 3 sections in the html code, section1 for alert example, section2 for confirm example and section3 for prompt example. You can see code comments for detail explanation.

<html>
<head>
<title>Process Javascript Popup In Selenium --- dev2qa.com</title>
</head>
<body>
<h3>Process Javascript Popup In Selenium Example</h3>

<!-- section1 : Begin alert section -->
<fieldset>
<legend>Alert Box</legend><p>Click below button to trigger an javascript popup.</p>
<button onclick="openAlertPopup()" id="btnTriggerAlert">Click Me To Trigger Alert</button>
<script>
function openAlertPopup()
{
 alert("This is the alert dialog triggered by selenium webdriver!");
}
</script>
</fieldset>
<!-- section1 : End alert section -->

<!-- section2 : Begin confirm section -->
<fieldset>
<legend>Confirm Box</legend><p>Click below button to trigger a confirm dialog.</p>
<button onclick="openConfirmPopup()" id="btnTriggerConfirm">Click Me To Trigger Confirm</button>
<p id="confirmText"></p>
<script>
function openConfirmPopup()
{
 var confirmText;
 var like = confirm("Do you like selenium webdriver.\n Press any button!");
 if (like==true)
 {
 confirmText="OK button clicked.";
 }
 else
 {
 confirmText="Cancel button clicked.";
 }
 document.getElementById("confirmText").innerHTML= "<font color=red>" + confirmText + "</font>";
}
</script>
</fieldset>
<!-- section2 : End confirm section -->

<!-- section3 : Begin prompt section -->
<fieldset>
<legend>Prompt Box</legend><p>Click below button to trigger a prompt dialog.</p>
<button onclick="openPromptPopup()" id="btnTriggerPrompt">Click Me To Trigger Prompt</button>
<p id="promptText"></p>
<script>
function openPromptPopup()
{
 var promptText;
 var name=prompt("Please input your name","Name");
 if (name!=null)
 {
 promptText = "Hi " + name + "! You are welcome to dev2qa.com";
 document.getElementById("promptText").innerHTML = "<font color=red>" + promptText + "</font>";
 }
}
</script>
</fieldset>
<!-- section3 : End prompt section -->

</body>
</html>

Following is the java code for each example.

Example1 : Handle alert popup in Selenium WebDriver. 

 /* First create Firefox driver object. */
 WebDriver ffDriver = new FirefoxDriver();
 
 /* Maximize the Firefox browser window. */
 ffDriver.manage().window().maximize();
 
 /* Open the html file which can generate javascript popup. */
 ffDriver.get("file:///C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/webdriver/testAlertInSelenium.html");
 
 /* Sleep 2 seconds to wait the page load. */
 Thread.sleep(2000);
 
 /* Find the button which can trigger popup by it's id. */
 By byId = By.id("btnTriggerAlert");
 
 WebElement alertBtn = ffDriver.findElement(byId);
 
 if(alertBtn!=null)
 {
 /* Click the button to trigger a javascript 
 * which will popup an dialog.*/
 alertBtn.click();
 }
 
 /* 
 * You can process javascript alert, confirm and prompt
 * with this object.
 * */
 Alert alertObj = ffDriver.switchTo().alert();
 
 /* Print out the popup text in the console. */
 System.out.println(alertObj.getText());
 
 /* Sleep 3 seconds to see the popup dialog. */
 Thread.sleep(3000);
 
 /* Accept this javascript popup and close it. */
 alertObj.accept();
 
 System.out.println("Click OK button automatically in testAlertInSelenium method.");
 
 /* Quit webdriver object and close Firefox web browser. */
 ffDriver.quit();

Example 2: Handle confirm popup in Selenium WebDriver. 

 /* First create Firefox driver object. */
 WebDriver ffDriver = new FirefoxDriver();
 
 /* Maximize the Firefox browser window. */
 ffDriver.manage().window().maximize();
 
 /* Open the html file which can generate javascript popup. */
 ffDriver.get("file:///C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/webdriver/testAlertInSelenium.html");
 
 /* Sleep 2 seconds to wait the page load. */
 Thread.sleep(2000);
 
 /* Find the button which can trigger confirm by it's xpath. */
 By byXPath = By.xpath("//button[@onclick='openConfirmPopup()']");
 
 WebElement confirmBtn = ffDriver.findElement(byXPath);
 
 if(confirmBtn!=null)
 {
 /* Click the button to trigger a javascript 
 * which will popup an confirm dialog.*/
 confirmBtn.click();
 }

 Alert alertObj = ffDriver.switchTo().alert();
 
 /* Print out the popup text in the console. */
 System.out.println(alertObj.getText());
 
 /* Sleep 2 seconds to see the dialog. */
 Thread.sleep(2000);
 
 /* Click cancel button in this confirmation popup and close it. */
 alertObj.dismiss();
 
 System.out.println("Click Cancel button automatically in testConfirmInSelenium method.");
 
 /* Sleep 3 seconds to see the effect of the javascript function.*/
 Thread.sleep(3000);
 
 /* Quit webdriver object and close Firefox web browser. */
 ffDriver.quit();

Example 3: Handle prompt popup in Selenium WebDriver. 

 /* First create Firefox driver object. */
 WebDriver ffDriver = new FirefoxDriver();
 
 /* Maximize the Firefox browser window. */
 ffDriver.manage().window().maximize();
 
 /* Open the html file which can generate javascript popup. */
 ffDriver.get("file:///C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/webdriver/testAlertInSelenium.html");
 
 /* Sleep 2 seconds to wait the page load. */
 Thread.sleep(2000);
 
 /* Find the button which can trigger prompt by it's id. */
 By byId = By.id("btnTriggerPrompt");
 
 WebElement promptBtn = ffDriver.findElement(byId);
 
 if(promptBtn!=null)
 {
 /* Click the button to trigger a javascript 
 * which will popup an prompt dialog.*/
 promptBtn.click();
 }
 
 Alert alertObj = ffDriver.switchTo().alert();
 
 alertObj.sendKeys("Jerry");
 
 /* Print out the text in the console. */
 System.out.println(alertObj.getText());
 
 /* Sleep 3 seconds to see the result in the console. */
 Thread.sleep(3000);
 
 /* Accept this popup and close it. */
 alertObj.accept();
 
 
 System.out.println("Click OK button automatically in testPromptInSelenium method.");
 
 /* Sleep 3 seconds to see the result in the console. */
 Thread.sleep(3000);
 
 /* Quit webdriver object and close Firefox web browser. */
 ffDriver.quit();

[download id=”681″]

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.