Mainly JavaScript
popups are produced by web application and they can be easily controlled by the
web browser.
Webdriver API offer
the ability to cope with JavaScript alerts using Alerts API(application programming
interface)
// Handle to the
open alert, prompt or confirmation
Alert alertinterce = driver.switchTo().alert();
Alert alertinterce = driver.switchTo().alert();
Alert is an
interface. Below are the methods
//Click on OK button
on confirmation box.
alert.accept();
alert.accept();
// Click on cancel
button on confirmation box.
.
alert.dismiss()
.
alert.dismiss()
//Getting the text
which is present on Alert window.
alert.getText();
alert.getText();
//Passing the text
to the prompt popup like text box on alert window
alert.sendkeys();
alert.sendkeys();
//Used to authenticate
by passing the credentials like (user name and password)
alert.authenticateUsing(Credentials credentials)
alert.authenticateUsing(Credentials credentials)
Work with Alerts using Selenium Webdriver API:
Below is the html code
for alerts, please copy below code and make ahtml file and pass it to the
webdriver API.
<html>
<head>
<title>Selenium Misc Alerts Sample document </title>
</head>
<body>
<h2>Alert Box Example for learning</h2>
<fieldset>
<legend>Alert Box</legend><p>Click on the button to display an alert box.</p>
<button onclick="alertFunction()">Click On</button>
<script>
function alertFunction()
{
alert("Here is an example for alert
box!");
}
</script>
</fieldset>
</body>
</html>
Below script will
demonstrate you working with alerts popup using above html file.
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class PopupsHandlingExampled {
WebDriver driver=new FirefoxDriver();
@Test
public void ExampleForAlert() throws InterruptedException
{
driver.manage().window().maximize();
driver.get("file:///E:/path/alertsexample.html");
driver.findElement(By.xpath("//button[@onclick='alertFunction()']")).click();
Alert alert=driver.switchTo().alert();
System.out.println(“Here we go”+alert.getText());
alert.accept();
}
}
Work with confirmation popups
Below is the sample
code for confirmation Popup, please copy and make a html file and pass it to
the webdriver API as below.
<html>
<head>
<title>Selenium misc Confirm popup Sample </title>
</head>
<body>
<h2>Confirm Box Example</h2>
<fieldset>
<legend>Confirm Box</legend>
<p>Click the button to display a confirm box.</p>
<button onclick="confirmFunction()">Click Here</button>
<p id="confirmdemo"></p>
<script>
function confirmFunction()
{
var cb;
var c=confirm("Here is the Example for Confirm Box.\n Press any button!");
if (c==true)
{
cb="Clicked on OK button";
}
else
{
cb="Clicked on Cancel button";
}
document.getElementById("confirmdemo").innerHTML=cb;
}
</script>
</fieldset>
</body>
</html>
Below program will understanding
to you working on confirmation popup using above html file.
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class PopupsHandlingexample {
WebDriver driver=new FirefoxDriver();
@Test
public void ExampleForConfirmBoxtest() throws InterruptedException
{
driver.manage().window().maximize();
driver.get("file:///D:/path/confirmation.html");
driver.findElement(By.xpath("//button[@onclick='confirmFunction()']")).click();
Alert alert=driver.switchTo().alert();
System.out.println(alert.getText());
alert.dismiss();
}
}
Work with Prompt Popups below is the html filed
In below prompt, Enter
the text using webdriver sendkeys("text.. like security code") method
Below is the sample script
for prompt popup, please copy and make a html file and pass it to the webdriver
API.
<html>
<head>
<title>Selenium misc prompt popup Sample </title>
</head>
<body>
<h2>Prompt Box Example for test purpose</h2>
<fieldset>
<legend>Prompt Box</legend>
<p>Click on the button for understanding the prompt box.</p>
<button onclick="promptFunction()">Click on me</button>
<p id="promptdemo"></p>
<script>
function promptFunction()
{
var x;
var person=prompt("Please enter your login name","Your user name");
if (person!=null)
{
x="Hello " + person + "! Welcome to Selenium misc..";
document.getElementById("promptdemo").innerHTML=x;
}
}
</script>
</fieldset>
</body>
</html>
Below automation
script will demonstrate you working on prompt popup using above html file.
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class PopupsHandling {
WebDriver driver=new FirefoxDriver();
@Test
public void ExampleForPromptBoxfortest()
{
driver.manage().window().maximize();
driver.get("file:///C:/path/prompt.html");
driver.findElement(By.xpath("//button[@onclick='promptFunction()']")).click();
Alert alert=driver.switchTo().alert();
driver.switchTo().alert().sendKeys("Helllo");
alert.accept();
System.out.println(alert.getText());
}
}
No comments:
Post a Comment