The application i am working throws a lot of unexpected alerts . I wanted to implement a way that catch all these through a common method isAlert present .
But how to call isAlertpresnt for each step in webdrriver , can anyone help me here ?
Typically my method would be like this :-
public void checkAlert() {
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (Exception e) {
//exception handling
}
}
The issue is how to call it before every step ? I know this will make my code slow but badly want this to be implemented .
I am looking for something which executes after each command\step in my test .Is this possible ?
Currently i am calling this method in all expected scenarios with a try catch.
Already one good answer is provided as by using event-listener.
Another simple way to handle, if your are using keywords/methods for all selenium actions. What i meant to say is, if you are using like click("locator") method to perform click in your test cases instead of writing driver command again and again, then you can insert that alert cross checking command after click in that click method.
so if you are using methods like click, input etc for selenium actions, then you can try like above.
Thank You, Murali
WebDriver has
WebDriverEventListener
listener for what you are trying to do exactly. By implementing this listener and registering the driver - you can get thischeckAlert
method called behind the scenes before/after every action you will perform using webdriver.Take a look at this example.
http://toolsqa.com/selenium-webdriver/event-listener/