Tuesday 24 September 2013

Handling different Javascript prompts using Selenium WebDriver

Purpose: Handling different Javascript prompts using Selenium WebDriver

We always come across different javascript prompts while accessing different websites. Follwoing code will help to handle while automating those functionalities.

This requires one web page which contains different ways to generate Javascript prompts. Following code for Web page

jscriptpopup.html

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("This is a JavaScript Alert Box!")
document.getElementById("result").innerHTML="Alert Prompted";
}
function show_confirm()
{
 var r=confirm("Press a button");
 if (r==true)
   {
   x="Pressed OK Button!";
   document.getElementById("result").innerHTML=x;
   }
 else
   {
   x="Pressed Cancel Button!";
   document.getElementById("result").innerHTML=x;
   }
}
function show_prompt()
{
 var v = prompt("Enter Value");
 if(v!=null)
  document.getElementById("result").innerHTML="Entered Value is "+v;
  else
 document.getElementById("result").innerHTML="Pressed Cancel button";
}
</script>
</head>
<body>
<input type="button" onclick="show_alert()" value="Show Alert Box" />
<input type="button" onclick="show_confirm()" value="Show Confrim Box" />
<input type="button" onclick="show_prompt()" value="Show Prompt Box" />
<div id="result"></div>
</body>
</html>

Please save the above code and provide the location of the file in WebDriver code

Webdriver Code:

 import org.openqa.selenium.By;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class JscriptPop {

 public String baseURL = "file:///D:/Raju/Projects/Automation/Sample/jscriptpopup.html"; // Location of the source file
 public Alert alert;
 public static WebDriver driver=new FirefoxDriver();

 @BeforeMethod
 public void setup()
 {
  driver.get(baseURL);
 }

 @AfterMethod
 public void tear()
 {
  driver.quit();
 }

 @Test
 public void testJscriptPop() throws InterruptedException
 {
  // Clicking Alert Box
  driver.findElement(By.xpath("//input[@value='Show Alert Box']")).click();
  Thread.sleep(3000);
  alert = driver.switchTo().alert();
  Thread.sleep(3000);
  // To click ok
  alert.accept();
  Thread.sleep(3000);

  // Clicking Confirm Box
  driver.findElement(By.xpath("//input[@value='Show Confrim Box']")).click();
  Thread.sleep(3000);
  alert = driver.switchTo().alert();
  Thread.sleep(3000);
  // To click OK
  alert.accept();
  Thread.sleep(3000);

  // Clicking Confirm Box
  driver.findElement(By.xpath("//input[@value='Show Confrim Box']")).click();
  Thread.sleep(3000);
  alert = driver.switchTo().alert();
  Thread.sleep(3000);
  // To click Cancel
  alert.dismiss();
  Thread.sleep(3000);

  // Clicking Confirm Box Prompt Button
  driver.findElement(By.xpath("//input[@value='Show Prompt Box']")).click();
  Thread.sleep(3000);
  alert = driver.switchTo().alert();
  alert.sendKeys("Test value");
  Thread.sleep(3000);
  // To click OK
  alert.accept();
  Thread.sleep(3000);

  // Clicking Confirm Box Prompt Button
  driver.findElement(By.xpath("//input[@value='Show Prompt Box']")).click();
  Thread.sleep(3000);
  alert = driver.switchTo().alert();
  Thread.sleep(3000);
  // To click Cancel
  alert.dismiss();
  Thread.sleep(3000);
 }
}

1 comment: