Selenium WebDriver – Handling Javascript Popups

by seetaram on July 5, 2011

In the latest release of Selenium 2.0 WebDriver Alert class is implemented.  Get the latest release of Selenium 2.0 from this Download link.  Download selenium-server-2.0rc3.zip and unzip into \Libraries folder.  Add “selenium-server-standalone-2.0rc3” to the classpath (Add the External Jar to buildpath of the project’s properties in Eclipse IDE).

Here, I have taken an example HTML file which produces an alert box after clicking a button on the page.  Below is the code for that HTML file.  Copy and paste this HTML code into notepad and save it as “jscriptpopup.htm”

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("This is an JavaScript alert box!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="Show alert box" />

</body>
</html>

Selenium 2.0 WebDriver provides class – Alert to handle the popups.  We can cancel the popup or else click on the OK button on the alert box.  We can also get the message on the alert box.

Before using the methods, alert class should be declared as follows:

Alert alert = driver.switchTo().alert();

Alert class provides various methods such as:

  1. accept()
  2. dismiss()
  3. getText()

This makes the Automation Tester’s life easier when there are lot of JavaScript popup’s in the Application under Test.

Create a new Java Class copy and paste the below code and then execute:

package com.selftechy.wdriver;
/*
 * Author - Seetaram Hegde
 * 
 */
import org.openqa.selenium.By;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class JscriptPopMain {
	public static void main(String[] args) {
		WebDriver driver=new FirefoxDriver();
		driver.get("file:///F:/Helios-Workspace/WebDriver/TestDataWebDriver/jscriptpopup.htm");
		driver.findElement(By.xpath("//input[@value='Show alert box']")).click();
		Alert alert = driver.switchTo().alert();
		System.out.println(alert.getText());
		alert.accept();
		driver.quit();
	}
}

Execution of the above code should produce the result as – “This is an JavaScript alert box!

{ 3 comments… read them below or add one }

Vikas Singh July 9, 2012 at 10:46 am

I tried using javascript but it does not work on Chrome browser but it works on Firefox. I used
Alert.accept() for chrome but it did not work so i used robot class to handle the pop ups. Can anyone tell me that on what browsers and versions, javascript could be executed.

Reply

raghuram August 22, 2013 at 9:30 am

Hi,

I am getting error when i try to run the above code.
org.openqa.selenium.UnhandledAlertException: Modal dialog present: This is an JavaScript alert box!
Build info: version: ’2.35.0′, revision: ‘c916b9d’, time: ’2013-08-12 15:42:01′
System info: os.name: ‘Linux’, os.arch: ‘amd64′, os.version: ’3.9.0-030900-generic’, java.version: ’1.6.0_45′

Browser: FF 22
OS: Ubuntu
Selenium jar : 2.35
Java : 1.6

Reply

seetaram August 26, 2013 at 4:34 am

You need to handle the modal dialog

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email