2017-07-06 75 views
0

使用Chrome中的開發人員工具和網絡選項卡,您可以查看跟蹤傳輸的數據量。硒軌道網絡數據傳輸

enter image description here

我試圖找到一種方式來做到這一點編程方式使用硒。

我曾嘗試加起來在Peformance.Resource項transferSize:

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

但是這個值是不準確的

這是可能的(如這裏所描述how to access Network panel on google chrome developer toools with selenium?。)?

我現在的(不準確)代碼合計的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

請張貼到目前爲止,你已經嘗試了代碼。 –

回答

1

你可能會看到因爲CORS不準確window.performancetransferSize值:

當CORS生效,許多的時機屬性「值返回爲零,除非服務器的訪問策略允許共享這些值。這要求服務器提供資源來發送Timing-Allow-Origin HTTP響應頭,其中包含一個值,指定允許獲取受限時間戳值的原點。

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


HAR Export Trigger通過Firebug的可能是你要考慮的一個工具。這個Firefox只擴展將所有的網絡/性能數據導出到文件.har,您同樣可以解析:

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

Thanks @budi獲取信息。所以我認爲chrome開發人員工具中的網絡標籤以不同的方式工作。你知道一個程序化的方式來利用這個嗎? –

+0

不是我所知道的,抱歉。 –