How to handle javascript Popups in selenium webdriver using java?.

HTML code : 

<html>
<head>
<title>JavaScript Pupups </title>
</head>
<body>
<h2>Alert Box</h2>
<fieldset>
<legend>Alert Box</legend><p>Click the button to display an alert box.</p>
<button onclick=”alertFunction()”>Alerts</button>

function alertFunction()
{
alert(“I am an example for alert box!”);
}

</fieldset>

<h2>Confirm Box</h2>
<fieldset>
<legend>Confirm Box</legend>
<p>Click the button to display a confirm box.</p>
<button onclick=”confirmFunction()”>Confirm</button>
<p id=”confirmdemo”></p>

function confirmFunction()
{
var cb;
var c=confirm(“I am an Example for Confirm Box.\n Press any button!”);
if (c==true)
{
cb=”You Clicked on OK!”;
}
else
{
cb=”You Clicked on Cancel!”;
}
document.getElementById(“confirmdemo”).innerHTML=cb;
}

</fieldset>

<h2>Prompt Box</h2>
<fieldset>
<legend>Prompt Box</legend>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick=”promptFunction()”>Prompt</button>
<p id=”promptdemo”></p>

function promptFunction()
{
var x;
var person=prompt(“Please enter your name”,”Your name”);
if (person!=null)
{
x=”Hello ” + person + “! Welcome to Selenium Easy..”;
document.getElementById(“promptdemo”).innerHTML=x;
}
}

</fieldset>
</body>
</html>

 

Selenium Code : 

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class JavaScriptPopup {
public static void main(String[] a) throws InterruptedException {
// Initialize Web driver
WebDriver driver = new FirefoxDriver();
//Maximize browser window
driver.manage().window().maximize();
//Go to Page
driver.get(“file:///E:/Javascript%20Popup.html”);
//===========Alerts ======
//click on button
driver.findElement(By.xpath(“html/body/fieldset[1]/button”)).click();
//handle to the open alert.
Alert alert = driver.switchTo().alert();
//get the text which is present on th Alert.
System.out.println(alert.getText());
Thread.sleep(3000);
//Click on OK button.
alert.accept();
//===========Confirmation Popups======
//click on button
driver.findElement(By.xpath(“html/body/fieldset[2]/button”)).click();
//handle to the open confirmation popup.
Alert Confirm = driver.switchTo().alert();
Thread.sleep(3000);
//click on Cancel button.
Confirm.dismiss();
//===========Prompt Popups======
//click on button
driver.findElement(By.xpath(“html/body/fieldset[3]/button”)).click();
//handle to the open prompt popup.
Alert Prompt = driver.switchTo().alert();
//pass the text to the prompt popup
Prompt.sendKeys(“Prompt Box”);
Thread.sleep(3000);
//Click on OK button.
Prompt.accept();
//close browser
driver.quit();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s