Selenium跟踪网络数据传输

时间:2017-07-06 18:09:38

标签: c# google-chrome selenium

使用chrome中的开发人员工具和网络标签,您可以查看跟踪传输的数据量。

enter image description here

我正试图找到一种使用selenium以编程方式执行此操作的方法。

我尝试在Peformance.Resource条目中添加transferSize(如此处所述:how to access Network panel on google chrome developer toools with selenium?。)

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

但是这个值不准确

这可能吗?

我当前(不准确)的代码总计sizeTransfer数字

 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;

1 个答案:

答案 0 :(得分:1)

由于CORS,您可能会看到不准确的window.performance transferSize值:

  

当CORS生效时,许多定时属性都是&#39;除非服务器的访问策略允许共享这些值,否则返回值为零。这要求服务器提供资源以发送Timing-Allow-Origin HTTP响应头,其值指定允许获取受限时间戳值的原点或原点。

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

Firebug的

HAR Export Trigger可能是您想要考虑的工具。这个仅限Firefox的扩展程序会将所有网络/性能数据导出到.har文件中,您可以同样解析该文件:

...
"content": {
    "size": 33,
    "compression": 0,
    "mimeType": "text/html; charset=utf-8",
    "text": "PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5Lz48L2h0bWw+XG4=",
    "encoding": "base64",
    "comment": ""
}
...