How to programmatically configure Chrome extension through Selenium WebDriver

Jerry picture Jerry · Jan 27, 2016 · Viewed 9.6k times · Source

I need to install and configure an extension in Chrome to modify all request headers during Selenium test execution. I've been able to follow an example from this support article in Saucelabs showing how to do this for Firefox locally, but not sure how to do so for Chrome.

The ChromeDriver documentation for extensions only goes into installing them, not configuring.

Questions

  • Can someone point me to some docs which explain how this can be accomplished or post an example here?
  • How would settings be updated?
  • How to find out what settings properties are available for any given extension?
  • Are there any differences between local and remote execution since that's one of the issues I've encountered with the Firefox method?

Plan is to run this against SauceLabs. Would try to use the ModHeader chrome extension to set the header values needed.

EDIT 1

Tried installing the Chrome version of the MODHeader extension, but running into similar problems. Able to get the extension installed locally, but in remote executions see an error.

private static IWebDriver GetRemoteDriver(string browser)
{

    ChromeOptions options = new ChromeOptions();
    options.AddExtensions("Tools/Chrome_ModHeader_2_0_6.crx");

    DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
    capabilities.SetCapability(ChromeOptions.Capability, options);


    capabilities.SetCapability("name", buildContext);
    capabilities.SetCapability(CapabilityType.BrowserName, "Chrome");
    capabilities.SetCapability(CapabilityType.Version, "");
    capabilities.SetCapability(CapabilityType.Platform, "Windows 10");
    capabilities.SetCapability("screen-resolution", "1280x1024");
    capabilities.SetCapability("username", "SaucelabsUserName");
    capabilities.SetCapability("accessKey", "SaucelabsAccessKey");
    capabilities.SetCapability("build", "BuildNumber");
    capabilities.SetCapability("seleniumVersion", "2.50.1");


    return new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com/wd/hub"), capabilities);
}

Error displayed in SauceLabs logs is

[1.968][INFO]: RESPONSE InitSession unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: Arguments

Answer

Rain9333 picture Rain9333 · May 31, 2016

Since you mention that the problem is mainly on remote and I notice you are using SauceLabs, have you check this article from them?

https://support.saucelabs.com/customer/en/portal/articles/2200902-creating-custom-firefox-profiles-and-chrome-instances-for-your-automated-testing

Installing an Firefox Extension such as Modify Headers(You would need download the .xpi file on your machine first):

DesiredCapabilities caps = new DesiredCapabilities();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File("path\of\Modify Headers xpi file"));
profile.setPreference("general.useragent.override", "UA-STRING");
profile.setPreference("extensions.modify_headers.currentVersion", "0.7.1.1-signed");
profile.setPreference("modifyheaders.headers.count", 1);
profile.setPreference("modifyheaders.headers.action0", "Add");
profile.setPreference("modifyheaders.headers.name0", "X-Forwarded-For");
profile.setPreference("modifyheaders.headers.value0", "161.76.79.1");
profile.setPreference("modifyheaders.headers.enabled0", true);
profile.setPreference("modifyheaders.config.active", true);
profile.setPreference("modifyheaders.config.alwaysOn", true);
profile.setPreference("modifyheaders.config.start", true);
caps.setCapability(FirefoxDriver.PROFILE, profile);

NOTE: If you trying to do the same using C#, you would need to use the ToBase64String() method.