0

Using the developer tools and network tab in chrome you are able to view track the quantity of data transfered.

enter image description here

I'm trying to find a way to do this programmatically using selenium.

I have tried adding up the transferSize in the Peformance.Resource entries (as described here: how to access Network panel on google chrome developer toools with selenium?.)

performance.clearResourceTimings();
  //Do Work
window.performance.getEntriesByType('resource');

However this value is not accurate

Is this possible?

My current (inaccurate) code that totals the sizeTransfer figures

 IWebDriver driver;

 /*Configure WebDriver*/

 IJavaScriptExecutor m_jse;
 m_jse = (IJavaScriptExecutor)m_driver;

 object Ents = m_jse.ExecuteScript("return window.performance.getEntriesByType('resource');");
 Type t = Ents.GetType();
 MethodInfo mGetCount = Ents.GetType().GetMethod("get_Count");
 int count = (int)mGetCount.Invoke(Ents, null);

  for(int i = 0; i < count; i++)
  {
      MethodInfo mi = Ents.GetType().GetMethod("get_Item");
      Dictionary<string, object> dict = (Dictionary<string, object>)mi.Invoke(Ents, new object[] { i });
      object Value;
      dict.TryGetValue("transferSize", out Value);
      string sSize = Value.ToString();
      size += Convert.ToDouble(sSize);
  }

  return size;
Tom Martin
  • 1,227
  • 2
  • 10
  • 17

1 Answers1

2

You are probably seeing inaccurate window.performance transferSize values because of CORS:

When CORS is in effect, many of the timing properties' values are returned as zero unless the server's access policy permits these values to be shared. This requires the server providing the resource to send the Timing-Allow-Origin HTTP response header with a value specifying the origin or origins which are allowed to get the restricted timestamp values.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Resource_Timing_API/Using_the_Resource_Timing_API#Coping_with_CORS


HAR Export Trigger by Firebug might be a tool you'd want to consider. This Firefox-only extension will export all network/performance data into a .har file, which you can similarly parse:

...
"content": {
    "size": 33,
    "compression": 0,
    "mimeType": "text/html; charset=utf-8",
    "text": "PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5Lz48L2h0bWw+XG4=",
    "encoding": "base64",
    "comment": ""
}
...
budi
  • 6,351
  • 10
  • 55
  • 80
  • Thanks @budi for the information. So I take it that the network tab in the chrome developer tools work a different way. Do you know of a programmatical way to harness this? – Tom Martin Jul 07 '17 at 08:09
  • Not that I'm aware of, sorry about that. – budi Jul 07 '17 at 15:14
  • @TomMartin The Chrome DevTools Protocol (https://chromedevtools.github.io/devtools-protocol/) for which you can also invoke commands through Selenium (the Chromedriver is basically just a mapping between the Webdriver and Chrome DevTools Protocol), e.g. in Python Selenium using `execute_cdp_cmd`. – phk Sep 27 '18 at 13:45