How to handle random pop ups

Issue:

 In some pages unexpected popup's(ex:ad's) will be displaying. Because of this the script will fail by throwing exceptions like ElementNotFoundException and currently Element is not Interactable etc.. . Here these ad's are not specific to particular page. It may come at any page. 

How to handle?

A popup can be an alert or an ad in a iframe or an ad in a new window and all these are treated in different ways.

  1. Assuming its an Alert, log_file has already provided you the solution, i.e. to use alertIsPresent(), and accept or dimiss it as per your requirements.
  2. Assuming the ad is in iframe, follow the below approach:

    driver.switchTo.frame(driver.findElement(By.id(locatorToIdentifyFrame)));       
    driver.findElement(By.id(locatorToCloseAd)).click(); //Close Ad
    driver.switchTo().defaultContent(); // Return to main window
    
  3. Assuming the ad in the window, follow the below approach:

    String mainWinHandle = driver.getWindowHandle(); // Get your main window
    String subWinHandle = null;
    
    Set<String> allHandle = driver.getWindowHandles(); // Fetch all handles
    Iterator<String> iterator = allHandle.iterator();
    while (iterator.hasNext()){
        subWindowHandler = iterator.next();
    }
    driver.switchTo().window(subWindowHandler); // switch to popup 
    
    //Write code to close Ad or skip                                            
    
    driver.switchTo().window(parentWindowHandler);
    

So, you need to use ExpectedConditions in all the three scenarios and based on your need, please put them into a function and call them in your @BeforeTest or @Test (not all but atleast in those scenarios where you suspect an unexepected ad might appear).

Comments

Popular Posts

Linux commands 1 : Commonly used FILE AND DIRECTORY COMMANDS for daily use and interview

Most common used Docker Commands for SDETs

Running tests from maven command line: Different options