Alerts,Confirmations and Prompts


Alert is a message box which displays on-screen notification to give the user some kind of information or ask for permission to perform certain kind of operation.

How to handle Alerts in selenium

Four methods are available for handling Alerts in Selenium
1) Void Dismiss() :
It will click on Cancel button when alert appears
2) Void accept():
It will click on OK button when alert appears
3) Void gettext():
It will capture the text message on alert
4) Void sendkey():
It will type the string pattern in alert box

Sample code for accept() and gettext() methods
packageactionsexp;
importorg.openqa.selenium.Alert;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
publicclass alertexp1 {
publicstaticvoidmain(String args[]){
WebDriver driver;
driver = newFirefoxDriver();
driver.get("http://www.ksrtc.in/oprs-web/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id='searchBtn']")).click();
Alert alert=driver.switchTo().alert();
String textmsg=alert.getText();
System.out.println("Alert Message -->"+textmsg);
alert.accept();
}
}

Sample html code for alert text box

<html>
<head>
<title>Selenium Prompt alert text </title>
</head>
<body>
<h2>Prompt Box Example</h2>
<fieldset>
<legend>Prompt Box</legend>
<button onclick="promptFunction()">Click on me</button>
<p id="promptdemo"></p>
<script>
function promptFunction()
{
var x;
var person=prompt("Please enter any thing","please enter");
if (person!=null)
{
x="your enterd text is ----> " + person + "";
document.getElementById("promptdemo").innerHTML=x;
}
}
</script>
</fieldset>
</body>
</html>

Copy the above code and make an html file in your local driver.

Sample Webdriver code for Alert – SendKey() and Dismiss() method

packageactionsexp;
importorg.openqa.selenium.Alert;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
publicclass alertexp1 {
publicstaticvoidmain(String args[]) throwsInterruptedException{
WebDriver driver;
driver = newFirefoxDriver();
driver.get("file:///E:/Books/seleniumbooks/alerttext.html");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[text()='Click on me']")).click();
Thread.sleep(2000);
Alert alert =driver.switchTo().alert();
// click on cancel button
alert.dismiss();
if (driver.findElement(By.xpath("//*[@id='promptdemo']")).isDisplayed()){
System.out.println("Successfully clicked on Cancel button");
}else{
System.out.println("Unsuccessfully clicked on Cancel button");
}
// below code is for enter some text in text box and click on ok button
driver.findElement(By.xpath("//*[text()='Click on me']")).click();
Thread.sleep(2000);
alert.sendKeys("Trend Nxt");
alert.accept();
if (driver.findElement(By.xpath("//*[@id='promptdemo']")).isDisplayed()){
String txt=driver.findElement(By.xpath("//*[@id='promptdemo']")).getText();
System.out.println("Successfully clicked on Ok button and message is --> "+txt);
}else{
System.out.println("Unsuccessfully clicked on ok button");
}
}
}

Note:
1) If alert is not present in the window and still we try to switchTo alert window then Selenium will throw NoAlertPresentException
2) Web-Based alert and Java Script alerts are same so do not get confused.
3) Until you do not handle alert window you cannot perform any action in the parent window.

Comments