如何使用selinium-chromedriver跟踪页面加载时间

时间:2015-12-30 10:31:40

标签: java google-chrome selenium automation selenium-chromedriver

是否可以帮助我使用镀铬驱动程序跟踪页面加载时间 - 我知道selinium不能解决性能时间问题

我希望捕获时间显示在:

  

开发者工具 - >网络 - >加载时间

Snap shot attached exactly what im looking- At bottom it will display load time - i want capture it using chrome driver

3 个答案:

答案 0 :(得分:0)

提供的图片链接中的网页加载时间轴只是您可以通过BrowserMob代理获取的HTTP存档(HAR)数据。

blog post中提供了BrowserMob用法的java实现。

import java.io.FileOutputStream;

import org.browsermob.core.har.Har;
import org.browsermob.proxy.ProxyServer;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class PerfTest {
    public static void main(String[] args) throws Exception {
        String strFilePath = "";

        // start the proxy
        ProxyServer server = new ProxyServer(4444);
        server.start();
        //captures the moouse movements and navigations
        server.setCaptureHeaders(true);
        server.setCaptureContent(true);

        // get the Selenium proxy object
        Proxy proxy = server.seleniumProxy();

        // configure it as a desired capability
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, proxy);

        // start the browser up
        WebDriver driver = new FirefoxDriver(capabilities);

        // create a new HAR with the label "apple.com"
        server.newHar("assertselenium.com");

        driver.get("http://assertselenium.com");

        // get the HAR data
        Har har = server.getHar();
        FileOutputStream fos = new FileOutputStream(strFilePath);
        har.writeTo(fos);
        server.stop();
        driver.quit();
    }
}

从上一行生成的HAR文件中获取页面加载时间,可以在此堆栈溢出question中看到。

public class ParseHarFile {

     public static void main(String[] args) {
         String fileName = "www.google.com.har";

         ReadHarFile(fileName);
    }

    public static void ReadHarFile(String fileName){

         File f = new File("C:\\Users\\Administrator\\Desktop\\test_files\\" + fileName);
         HarFileReader r = new HarFileReader();

         try
         {
             System.out.println("Reading " + fileName);
             HarLog log = r.readHarFile(f);

             // Access all pages elements as an object
              HarPages pages = log.getPages();

              long startTime =   pages.getPages().get(0).getStartedDateTime().getTime();

              System.out.println("page start time: " + startTime);

             // Access all entries elements as an object
             HarEntries entries = log.getEntries();

             List<HarEntry> hentry = entries.getEntries();

             long loadTime = 0;

             int entryIndex = 0;
             //Output "response" code of entries.
             for (HarEntry entry : hentry)
             {
                 System.out.println("entry: " + entryIndex);
                 //Output request type 
                 System.out.println("request code: "+entry.getRequest().getMethod()); 
                 // Output start time
                 System.out.println("start time: "+entry.getStartedDateTime().getTime()); 
                 // Output start time
                 System.out.println("time: " + entry.getTime()); 

                 long entryLoadTime = entry.getStartedDateTime().getTime() 
                                       + entry.getTime();

                 if(entryLoadTime > loadTime){
                     loadTime = entryLoadTime;
                 }

                 System.out.println();
                 entryIndex++;
             }

             long loadTimeSpan = loadTime - startTime;
             System.out.println("loadTimeSpan: " + loadTimeSpan);

             Double webLoadTime = ((double)loadTimeSpan) / 1000;
             double webLoadTimeInSeconds = Math.round(webLoadTime * 100.0) / 100.0; 
             System.out.println("Web Load Time: " + webLoadTimeInSeconds) ;

         }
         catch (JsonParseException e)
         {
             e.printStackTrace();
             System.out.println("Parsing error during test");
         }
         catch (IOException e)
         {
             e.printStackTrace();
             System.out.println("IO exception during test");
         }
     }
}

答案 1 :(得分:0)

参考: -

how to access Network panel on google chrome developer toools with selenium?

虽然您只是想要时间,但请使用以下代码: -

long start = System.currentTimeMillis();

driver.get("Some url");

long finish = System.currentTimeMillis();
long totalTime = finish - start; 
System.out.println("Total Time for page load - "+totalTime); 

答案 2 :(得分:0)

页面加载时间插件可用于chrome,它会显示位置栏旁边的加载时间,并且还提供详细信息。