单击网站上的每个链接和第一级子链接时,将显示未处理的弹出窗口

问题描述 投票:0回答:1

我写了一个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.");
        });
    });
}
selenium-webdriver browserstack
1个回答
1
投票

如果没有明确了解如何在没有用户名/密码的情况下通过身份验证,在这种情况下页面将无法打开,下面的代码将使用页面加载超时取消身份验证。

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

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.");
            });
        });
    }
© www.soinside.com 2019 - 2024. All rights reserved.