Issue installing extension with Selenium remote Firefox webdriver in Saucelabs

天涯浪子 提交于 2019-12-10 11:08:30

问题


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


回答1:


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://stackoverflow.com/a/14285902/276681
https://code.google.com/p/selenium/issues/detail?id=2696#c4



来源:https://stackoverflow.com/questions/35027258/issue-installing-extension-with-selenium-remote-firefox-webdriver-in-saucelabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!