Alert is a pop up window that comes up on screen. Alert is used to give you some kind of information or to ask for permission to perform certain kind of operations.
Two types of Alerts dialog box :-
- Windows based alert pop ups
- Web based alert pop ups
Selenium webDriver provides an Alert interface to handle Web based alert dialogs box. If there is no dialog currently present, and you invoke this API, it throws back a NoAlertPresentException. To handle web based alert pop ups we can use several third party tools available for handling window alert pop ups like Sikuli,AutoIT etc.
Alter interface provides following methods to handle Web based alert pop ups.
- void accept()
- void dismiss()
- String getText()
- sendKeys(java.lang.String keysToSend)
Step 1 :- Switch to Alert pop up
The alert() method is used to switch the control to the recently generated pop up window.
Ex:-
Alert alert = driver.switchTo().alert();
Step 2 :- Perform following operation on Altert pop ups
1. alert.accept()
The method is used to accepts the alert thereby clicking on the Ok button.
Ex:-
Alert alert= driver.switchTo().alert();
alert.accept();
2. alert.dismiss();
The method is used to closes the alert thereby clicking on the Cancel button and hence the operation should not proceed.
The method is used to closes the alert thereby clicking on the Cancel button and hence the operation should not proceed.
Ex:-
Alert alert= driver.switchTo().alert();
alert.dismiss();
3. alert.getText()
This method isused to get all the text displayed on the alert box.
Ex:-
Alert alert= driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("Alert text is " + alertText);
4. alert.sendKeys("Password")
This method is used to enters the specified string pattern into the alert box.
Ex:-
Alert alert= driver.switchTo().alert();
alert.sendKeys("EnterString")
Comments
Post a Comment