1

I am running selenium-java script to click (nearly) all links on website. The script runs fine, though, when I observe the running chrome browser it happens that: Not all links are opening the correct new webpage, i. e. the six top menu entries are not followed.

I evaluated some methods for waiting on page to load, but there is no difference in chrome observation. And Selenium should wait for the correct pages to load for itself.

Here is my java code AppTest.java:

package de.auticon.website;

import org.openqa.selenium.support.ui.ExpectedConditions;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.Assert.*;
import org.junit.Assert;


import org.openqa.selenium.By;      
import org.openqa.selenium.WebDriver;       
import org.openqa.selenium.chrome.ChromeDriver;     
import java.util.List;      
import java.util.concurrent.TimeUnit;       
//import java.util.concurrent.TimeSpan;     
import org.openqa.selenium.*;       
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedCondition;
//import org.openqa.selenium.IJavaScriptExecutor;
//import org.openqa.selenium.IJavaScriptExecutor.ExecuteScript;
//import org.openqa.selenium.IWebDriver;
//import org.openqa.selenium.support.ui.IWait;

public class AppTest extends TestCase{              

WebDriver driver = new ChromeDriver();


 public void waitForPageLoaded() {
    ExpectedCondition<Boolean> expectation = new
            ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driver) {
                    return ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete");
                }
            };
    try {
        Thread.sleep(1000);
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(expectation);
    } catch (Throwable error) {
        Assert.fail("Timeout waiting for Page Load Request to complete.");
    }
}       

public void test_all_links() {                                  
    String baseUrl = "http://auticon.nepomedia-staging.de/";                    
    System.setProperty("webdriver.chrome.driver","c:\\chromedriver_win32\\chromedriver.exe");                   


    String underConsTitle = "not found";                    
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);                 
        driver.get(baseUrl);                    

    //show DSGVO:
    WebElement we = driver.findElement(By.linkText("Datenschutzerklärung"));

    Actions xAct = new Actions(driver);
    xAct.moveToElement(we);
    xAct.click();
    xAct.perform();     

          if (driver.getTitle().equals(underConsTitle)) {                           
            System.out.println("\"" + we + "\""                             
                    + " is under construction.");           
          } else {          
            System.out.println("\"" + we + "\""                             
                    + " is working.");          
          }     

          //driver.navigate().back();   
          driver.get(baseUrl);      

    //remove DSGVO banner:
    we = driver.findElement(By.linkText("Ja, ich stimme zu"));

    xAct = new Actions(driver);
    xAct.moveToElement(we);
    xAct.click();
    xAct.perform();

    //click_all_links:  
    //List<WebElement> linkElements = driver.findElements(By.tagName("a"));                         
    //String[] linkTexts = new String[linkElements.size()];                         
        //int                   i = 0;                  

        //extract the link texts of each link element       
        //for (WebElement e : linkElements) {                           
        //linkTexts[i] = e.getText();                           
        //i++;          
        //}     

        //test each link        
        //for (String t : linkTexts) {                          



        //if (!t.isEmpty()){

        for (int i=0; true; i++)
        {
            List<WebElement> links = driver.findElements(By.tagName("a"));
            if (i >= links.size())
            break;
        if(!links.get(i).getText().isEmpty())
        {

          //WebDriverWait wait = new WebDriverWait(driver, 10);
          //WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(links.get(i)));


          we = links.get(i);//driver.findElement(By.linkText(t));

          System.out.println("\"" + links.get(i).getText() + "\""   );

          xAct = new Actions(driver);
          xAct.moveToElement(we);
          xAct.click();
          xAct.perform();

          //IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));

          //wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

          waitForPageLoaded(); 

          if (driver.getTitle().equals(underConsTitle)) {                           

                System.out.println(    " is under construction.");          
          } else {          
            //System.out.println("\"" + links.get(i).getText() + "\""                               
                System.out.println( " is working.");            
          }     
          //driver.navigate().back();   
          driver.get(baseUrl);
        }           
    }       
        driver.quit();          
}       
}
Leder
  • 396
  • 1
  • 5
  • 21

1 Answers1

1

You can get all links at once and then check them as you with:

driver.get("http://auticon.nepomedia-staging.de/");

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

links.forEach(link->{
    driver.get(link);
    //check here
});
Sers
  • 12,047
  • 2
  • 12
  • 31
  • Yes, that was my first attempt: But after visiting the first link the other links got obsolete and there was an error, the name I do not remember. Now I did the workaround with the `links.size()` and `break;` checks. – Leder Oct 29 '18 at 12:13
  • Hello @sers, one more question: I have a site with links `href` that do not start with `http`. How can I reach these local links? – Leder Mar 13 '19 at 08:54
  • Can share link without href – Sers Mar 13 '19 at 09:01
  • I have this code: `String base_url = "https://infinite-taiga-25466.herokuapp.com"; driver.get(base_url); //get all links with href that start with / ArrayList links = (ArrayList) ((JavascriptExecutor) driver).executeScript("return [...document.querySelectorAll(\"a[href^='/']\")].map(e=>e.getAttribute('href'))");` ...but always run into a popup which I cannot handle with my close code! – Leder Mar 13 '19 at 10:46
  • Please ask new question for this – Sers Mar 13 '19 at 12:28
  • OK, I have written new question here: (click all links and handle popup)[https://stackoverflow.com/questions/55164992/while-clicking-every-link-and-first-level-sublink-on-website-unhandled-popup-ap] – Leder Mar 14 '19 at 14:36