2012-06-13 69 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; 
    } 
} 
相关问题