java selenium webdriver driver.get(url) 打开前需要 9000 毫秒 url

java selenium webdriver driver.get(url) takes 9000ms before opening url

看这段代码。我正在做硒测试。我必须打开一个 url,其中从 CSV 文件中检索字符串 url。到达页面后,我通过两种不同的方法检查标题和面包屑是否正确,这些检查工作正常:意味着 url 始终正确打开。

但是打开 urls selenium 需要 90 秒。 为什么? 我知道问题出在 "open(path)" 方法中(已通过调试模式确认)。 在单元测试中没有问题(所有 url 都很快打开)但是在 hudson 上它需要很多时间。

注意:我的服务器快速响应浏览器请求。

private void checkPages(Locale locale) {
    for (String page : pages.keySet()) {
        Map<String, String> attributes = pages.get(page);
        String url = attributes.get(CsvFileReader.URL);
        System.out.println("  Check page: "+url); //TODO Remove         
        if (attributes.get(CsvFileReader.TYPE).equals(
                CsvFileReader.TYPE_POPUP)) {
            // TODO : comment vérifier que la bonne redirection est faite
            // après l'ouverture d'une popup ?
        } else if (attributes.get(CsvFileReader.TYPE).equals(
                CsvFileReader.TYPE_MENU)) {
            // TODO : comment vérifier que la bonne redirection est faite
            // après l'ouverture d'un link to page ?
        } else {
            open(url);
            if (Locale.FRENCH.equals(locale)) {
                checkBreadcrumb(attributes.get(CsvFileReader.FRENCH_NAME));
                checkTitlePage(attributes.get(CsvFileReader.HTML_TITLE_FRENCH));
            } 
            else {
                checkBreadcrumb(attributes.get(CsvFileReader.NAME));
                checkTitlePage(attributes.get(CsvFileReader.HTML_TITLE));                   
            }
        }
    }
}


long t2, t1;
protected void open(String path) {
    String ctx = "Open " + path;
                        t1 = System.currentTimeMillis();        
                        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                        Date date = new Date();
    System.out.println("\n" + "dans open " + dateFormat.format(date));  
    try {
        testContext.put(ctx, STATUS_START);
        driver.get(path);
        //driver.navigate().to(path);
        //driver.navigate().to("http://rns059lv:8080/winportal/group/mssportal/set-upchangenotificationrules");
        System.out.println("logout link is displayed: " + driver.findElement(By.xpath("//*[@id='logoutLink']")).isDisplayed());
        testContext.put(ctx, STATUS_OK);
                        t2 = System.currentTimeMillis() - t1;
                        ctx = ctx + " -- duration = " + t2 + "ms";
                        System.out.println(ctx);        
    } catch (Throwable e) {
        ctx = ctx + e.getMessage();
        testContext.put(ctx, throwableToString(e));
    }
}

private void checkBreadcrumb(String breadcrumbName) {
    String ctx = "";
    t1 = System.currentTimeMillis();        
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    System.out.println("\n" + " dans checkbreadcrumb " + dateFormat.format(date));      
    checkTextPortalPages(breadcrumbName);
    t2 = System.currentTimeMillis() - t1;
     ctx = "check breadcrumb --" + breadcrumbName + "-- duration = " + t2 + "ms";
     System.out.println(ctx);
}

protected void checkTitlePage(String title) {
    String ctx = "Check if title is correct (" + title + ")";
    String ctx1 = "";
    t1 = System.currentTimeMillis();
    try {
        testContext.put(ctx, STATUS_START);
        waitPageLoad();
        if (!driver.getTitle().equals(title)) {
            testContext.put(ctx, STATUS_ERROR);
            testContext.put("Title received = " + driver.getTitle(), STATUS_ERROR);
        } else {
            testContext.put(ctx, STATUS_OK);
        }
    }
    catch (Throwable e) {
        testContext.put(ctx, throwableToString(e));
    }
    t2 = System.currentTimeMillis() - t1;
     ctx1 = "Check title --" + title + "-- duration = " + t2 + "ms";
     System.out.println(ctx1);
}

我终于在这里找到了我的回复: Selenium WebDriver works but SLOW (Java)

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.cache.disk.enable", true);
profile.setPreference("browser.cache.memory.enable", true);
profile.setPreference("browser.cache.offline.enable", true);
profile.setPreference("network.http.use-cache", true);
**profile.setPreference("webdriver.load.strategy", "unstable");** // the most important
this.driver = new FirefoxDriver(profile);