Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Making WebMock, Selenium, and WebDriver Play Nicely

Danny Burkes
Friday, June 10, 2011

Update

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!

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

4 Comments

  1. Paul Hepworth says:

    Thanks, but wouldn’t it be better to fork the webmock gem and submit a pull request? This is a bug within webmock, right?

    July 30, 2011 at 5:48 pm

  2. Wojtek says:

    Good thinking @Paul. Alas, I believe webmock-disabler only disables NetHTTP client integration. There’s more work to cover them all before the feature could be merged to webmock gem.

    July 31, 2011 at 12:02 pm

  3. Bartosz Blimke says:

    I just released new WebMock 1.7.0 which does both:

    1) Fixes selenium-webdriver compatibility
    2) Adds WebMock.disable! and WebMock.enable! for all adapters, or selectively with :except option. Full changelog is here https://github.com/bblimke/webmock/blob/master/CHANGELOG.md

    August 15, 2011 at 7:15 am

  4. Danny Burkes says:

    Great news, Bartosz- I’ll update the article to reflect this.

    August 15, 2011 at 9:54 pm

Add New Comment Cancel reply

Your email address will not be published.

Danny Burkes

Danny Burkes
San Francisco

Recent Posts

  • Big Data
  • Urban Dictionary: Recent Infrastructure Changes for Rails at Scale
  • Scaling Web Applications with MemCachier
Subscribe to Danny's Feed

Author Topics

activerecord (1)
webmock (1)
agile (3)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >