1

Can you please help me track the pageload time using chrome driver - I know selinium is not prefferable to tack performace time

I want capture time dispalyed in :

Developer tools ->Network ->load time

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

Alicia
  • 1,152
  • 1
  • 23
  • 41
ashok
  • 67
  • 1
  • 1
  • 6

3 Answers3

0

The webpage load timeline in the image link provided is nothing but HTTP Archive (HAR) data which you can get through BrowserMob proxy.

A java implementation of BrowserMob usage is given in this blog post.

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

And getting page load time from HAR file generated in the previous line, can be seen explained in this stack overflow 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");
         }
     }
}
Community
  • 1
  • 1
parishodak
  • 4,506
  • 4
  • 34
  • 48
  • I am getting below error while runnin the batch file INFO 12/31 10:02:48 o.b.p.Main - Starting BrowserMob Proxy version 2.0 -beta-6 INFO 12/31 10:02:50 o.e.j.u.log - jetty-7.3.0.v20110203 INFO 12/31 10:02:50 o.e.j.u.log - started o.e.j.s.ServletContextHandler {/,null} WARN 12/31 10:02:50 o.e.j.u.log - FAILED SelectChannelConnector@0.0.0.0 :8080: java.net.BindException: Address already in use: bind – ashok Dec 31 '15 at 10:03
  • WARN 12/31 10:02:50 o.e.j.u.log - FAILED org.eclipse.jetty.server.Ser r@15c822d: java.net.BindException: Address already in use: bind Exception in thread "main" java.net.BindException: Address already in use: bin at 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 – ashok Dec 31 '15 at 10:06
  • port seems to be used by some other process you are using. here its 4444. see this post http://javarevisited.blogspot.in/2011/12/address-already-use-jvm-bind-exception.html for more details. may be keep trying changing the port as `ProxyServer server = new ProxyServer(9090);` in PerfTest class. – parishodak Dec 31 '15 at 10:24
  • also refer slide 21 in slideshare link http://www.slideshare.net/watsonmw/performance-monitoring-in-a-day – parishodak Dec 31 '15 at 10:27
0

Refer this:-

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

While you just want time then use below code:-

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); 
Community
  • 1
  • 1
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
0

Page load time Plugin is available for chrome, it shows the load time next to the location bar and it also provides details.

Karthika
  • 110
  • 12