Friday, October 19, 2012

Selenium FirefoxProfile setProxyPreferences is @Deprecated++

I recently updated the Selenium and BrowserMob Proxy versions in WebTestingExplorer. WebTestingExplorer will proxy requests through BrowserMob, primarily to capture the HTTP response code because this is bafflingly but intentionally not supported in the WebDriver API. While you're here, surf over to Google Code and put a star on Selenium issue #141.

After I did this, I watched some tests running in WebTestingExplorer and noticed that certain HTTP form POSTs were not responding as expected. I've always thought that the WebDriver+BrowserMob setup seemed a little fragile (another reason why you should go star Selenium issue #141), so I instantly suspected it, and sure enough, when I pulled out the proxy, the form POSTs worked fine.

I took another look at the code where we set the proxy into the FirefoxProfile, which looked like this:

  @Override
  public WebDriver createWebDriver(WebDriverProxy proxy) throws Exception {
    if (proxy != null) {
      profile.setProxyPreferences(proxy.getSeleniumProxy());
    }
    return new FirefoxDriver(profile);
  }

What you can't see here but I saw in Eclipse was the @Deprecated strikethrough on the call to FirefoxProfile.setProxyPreferences: "This is now handled by the driver itself."

I figured that while I was settling in to debug this code anyway, I would remove the deprecation warning. So I updated to the new style as follows:

  @Override
  public WebDriver createWebDriver(WebDriverProxy proxy) throws Exception {
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability(FirefoxDriver.PROFILE, profile);
    if (shouldUseProxy) {
      capabilities.setCapability(CapabilityType.PROXY, proxy);
    }
    return new FirefoxDriver(capabilities);
  }

I run through my tests again. Now it works! Even though it is generally considered bad manners to break something when you @Deprecate it, I'm quite suspicious that the Selenium folks did that here.

No comments: