0

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.");
        });
    });
}
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
Leder
  • 396
  • 1
  • 5
  • 21
  • Alert is authorization, you can not dismiss. Do you have username/password? – Sers Mar 15 '19 at 08:40
  • OK, but the authorization has a cancel button. I am not sure if I want to authorize in a cloud service. I just want to click all publicly available links. – Leder Mar 16 '19 at 01:42
  • If you cancel authorization page will not open. – Sers Mar 16 '19 at 08:46
  • So to consider click was successful you need to open the linked page? Not just click a link? In that case you need the credetials and pass the auth. – pburgr Mar 20 '19 at 12:33
  • No, I will not enter credentials in cloud service. For me it is enough for clicking all the links and no error page is displayed. This is what I have implemented. – Leder Mar 20 '19 at 23:11
  • I will click the cancel button manually, as @sers as suggested. – Leder Mar 20 '19 at 23:13
  • @Leder I didn't suggest to cancel manually, it will not be automation then. See my answer if it solve your issue – Sers Mar 21 '19 at 07:45
  • Yes, that's a good idea. I was trying to say, I will press the cancel button by its ID, not manually. – Leder Mar 23 '19 at 06:39

1 Answers1

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.");
            });
        });
    }
Sers
  • 12,047
  • 2
  • 12
  • 31
  • Good job! I think I can accept the answer, because it is working locally. Unfortunately, when I test on cloud service browserstack.com, by removing instantiation of variable driver, I get: - closing of first alert window - blocking at the second alert window – Leder Mar 25 '19 at 10:08
  • Update: Sorry, previous comment is wrong: browserstack.com is working, too, but very slow at the timeout! – Leder Mar 25 '19 at 10:14