Making WebMock, Selenium, and WebDriver Play Nicely

June 11, 2011 Danny Burkes


Since this article was written, version 1.7.0 of WebMock has been released, which includes WebMock.disable! functionality, as well as fixes the problem with selenium-webdriver. So please use that, instead of webmock-disabler.

The Problem

Recently, one of my projects ran into a problem where our integration tests would intermittently fail with weird timeout errors and complaints about page elements that couldn’t be found.

A little googling revealed that we weren’t the only ones having this problem.

Apparently, WebMock, Selenium, and WebDriver don’t play nicely together, even if you tell WebMock to allow the outgoing connections necessary to drive the browser.

Some Gemfile hacking revealed that it was the mere presence of WebMock that caused the error. If we removed WebMock from our Gemfile, our integration tests ran fine, but then of course all of our tests that actually needed WebMock failed.

At that point, we could have decided to run our integration tests separately from the rest of the tests, but we really liked the idea of having all the tests run in the same VM, to avoid the duplicate VM startup time.

What we really needed was a way to turn WebMock on and off selectively for different types of tests.

The Solution

With some gnarly monkey patching and offensive use of alias_method, I have created webmock-disabler, a gem which provides new WebMock.disable! and WebMock.enable! methods. You can use these methods in individual tests, or on classes of tests as shown in the README.

The Result

We’re now able to run our tests that need WebMock in the same VM as those that would otherwise break if we didn’t do WebMock.disable!.

Celebration time!

About the Author

Danny Burkes

Danny Burkes is Senior Director of Asia Pacific, and Japan at Pivotal

Android: Unit testing custom views
Android: Unit testing custom views

Android Activity classes can become gigantic and unwieldy if you're not careful. Testing complex Activities...

Android Tidbits 6/9/2011: CD2M Resources
Android Tidbits 6/9/2011: CD2M Resources

C2DM One of our projects just started implementing the Android Cloud to Device Messaging (C2DM) framework....

SpringOne 2021

Register Now