单击网站上的每个 link 和第一级子 link 时,出现未处理的弹出窗口

While clicking every link and first level sublink on website, unhandled popup appears

我编写了一个 selenium 测试,可以点击页面上的所有链接。但是我的弹窗关闭代码没有处理弹窗,测试中止了。

我在使用 Selenium Java V2.53.1,TestNG,后端是 browserstack。

这是调用堆栈,在最后一页之后弹出窗口出现并且没有被关闭!

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
link: /
link2: /
link2: /articles
link: /articles
link2: /

这是我的测试方法:

@Test
public void test_click_all_links() throws Exception {

    String base_url = "https://infinite-taiga-25466.herokuapp.com";     

    driver.get(base_url);

    //get all links with href that start with /
    ArrayList<String> links = (ArrayList) ((JavascriptExecutor) driver).executeScript("return [...document.querySelectorAll(\"a[href^='/']\")].map(e=>e.getAttribute('href'))");

    links.forEach(link->{

            driver.get(base_url + link);
            System.out.println("link: " + link);

        //check here            
            try {
                WebDriverWait wait = new WebDriverWait(driver, 5, 100);
                wait.until(ExpectedConditions.alertIsPresent());
                Alert alert = driver.switchTo().alert();
                // Prints text and closes alert
                //System.out.println(alert.getText());
                alert.dismiss();
            } catch (NoAlertPresentException | TimeoutException ex) {
                //do nothing
            };

        Assert.assertNotEquals(title(), "The page you were looking for doesn't exist.");

        //get all sublinks with href that start with /
        ArrayList<String> sublinks = (ArrayList) ((JavascriptExecutor) driver).executeScript("return [...document.querySelectorAll(\"a[href^='/']\")].map(e=>e.getAttribute('href'))");    
        sublinks.forEach(link2->{    
            driver.get(base_url + link2);
            System.out.println("link2: " + link2);

        //check here
            try {
                WebDriverWait wait = new WebDriverWait(driver, 5, 100);
                wait.until(ExpectedConditions.alertIsPresent());
                Alert alert = driver.switchTo().alert();
                // Prints text and closes alert
                //System.out.println(alert.getText());
                alert.dismiss();
            } catch (NoAlertPresentException | TimeoutException ex) {
                //do nothing
            };      
            Assert.assertNotEquals(title(), "The page you were looking for doesn't exist.");
        });
    });
}

没有清楚地了解您将如何在没有 username/password 的情况下通过身份验证,在这种情况下页面将无法打开,下面的代码将使用页面加载超时取消身份验证。

如何使用基本身份验证,您可以找到

private WebDriver driver;
    private WebDriverWait wait;
    private JavascriptExecutor js;

    private String baseUrl = "https://infinite-taiga-25466.herokuapp.com";

    @BeforeMethod
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, 5, 100);
        js = (JavascriptExecutor) driver;
    }

    public void closeAlert() {
        try {
            wait.until(ExpectedConditions.alertIsPresent());
            Alert alert = driver.switchTo().alert();
            alert.dismiss();
        } catch (NoAlertPresentException | TimeoutException ignored) { }
    }

    @SuppressWarnings("unchecked")
    public ArrayList<String> getLinks() {
        return (ArrayList<String>) js.
                executeScript("return [...document.querySelectorAll(\"a[href^='/']:not([href='/'])\")].map(e=>e.getAttribute('href'))");
    }

    @Test
    public void clickAllLinks() {

        driver.get(baseUrl);

        ArrayList<String> links = getLinks();

        links.forEach(link -> {
            System.out.println("link: " + link);

            driver.get(baseUrl + link);

            closeAlert();

            Assert.assertNotEquals(driver.getTitle(), "The page you were looking for doesn't exist.");

            ArrayList<String> subLinks = getLinks();

            subLinks.forEach(link2 -> {
                System.out.println("link2: " + link2);

                try {
                    driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
                    driver.get(baseUrl + link2);
                } catch (Exception ignore) {
                    System.out.println("Cancel authorization popup");
                } finally {
                    driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
                }

                // On page loading timeout, authentication closed automatically.
                // No need
                //closeAlert();

                Assert.assertNotEquals(driver.getTitle(), "The page you were looking for doesn't exist.");
            });
        });
    }