我正在尝试使用Web Timing API和Selenium 2 webdrivers以及C#
来捕获我的网页加载的各种事件所需的时间(以捕获性能)基本上这个想法(最初来自Mozilla团队的开发者)来自Dean Hume的博客文章...... 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 :(得分:1)
虽然年龄很大,但我遇到了同样的问题。我发现你可以使用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;
}
}