单击网站上的每个链接和第一级子链接时,出现未处理的弹出窗口 [英] While clicking every link and first level sublink on website, unhandled popup appears

查看:85
本文介绍了单击网站上的每个链接和第一级子链接时,出现未处理的弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个硒测试,单击页面上的所有链接.但是我的弹出窗口关闭代码无法处理弹出窗口,并且测试已终止.

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.

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

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: /

这是我的测试方法:

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

推荐答案

在没有用户名/密码的情况下,您不清楚如何通过身份验证,在这种情况下无法打开页面,下面的代码将使用页面加载超时来取消身份验证

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

这篇关于单击网站上的每个链接和第一级子链接时,出现未处理的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆