How To Handle Unexpected Alerts In Selenium WebDriver

Some times when we browsing software web application, Display some unexpected alerts due to some error or some other reasons. This kind of alerts not display every time but they are displaying only some time. If you have created webdriver software automation test case for such page and not handled this kind of alerts In your code then your script will fail Immediately If such unexpected alert pop up displayed.

We have already learnt, How to handle expected alert popups In selenium webdriver software testing tool In THIS EXAMPLE. Now unexpected alert appears only some times so we can not write direct code to accept or dismiss that alert. In this kind of situation, we have to handle them specially.


To handle this kind of unexpected alerts, You must at least aware about on which action such unexpected alert Is generated. Sometimes, They are generated during software web application page load and sometime they are generated when you perform some action. So first of all we have to note down the action where such unexpected alert Is generated and then we can check for alert after performing that action. We need to use try catch block for checking such unexpected alters because If we will use direct code(without try catch) to accept or dismiss alert and If alert not appears then our test case will fail. try catch can handle both situations.

I have one example where alert Is displaying when loading software web page. So we can check for alert Inside try catch block after page load as shown In bellow given example. After loading page, It will check for alert. If alert Is there on the page then It will dismiss It else It will go to catch block and print message as shown In bellow given example.

Run bellow given example In eclipse with testng and observe the result.
package Testng_Pack;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class unexpected_alert {
 WebDriver driver = null;

 @BeforeTest
 public void setup() throws Exception {
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/06/alert_6.html");
 }

 @AfterTest
 public void tearDown() throws Exception {
  driver.quit();
 }

 @Test
 public void Text() throws InterruptedException {
  //To handle unexpected alert on page load.
  try{   
   driver.switchTo().alert().dismiss();  
  }catch(Exception e){ 
   System.out.println("unexpected alert not present");   
  }
  
  driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("fname");
  driver.findElement(By.xpath("//input[@name='lname']")).sendKeys("lname");
  driver.findElement(By.xpath("//input[@type='submit']")).click();
 }
}

Above given webdriver code Is just for example. It Is just explaining the way of using try catch block to handle unexpected alert. You can use such try catch block In that area where you are facing unexpected alerts very frequently.

7 comments:

  1. Can u please tell how to handle unexpected popup window as well.
    Thank you

    ReplyDelete
  2. Great! This works for me. Thank u :)

    ReplyDelete
  3. In our mobile application i started some process which is happening in Background mean while my test execution should continue once the background process completes popup comes with cancel or delete option.I dont know at what step of my execution this popup comes?.So how can i handle this popup.Right now i am handling with thread concept but it sometimes not perfect.

    ReplyDelete
  4. This program is not working .. WE have to either put Thread.sleep(6) or explicit or fluent wait before driver.switchTo().alert().dismiss(); , then only the above program works..

    thanks for the share

    ReplyDelete
  5. Here you knows the action that some where you may get the alert, so you can do accept/dismiss. But In over all application, you doesn't anything that when or after which action you may find any popup/alert, how you will handle that type of situation

    ReplyDelete