代码之家  ›  专栏  ›  技术社区  ›  Leder

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

  •  1
  • Leder  · 技术社区  · 8 年前

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

    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 回复  |  直到 8 年前
        1
  •  1
  •   Sers    8 年前

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