Handling JavaScript Alert in Selenium WebDriver is big question if you have just started learning Selenium WebDriver. Because we always get multiple pop-ups and alert and some time we get information about validity of input and some time these pop-ups speaks about error, and also asks about user response.
But as a tester we need to verify the alert message and for the same we need to handle the Alert pop up. So its time to celebrate because Selenium WebDriver have provided us with Alert class to deal with JavaScript Alert In Selenium.
Since I have created this alert through a basic html file and it has one button Ok. So lets learn about all kind of javascript alerts present.
What is Alert In Selenium?
JavaScript Alert are most common information notification small window which grab the focus of all windows and intends end user to provide consent to proceed or cancel the changes.
So there are three common alert that we normally see in our day to day life
1- Plain Alert or Simple Alert: Such alert in Selenium Automation, Provides information to end user of recent change.
Such alert comes with only OK button.
2- Confirmation Box: Such alert comes on screen with some information of change on page and it always comes with two buttons, First One OK and Second One Cancel. Such confirmation box, in general we see when we are deleting any information or updating any information or navigating away from the recent page to another without making any change.
Here is one example screenshot:
3- Prompt Alert : Such alert always appears with three fields, One textbox which wants input from end user and two buttons. One Ok and Another Cancel.
Here is one sample prompt alert :
So lets learn handling of alert in selenium with one example and in this example I am taking Confirmation Alert.
Scenario : Suppose we have one button, and once we hit on the button a alert appears with two button Ok and Cancel
a) In first case, Here end user clicks on Ok button
@Test public void testAlertOk() { //Now we would click on AlertButton WebElement button = driver.findElement(By.id("AlerButton")); button.click(); try { //Now once we hit AlertButton we get the alert Alert alert = driver.switchTo().alert(); //Text displayed on Alert using getText() method of Alert class String AlertText = alert.getText(); //accept() method of Alert Class is used for ok button alert.accept(); //Verify Alert displayed correct message to user assertEquals("this is alert box",AlertText); } catch (Exception e) { e.printStackTrace(); } }
So we can see that we have used
driver.switchTo().alert();
This line of code is going to switch the cursor or focus from all windows to alert and here we can perform action on Alert In Selenium.
b) In second case we want to click on Cancel button
So above code would remain same only thing that would change in above script is
alert.accept();
For clicking on Cancel we need to use dismiss() method of Alert Class
alert.dismiss();
Hope this script is fruitful for you. This was my effort to share post on Alert In Selenium With Example.
Reference:
https://stackoverflow.com/questions/27841988/how-to-handle-javascript-alert-pop-up-window-in-selenium-webdriver
Leave a Reply