2012-06-13 104 views
3

我想捕獲各種各樣所需的時間我的網頁加載的事件使用Web定時API和硒2個webdrivers和C#無法轉換'OpenQA.Selenium.Remote.RemoteWebElement'類型的對象來鍵入'System.Collections.Generic.Di ctionary`2 [System.String,System.Object]

基本上(從Mozilla的團隊開發原)的想法是從院長休謨的博客文章(捕捉性能)... http://deanhume.com/Home/BlogPost/measuring-web-page-performance-with-selenium-2-and-the-web-timings-api/56

我無恥地複製了擴展類,並寫了幾個方法來獲取我想要的格式的數字..

public static class Extensions 
{ 
    public static Dictionary<string, object> WebTimings(this IWebDriver driver) 
    { 
     const string scriptToExecute = 
      "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var timings = performance.timing || {}; return timings;"; 

     var webTiming = (Dictionary<string, object>)((IJavaScriptExecutor)driver) 
      .ExecuteScript(scriptToExecute); 

     return webTiming; 
    } 
} 

我的方法,這將使我的時間差......

 //InternetExplorerOptions options = new InternetExplorerOptions(); 
     //options.IntroduceInstabilityByIgnoringProtectedModeSettings = true; 
     //options.UseInternalServer = true; 
     //IWebDriver _driver = new InternetExplorerDriver(options); 

     IWebDriver _driver = new FirefoxDriver(); 

     //IWebDriver _driver = new ChromeDriver(); 

     Program p = new Program(); 

     _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30)); 
     _driver.Navigate().GoToUrl("http://www.google.com"); 

     Dictionary<string, object> webTimings = _driver.WebTimings(); 
     Dictionary<string, Int64> timeinSec = new Dictionary<string, Int64>(); 

     timeinSec.Add("ConnectTime", p.GetTimeDiff(webTimings["connectEnd"], webTimings["connectStart"])); 

我打這個異常,當我使用InternetExplorerDriver(選項)。但是相同的代碼適用於Firefox和Chrome驅動程序。

IE始終是一個痛苦的屁股,它繼續證明是如此**我不明白我怎麼能鑄造什麼.ExecuteScript(scriptToExecute);返回...?

欣賞任何輸入這裏..

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 
'OpenQA.Selenium.Remote.RemoteWebElement' to type 'System.Collections.Generic.Di 
ctionary`2[System.String,System.Object]'. 
    at Selenium.Examples.Performance.Extensions.WebTimings(IWebDriver driver) in 
C:\Users\......\Extensions.cs:line 13 
    at PerfCaptureSample.Program.Main(String[] args) in C:\.........\Program.cs:line 52 
+0

不要投的對象字典。把它分配給一個變量,然後檢查這個變量的值是多少 –

+0

我很抱歉,我沒有做足夠的調查,我的回答是垃圾,所以我刪除了它。我試圖在Java中執行相同的腳本,並面臨同樣的問題,即IE沒有返回它應該返回的內容。 – JacekM

+0

任何其他輸入? – Adi

回答

1

雖然increidbly老了,我碰到同樣的問題跑。我發現你可以使用toJSON來轉換對象,並且它可以正確返回。工作代碼如下:

return timings.toJSON(); 

最後的代碼是:

public static class Extensions 
{ 
    public static Dictionary<string, object> WebTimings(this IWebDriver driver) 
    { 
     const string scriptToExecute = 
      "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var timings = performance.timing || {}; return timings.toJSON();"; 

     var webTiming = (Dictionary<string, object>)((IJavaScriptExecutor)driver) 
     .ExecuteScript(scriptToExecute); 

     return webTiming; 
    } 
} 
相關問題