Alerts are notification message box, which appeared on screen display. Alerts give some information or ask for permission to perform certain action or job.
To automate the alert popups has always been a challenging or tricky task for automation tester.
There are two types of alert pop-ups :
- Web based alert pop ups
- Window based alert pop-ups
To handle web based alert pop up, Alert interface provides following methods:
// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.switchTo().alert();
//To Click on OK button.
alert.accept();
// To click on Cancel button.
alert.dismiss();
//To get or captured the text on the Alert.
alert.getText();
//To send the text to the prompt popup
alert.sendkeys();
//Is used to Authenticate by entering the credentials
alert.authenticateUsing(Credentials credentials);
Steps:-
1. Launch the URL
2. Now find the Xpath of alert button
Example:
import org.openqa.selenium.Alert;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
public class AlertHandling {
public static void main(String[] args) throws NoAlertPresentException, InterruptedException {
WebDriver driver = new FirefoxDriver();
// Alert Message handling
driver.get("http://www.seleniumframework.com/Practiceform/");
driver.findElement(By.xpath("//*[@id='alert']")).click();
// Switching to Alert
Alert alertbox=driver.switchTo().alert();
Thread.sleep(2000);
// Capturing alert message.
String alerttext=driver.switchTo().alert().getText();
// Displaying alert message
System.out.println(alerttext);
// Accepting alert
alertbox.accept();
}
}
0 Comment(s)