Kir
Kir

Reputation: 385

Unable to cast object of type 'OpenQA.Selenium.Remote.RemoteWebElement' to type 'System.Collections.Generic.Di ctionary`2[System.String,System.Object]

I am trying to capture time it takes for various events of my webpage load (to capture performance) using the Web Timing API and Selenium 2 webdrivers and C#

Basically the idea (originally from a dev on the Mozilla team) is from Dean Hume's blog post ... http://deanhume.com/Home/BlogPost/measuring-web-page-performance-with-selenium-2-and-the-web-timings-api/56

I shamelessly copied the extensions class and wrote a couple of methods to get the numbers in the format I want..

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;
    }
}

My method that will give me the time differences ...

        //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"]));

I am hitting this exception when I use InternetExplorerDriver(options). But the same code works for Firefox and chrome drivers.

IE is always a pain in the butt and it continues to prove to be so ** I don't understand how else I can cast what .ExecuteScript(scriptToExecute); returns...?

Appreciate any inputs here..

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

Upvotes: 3

Views: 7851

Answers (1)

tlbignerd
tlbignerd

Reputation: 1114

While increidbly old, I ran across this same problem. I figured out that you can convert the object using toJSON and it returns correctly. The working code is the following:

return timings.toJSON();

The final code is:

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;
    }
}

Upvotes: 1

Related Questions