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();
}
}