在chrome浏览器中进行视觉验证时,Selenium为什么不打开单击的链接?

时间:2018-10-29 11:04:16

标签: java selenium google-chrome

我正在运行selenium-java脚本来单击(几乎)网站上的所有链接。但是,当我观察正在运行的chrome浏览器时,脚本运行良好,但发生以下情况:并非所有链接都在打开正确的新网页。 e。六个顶部菜单项未遵循。

我评估了一些等待页面加载的方法,但是chrome观察没有什么区别。 Selenium应该等待正确的页面为其自身加载。

这是我的Java代码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();          
}       
}

1 个答案:

答案 0 :(得分:1)

您可以一次获取所有链接,然后使用以下命令进行检查:

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