Issue installing extension with Selenium remote Firefox webdriver in Saucelabs
Asked Answered
G

1

2

Issue
Trying to install a Firefox browser extension during remote execution of Selenium tests on Saucelabs. When executing the tests locally, the extension is installed and active in Firefox, but in remote execution on Saucelabs the extension does not appear in the list of installed extensions. Following the steps outlined in this Saucelabs support article.

Setup
Selenium.Support v2.48.2 or v2.49.0
Selenium.WebDriver v2.48.2 or v2.49.0
Windows 10 or 7
Firefox 43

C# test setup

private static FirefoxProfile CreateFirefoxProfile()
    {
        FirefoxProfile profile = new FirefoxProfile();
        profile.AddExtension("Tools/modify_headers-0.7.1.1-fx.xpi");
        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", "SampleHeader");
        profile.SetPreference("modifyheaders.headers.value0", "test1234");
        profile.SetPreference("modifyheaders.headers.enabled0", true);
        profile.SetPreference("modifyheaders.config.active", true);
        profile.SetPreference("modifyheaders.config.alwaysOn", true);
        profile.SetPreference("modifyheaders.config.start", true);

        return profile;
    }

private static IWebDriver GetRemoteDriver()
    {
        var capabilities = new DesiredCapabilities();

        var profile = CreateFirefoxProfile();

        capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile);

        capabilities.SetCapability("name", buildContext);
        capabilities.SetCapability(CapabilityType.BrowserName,"firefox");
        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");

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

Firefox settings
When looking at about:support in Firefox during local execution and opening the user.js file, it includes the following extension setup which matches the web driver configuration. Inspecting user.js on the Saucelabs remote instance does not include that. Here's a paste bin of the contents of the remote user.js file.

user_pref("general.useragent.override", "UA-STRING");
user_pref("extensions.modify_headers.currentVersion", "0.7.1.1-signed");
user_pref("modifyheaders.headers.count", 1);
user_pref("modifyheaders.headers.action0", "Add");
user_pref("modifyheaders.headers.name0", "SampleHeader");
user_pref("modifyheaders.headers.value0", "test1234");
user_pref("modifyheaders.headers.enabled0", true);
user_pref("modifyheaders.config.active", true);
user_pref("modifyheaders.config.alwaysOn", true);
user_pref("modifyheaders.config.start", true);

I've also tried referencing an external version of the xpi with same result. https://addons.mozilla.org/firefox/downloads/latest/967/addon-967-latest.xpi

Grote answered 27/1, 2016 at 1:28 Comment(0)
G
2

Posted a bug report to SeleniumHQ and received this response, which fixed the above code.

In the RemoteWebDriver case for .NET, you need to use the ToBase64String() method. This should resolve the issue. Note that this is one of the reasons that other drivers have type-safe options classes instead of passing raw capabilities. Future versions of the .NET bindings should extend this pattern to Firefox as well, removing this as an issue in the future.

The GetRemoteDriver method from above should be updated to this.

private static IWebDriver GetRemoteDriver()
{
    var capabilities = new DesiredCapabilities();

    var profile = CreateFirefoxProfile();

    // Note the change here, calling .ToBase64String()
    capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());

    capabilities.SetCapability("name", buildContext);
    capabilities.SetCapability(CapabilityType.BrowserName,"firefox");
    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");

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

After seeing what the fix is, I was able to find additional resources that mentioned this change.

https://mcmap.net/q/832171/-selenium-grid-2-remote-webdriver-not-setting-the-user-agent-preference-in-firefox
https://code.google.com/p/selenium/issues/detail?id=2696#c4

Grote answered 3/2, 2016 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.