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

巧了我就是萌 提交于 2020-01-06 08:07:56

问题


I have written a selenium test that clicks all links on a page. But my popup close code does not handle the popup and the test is discontinued.

I am on Selenium Java V2.53.1, TestNG and backend is browserstack.

This is the call stack, after the last page the popup appears and is not dismissed!

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

This is my test method:

@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.");
        });
    });
}

回答1:


Without no clear understand how you're going pass authentication without username/password, page will not open in this case, code below will cancel authentication using page load timeout.

How to use basic authentication you can find 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.");
            });
        });
    }


来源:https://stackoverflow.com/questions/55164992/while-clicking-every-link-and-first-level-sublink-on-website-unhandled-popup-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!