【问题标题】:How to track pageload time using selinium- chromedriver如何使用 selenium-chromedriver 跟踪页面加载时间
【发布时间】:2016-04-04 07:41:18
【问题描述】:

你能帮我跟踪使用 chrome 驱动程序的页面加载时间吗?我知道 selinium 不适合增加性能时间

我想将捕获时间显示在:

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

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

【问题讨论】:

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


    【解决方案1】:

    提供的图片链接中的网页加载时间线只不过是您可以通过 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");
             }
         }
    }
    

    【讨论】:

    • 我在运行批处理文件时遇到错误 INFO 12/31 10:02:48 obpMain - 启动 BrowserMob 代理版本 2.0 -beta-6 INFO 12/31 10:02:50 oe​​julog - jetty-7.3.0.v20110203 INFO 12/31 10:02:50 oe​​julog - 启动 oejsServletContextHandler {/,null} WARN 12/31 10:02:50 oe​​julog - 失败 SelectChannelConnector@0.0.0.0 :8080: java .net.BindException:地址已在使用中:绑定
    • WARN 12/31 10:02:50 oe​​julog - org.eclipse.jetty.server.Ser r@15c822d 失败:java.net.BindException:地址已在使用:线程中的绑定异常“ main" java.net.BindException:地址已在使用中:sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Unknown Source) at sun.nio.ch.Net .bind(Unknown Source) at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) at
    • 端口似乎被您正在使用的其他进程使用。这里是 4444。有关更多详细信息,请参阅此帖子 javarevisited.blogspot.in/2011/12/…。可能会继续尝试在 PerfTest 类中将端口更改为 ProxyServer server = new ProxyServer(9090);
    • 另请参阅幻灯片共享链接中的幻灯片 21 slideshare.net/watsonmw/performance-monitoring-in-a-day
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      参考这个:-

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-12
        • 1970-01-01
        • 2012-12-31
        • 1970-01-01
        • 2014-05-11
        • 2014-11-28
        • 2020-08-02
        相关资源
        最近更新 更多