Showing posts with label Handle Alerts in Selenium. Show all posts
Showing posts with label Handle Alerts in Selenium. Show all posts

Friday, October 26, 2018

How to handle Alerts/Pop-ups in selenium WebDriver | Selenium WebDriver tutorial

In this tutorial, I am going to discuss how to handle alerts/pop-ups in selenium WebDriver scripts.

Topics to be covered - 
  • What is Alert?
  • Different types of Alert.
  • Alert Interface in Selenium WebDriver.
  • Methods of Alert Interface.
  • Practical Example.
  • Exception in Alert Interface.


Alert In Selenium





Alert is a small message box or pop-up that appears on the screen to display some notification/information to the users, asks permission form the users to perform some actions or asks some input from the users.


Types of Alerts -


 1. Simple Alert - This type of alert appears on the screen to give some notification/information to the users. It has the 'OK' button.




2. Prompt Alert - This type of alert appears on the screen to ask the user to input some values in the text field displayed on the alert box. It has 'OK' and 'CANCEL' buttons.





3. Confirmation Alert - This type of alert appears on the screen to get permission from the user to perform certain kind of actions It has 'OK' and 'CANCEL' buttons.





Alert Interface - 

To handle Alert pop-ups, Selenium provides an interface called Alert. To use it in the WebDriver script, we need to import one package

 import org.openqa.selenium.Alert;  

To handle Alert, WebDriver has provided one method -

  • driver.switchTo().alert() - This method switches to the alert available on the web-page. It returns a reference of the alert interface.


Methods provided by Alert Interface - 

  1.  void accept() - It clicks on 'OK' buttons.
  2. void dismiss() - It clicks on 'CANCEL' buttons.
  3. String getText() - It captures alert's message.
  4. void sendKeys() - It enters text value in the alert's text field.

Scenario
  1. Open rediff.com login page in chrome browser.
  2. Click on 'Login' button.
  3. Get the text of the alert box and verify it.
  4. If it verified then click on 'OK' button.


 package com.sessions;  
 import org.openqa.selenium.Alert;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 public class Selenium_Alert {  
      public static void main(String[] args) throws Exception {  
           System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
           WebDriver driver = new ChromeDriver();  
           //launch rediff application.  
           driver.get("https://mail.rediff.com/cgi-bin/login.cgi");  
           //maximizing browser.  
           driver.manage().window().maximize();  
           //clicking on 'GO' button.  
           driver.findElement(By.name("proceed")).click();  
           //Creating Alert Interface  
           Alert alert = driver.switchTo().alert();  
           //Storing Expected Alert Message  
           String ExpectedAlertMessage = "Please enter a valid user name";  
           //Get text message of the alert box  
           String ActualAlertMessage = alert.getText();  
           //Putting execution of program in sleep for 5 secs.  
           Thread.sleep(5000);  
           //Comparing actual and expected. If matches click on 'OK' button.  
           if(ExpectedAlertMessage.equals(ActualAlertMessage))  
           {  
                alert.accept();  
           }  
      }  
 }  

Code Walk Through - 


1. Open rediff.com login page in chrome browser.

 //launch rediff application.  
 driver.get("https://mail.rediff.com/cgi-bin/login.cgi");  

2. Maximize the browser

 //maximizing browser.  
 driver.manage().window().maximize();  

3. Click on the 'GO' button.

 //clicking on 'GO' button.  
 driver.findElement(By.name("proceed")).click();  

Note - After clicking on the 'GO' button, an alert will be displayed. So, we need to switch to it as discussed above.

4. Create an Alert Interface reference and switch to the Alert.

 //Creating Alert Interface  
 Alert alert = driver.switchTo().alert();  

5. Store Expected Alert text message value in a string variable.

 //Storing Expected Alert Message  
 String ExpectedAlertMessage = "Please enter a valid user name";  

6. Get the Actual alert text message from the alert box and store it also in a string variable.

 //Get text message of the alert box  
 String ActualAlertMessage = alert.getText();  

7. Stop the execution of the program for some seconds.

 //Putting execution of program in sleep for 5 secs.  
 Thread.sleep(5000);  

Note - If you automate navigation to alert and performing any action on it, then these executions will happen so fast that you even won't be able to observe alert properly. So, it is necessary to halt execution of the program for some seconds in order to see the alert on screen.


8. Now, compare actual alert text message with the expected alert text message and if matches then only click on 'OK' button.

 //Comparing actual and expected. If matches click on 'OK' button.  
           if(ExpectedAlertMessage.equals(ActualAlertMessage))  
           {  
                alert.accept();  
           }  


Point To Note -

What if there is no alert and you wrote code for handling alert??

The system will throw NoAlertPresentException on the code line where you are trying to switch to alert.



That all guys!!! 

In my next tutorial, I am going to cover - How to handle frames in Selenium WebDriver



Do comment in the comment box and share your views about the post. If you have any query, please comment on it. It will help me to reach to you and help you to resolve your query.