How to get rid of an overlaying pop up

Viewed 48

Currently practicing running TestNG tests on the Lord & Taylor online site and I have an issue where a overlaying pop up from Lord and Taylor pops up on the screen at least once during a session. Most of the time it appears on the Homepage but can also appear once I click on one of the sections. I have tried using the xpath but it seems to change every time I run the browser. I have also tried using link text but that doesnt seem to work either. I can use an Implicit wait and manually click out of it but I feel like there has to be a way to do this with code. I also run into a problem where sometimes the ad wont load at all so it allows me to complete the initial few tests before it pops up out of nowhere. Its become a hurdle for me that I cant seem to get over. Ive attached what my code looks like now and can really use some help figuring this out.

    package com.LT.Tests;

    import java.util.concurrent.TimeUnit;    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.Assert;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;    
    import com.LT.Pages.googlePage;
    import com.LT.Pages.homePage;

    public class homepageTest {
    
        WebDriver driver;
        googlePage gp;
        homePage hp;
    
    @BeforeTest
    public void beforeTest() throws InterruptedException {
                 
        System.setProperty("webdriver.chrome.driver","/Users/nezamseraj/Desktop/WSA/Drivers/chromedriver 5");
        
        driver = new ChromeDriver();
        hp = new homePage(driver);
        gp = new googlePage(driver);
                                                        
        driver.manage().deleteAllCookies();
        Thread.sleep(2000);
        driver.get("https://www.google.com/");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);            
    }
    
    @AfterTest
    public void afterTest() {
        
        driver.quit();          
    }
    
    @Test
    public void gpValidation() {
        
        boolean searchbar = gp.searchBar().isEnabled();
        boolean searchbutton = gp.searchbutton().isEnabled();
        
        Assert.assertTrue(searchbar);
        Assert.assertTrue(searchbutton);            
    }
    
    @Test(priority = 2)
    public void searchLT() {
        
        gp.searchBar().sendKeys("Lord and Taylor");
        gp.searchbutton().click();
        gp.ltLink().click();            
    }
    
    @Test(priority = 3)
    public void dismissAD() {
        
        WebDriverWait wait = new WebDriverWait(driver,30);
        wait.until(ExpectedConditions.elementToBeClickable(By.linkText("DECLINE OFFER")));
        hp.declineOffer().click();
    }
    
    @Test(priority = 3)
    public void hpValidation() {
        
        boolean ws = hp.womenSection().isEnabled();
        boolean ds = hp.designerSection().isEnabled();
        boolean as = hp.accessoriesSection().isEnabled();
                    
        Assert.assertTrue(ws);
        Assert.assertTrue(ds);
        Assert.assertTrue(as);
    }
}
1 Answers

We are using QAF and utilizing qaf webdriver and webelement listeners for to deal with random popups. You can add retry in element or driver listener's onfailure method. Ideal example is unexpected or random popup that needs to be dismissed. The implementation may look like below:

    public void onFailure(QAFExtendedWebElement element, CommandTracker commandTracker) {
        //check and close popup if exist
        boolean shouldRetry = closePopUpIfExist(); 

        if (commandTracker.hasException()
                && commandTracker.getException() instanceof StaleElementReferenceException) {
            element.setId("-1");
            shouldRetry = true;
        }
            commandTracker.setRetry(shouldRetry);
    }

closePopUpIfExist implementation may look like below:

private void closePopUpIfExist(){
 QAFExtendedWebElement popup_declineEle = new  QAFExtendedWebElement("link=DECLINE OFFER");
 if(popup_declineEle.isPresent() && popup_declineEle.isDisplayed()){
  popup_declineEle.click();
  return true;
 }
 return false;
}

UPDATE: With qaf take benefit of base test class and base test page

public class homepageTest extends extends WebDriverTestCase  {
    @Test
    public void gpValidation() {

       GooglePage gp = new GooglePage();
       gp.launchPage(null);
       gp.getSearchTextbox().verifyEnabled();
       gp.searchbutton().verifyEnabled();
       // this test case doesn't makes sense because google is not your AUT and here you are testing google functionality!...
    }

    @Test(priority = 3)
    public void hpValidation() {
        GooglePage gp = new GooglePage();
        gp.launchPage(null);
        gp.getSearchTextbox().sendKeys("Lord and Taylor");
        gp.searchbutton().click();
        gp.ltLink().click();   
        HomePage hp = new HomePage();
        hp.womenSection().verifyEnabled();
        hp.designerSection().verifyEnabled();
        hp.accessoriesSection().verifyEnabled();
    }
}

Your page class may look like below:

public class GooglePage extends WebDriverBaseTestPage<WebDriverTestPage>{
   //declare elements
   // you can use @Findby from selenium support or from qaf
   @FindBy(locator = "name=q")
   private QAFWebElement searchTextbox;
   ...
   protected void openPage(PageLocator loc){
     driver.get("https://www.google.com/");
   }

   public QAFWebElement getSearchTextbox(){
     return searchTextbox;
   }
   ...

}

public class HomePage extends WebDriverBaseTestPage<WebDriverTestPage>{
   //declare elements
   // you can use @Findby from selenium support or from qaf

   protected void openPage(PageLocator loc){
     driver.get("/");
   }
}

You may observe that there is no code to create/teardown driver. That is done through properties.

driver.name=chromeDriver

I would suggest checkout sample project. It has basic google search test case available with example configuration. you can download from github and run as maven test for playing around.

Related