Search code examples
c#selenium-webdriverselenium-chromedriverselenium-edgedriver

Why does ExecuteCdpCommand not work with WebDriver


We have a regression suite which we run on Chrome with code created in Visual Studio/C# and Selenium.

Part of the startup code is shown below

protected static ChromeDriver? Driver { get; set; } = null;
Driver = new ChromeDriver(service, options, TimeSpan.FromSeconds(MaxWait));
Driver.ExecuteCdpCommand(
  "Emulation.setTimezoneOverride",
  new Dictionary<string, object>
  {
     ["timezoneId"] = "Europe/London"
  });

The ExecuteCdpCommand is to ensure certain test run in UK time when executing with an azure pipeline and this works very well. But now we are going to running the overnight regression on Chrome one night then Edge and then Chrome again etc etc and everything is running apart from the ExecuteCdpCommand part.

So now we have

protected static WebDriver? Driver { get; set; } = null;
Driver = new ChromeDriver(service, options, TimeSpan.FromSeconds(MaxWait));
or (depending on day of week)
Driver = new EdgeDriver(service, options, TimeSpan.FromSeconds(MaxWait));

And this returns the following compiler error

'WebDriver' does not contain a definition for 'ExecuteCdpCommand' and no accessible extension method 'ExecuteCdpCommand' accepting a first argument of type 'WebDriver' could be found (are you missing a using directive or an assembly reference?)

enter image description here

Which is rather annoying because apart from this issue we are good to go. Anyone got a solution for this little issue please.


Solution

  • The ExecuteCdpCommand method appears to be defined on the concrete child classes: ChromeDriver and EdgeDriver. Once you cast the driver object up to the abstract WebDriver class, that method is no longer available. This is where a little copy-and-paste is forgivable. You will need to initialize both drivers as their concrete types and return the drivers fully initialized before assigning it to the protected static WebDriver? Driver property. It can be as simple as two static methods, one for Chrome and another for Edge:

    private static WebDriver CreateChromeDriver()
    {
        var service = // ...
        var options = // ...
        var driver = new ChromeDriver(service, options, TimeSpan.FromSeconds(MaxWait));
    
        driver.ExecuteCdpCommand("Emulation.setTimezoneOverride",
            new Dictionary<string, object>
            {
                ["timezoneId"] = "Europe/London"
            });
    
        return driver;
    }
    
    private static WebDriver CreateEdgeDriver()
    {
        var service = // ...
        var options = // ...
        var driver = new EdgeDriver(service, options, TimeSpan.FromSeconds(MaxWait));
    
        driver.ExecuteCdpCommand("Emulation.setTimezoneOverride",
            new Dictionary<string, object>
            {
                ["timezoneId"] = "Europe/London"
            });
    
        return driver;
    }
    

    Now, depending on the conditions you decide, call CreateChromeDriver() or CreateEdgeDriver().

    Driver = dependingOnDayOfTheWeek ? CreateChromeDriver() : CreateEdgeDriver();