【问题标题】:How to get rid of an overlaying pop up如何摆脱重叠弹出窗口
【发布时间】:2021-12-28 18:42:43
【问题描述】:

目前正在 Lord & Taylor 在线网站上练习运行 TestNG 测试,我遇到了一个问题,即在会话期间,Lord 和 Taylor 的叠加弹出窗口至少在屏幕上弹出一次。大多数时候它出现在主页上,但一旦我点击其中一个部分,它也会出现。我曾尝试使用 xpath,但每次运行浏览器时它似乎都会改变。我也尝试过使用链接文本,但这似乎也不起作用。我可以使用隐式等待并手动单击它,但我觉得必须有一种方法可以用代码来做到这一点。我还遇到了一个问题,有时广告根本无法加载,因此它允许我在它突然弹出之前完成最初的几个测试。它成为我似乎无法克服的障碍。我已经附上了我的代码现在的样子,并且可以真正使用一些帮助来解决这个问题。

    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);
    }
}

【问题讨论】:

    标签: java selenium selenium-webdriver testng


    【解决方案1】:

    我们正在使用QAF 并利用 qaf webdriver 和 webelement listeners 来处理随机弹出窗口。 您可以在元素或驱动程序侦听器的 onfailure 方法中添加重试。理想的示例是需要关闭的意外或随机弹出窗口。实现可能如下所示:

        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 实现可能如下所示:

    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;
    }
    

    更新: 使用 qaf 可以利用 base test classbase 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();
        }
    }
    
    

    您的页面类可能如下所示:

    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("/");
       }
    }
    

    您可能会发现没有创建/拆卸驱动程序的代码。这是通过properties 完成的。

    driver.name=chromeDriver
    
    

    我建议结帐sample project。它具有基本的谷歌搜索测试用例,可用于示例配置。您可以从 github 下载并作为 maven 测试运行。

    【讨论】:

    • 感谢您的回答。我能够找到正确的 jar 文件并将其添加到构建路径中,但我想知道现在如何构建测试用例。我是将它们放在单独的测试中还是将它们放在其他地方?我创建了一个单独的测试并将两个函数都放入其中,但是在导入所有内容后测试无法启动。
    • 参考更新...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 2021-04-03
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多