Skip to content
November 21, 2012 / selenium34

Blocking Third Party Hosts

Often times while testing you’ll want to ensure that you are testing your system in isolation. We find that our tests are often much slower due to third party integrations, it might be a social media provider or it could be a third party analytics provider. These items are outside of your control, and you may wish to avoid their effects while testing.

How can we do this? Pull in the browsermob-proxy project into your tests (we’d love to hear your comments on how this can be achieved with other language bindings). With this addition you now have easy ways to ensure your third party providers will not interfere with your testing. You might wish to run these tests with and without the proxy enabled to get an idea of how your end-users will perceive the performance of your site. With all of this being said, it is worth keeping these artifacts somewhere, and running eventually deriving some statistics from your data sets.

So how do we do this? First we need to setup our proxy server, this is as simple as adding the proxy to your class path, and some setup code. Remember we are using TestNG in this case below.

public class ExampleProxyTest {
  private ProxyServer server;
  private WebDriver driver;

  public void setup() throws Exception {
    server = new ProxyServer(9000); //Any high numbered port should work fine
    for (final String s : getHostsToBlackList()) { 
      server.blacklistRequests(s, 200); //Send a 200 when the host matches
    final DesiredCapabilities dc = new DesiredCapabilities();
    dc.setCapability(CapabilityType.PROXY, server.seleniumProxy());
    driver = new FirefoxDriver(dc);

  private List<String> hostsToBlackList() {
    final List<String> hosts = new ArrayList<String>();
    return hosts;

As you can see we have blocked out a number of Social Media sites. This list should be modified for your needs obviously. However, now it’s as simple as opening your site, and noticing the speed increases.

public void testThirdPartyBlocks() {
  try { 
    server.getHar().writeTo(new File("example.har"));
  } catch (IOException ioe) { }

That’s it. You should see a nice increase in testing speed now, you could also test to see how your application works / looks when your JavaScript fails to load, or a myriad of other things. Don’t forget to close the driver, and shut down the proxy:

public void shutdown() {

If you hadn’t seen our post on changing browsers without changing code, it’s worth noting that we did not show you that we also have the proxy in a profile. If that profile is turned on than the driver we return has the proxy enabled. It will be nice once the cloud providers can allow us to use this functionality on their sites.

Until next time, enjoy the Thanksgiving holiday if you’re celebrating it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: