Pratik Vadwala
Pratik Vadwala

Reputation: 66

Selenium ChromeDriver: Adding chrome extension does not work on Grid

I am trying to run a test on selenium grid using Chrome with ModHeader extension.

The extension loads fine locally with the solution provided by Florent B in chrome modify headers in selenium java, i am able to add extension .crx through script

But I am not able to find out why the extension is not loading on Grid Node. Infact browser does not even open when running the below code.

Can you please help? Code below for your reference.

Regards, Pratik

private static RemoteWebDriver UseSeleniumGrid()
{
    var path =
        Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName +
        "\\Local\\Google\\Chrome\\User Data\\Default\\Extensions\\idgpnmonknjnojddfkpgkljpfnnfcklj\\extension_2_1_1.crx";
    var options = new ChromeOptions();
    options.AddExtension(Path.GetFullPath(path));

    var capability = new DesiredCapabilities();
    capability.SetCapability(ChromeOptions.Capability, options);

    capability.SetCapability("browserName", "chrome");

    return new CorrelatedDriver(
        new Uri("http://localhost:4444/wd/hub"), capability, RemoteTimeout);
}

Upvotes: 1

Views: 1030

Answers (1)

Pratik Vadwala
Pratik Vadwala

Reputation: 66

Manage to find the solution.

The error was in lines:

var capability = new DesiredCapabilities();
capability.SetCapability(ChromeOptions.Capability, options);

According to https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/7043 Chrome options cannot be programmtically set after SeleniumDriver upgrade. The solution is to cast options to desired capability.

Working code below:

private static RemoteWebDriver UseSeleniumGrid()
{
    var path =
        Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName +
        "\\Local\\Google\\Chrome\\User Data\\Default\\Extensions\\idgpnmonknjnojddfkpgkljpfnnfcklj\\extension_2_1_1.crx";
    var options = new ChromeOptions();
    options.AddExtension(Path.GetFullPath(path));

    var capability = options.ToCapabilities() as DesiredCapabilities;

    capability.SetCapability("browserName", "chrome");

    return new CorrelatedDriver(
            new Uri("http://localhost:4444/wd/hub"), capability, RemoteTimeout);
}

Upvotes: 0

Related Questions