3

Web Timing API と Selenium 2 Webdrivers と C# を使用して、Web ページの読み込みのさまざまなイベントにかかる時間を (パフォーマンスをキャプチャするために) キャプチャしようとしています。

基本的に、このアイデア (元は 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 ドライバーでも機能します。

.ExecuteScript(scriptToExecute);IE は常にお尻の痛みであり、そうであることが証明され続けています ** 他に何を返すかをキャストする方法がわかりません...?

ここでの入力に感謝します..

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 に答える