3

我正在尝试使用 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(options) 时,我遇到了这个异常。但相同的代码适用于 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
4

1 回答 1

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;
    }
}
于 2016-02-16T17:04:45.260 回答