ericmckean / chromedriver

Automatically exported from code.google.com/p/chromedriver
0 stars 0 forks source link

access window.performance.timing #900

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I am trying to measure the performance of my website based on 
window.performance.timing object 
(http://www.w3.org/TR/navigation-timing/#processing-model#processing-model).

(I use node so first i run 'npm install selenium-webdriver')

my first attempt was this:
'use strict';

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();

driver.get('http://cnn.com');

getMetric('loadEventEnd', function (err, value) {
    console.log(value);
});

function getMetric(name, cb) {
  driver.executeScript('return window.performance.timing.' + name).then(function(value) {
    cb(null, value);
  });
}

driver.quit();

but loadEventEnd is 0 most of the times I run it.

my second attempt is based on the advice i got on #selenium:

var webdriver = require('selenium-webdriver');

// tells the chromedriver to capture performance logs from the browser
var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome().
       set('loggingPrefs', {
         'browser': 'ALL',
         'performance': 'INFO'})).
   build();

driver.get('http://localhost:3000');

driver.manage().logs().get('browser').then(function(entries) {
  // console.dir(entries);
});

// retrieve the performance logs and does a little processing on them
driver.manage().logs().get('performance').then(function(entries) {
  entries.forEach(function(entry) {
    var message = JSON.parse(entry.message).message
    if (message.method === 'Timeline.eventRecorded') {
      console.log(message.params.record);
    }
  });
});

driver.quit();

This script outputs a lot of stuff but I don't know how to find the different 
keys I need (http://www.w3.org/TR/navigation-timing/#performancetiming).

Selenium version: selenium-webdriver@2.42.1
OS: Ubuntu 14.04
Browser: Chrome
Browser version: 36.0.1985.125

Original issue reported on code.google.com by macmilla...@gmail.com on 19 Aug 2014 at 10:18

GoogleCodeExporter commented 9 years ago
It seems that JSON data in the response string is not properly quoted.

Original comment by barancev on 15 Sep 2014 at 2:51

GoogleCodeExporter commented 9 years ago
I ran in to this issue when trying to fetch the performance object by executing 
Javascript. The loadEventEnd indeed is 0 for most page loads but doesn't seem 
to be a problem with the driver itself. If you debug through the Javascript 
execution (we us Java based tests) loadEventEnd always returns valid values.

Problem appears to be with page load timing. After driver.get(), if we don't 
wait long enough for the page to load, the above Javascript executes before 
loadEndEvent is fired or completed - see this 
http://msdn.microsoft.com/en-us/library/ie/ff974721%28v=vs.85%29.aspx as 
reference.

So the question really is should the driver bear the responsibility of waiting 
for all appropriate events to be completed or the tests should include logic 
for complete page load?

Original comment by shri...@gmail.com on 18 Nov 2014 at 8:39

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Forgot to add, as a validation I added Thread.sleep(2000) in my test to 
explicitly pause after driver.get and loadEndEvent always returned a non-zero 
value.

Original comment by shri...@gmail.com on 18 Nov 2014 at 8:49

GoogleCodeExporter commented 9 years ago

Original comment by samu...@chromium.org on 21 Feb 2015 at 12:18