We're hiring!

We're actively seeking developers for our new Detroit location. Learn more

Script Away your Annoyances – Patching a Gem

Automation is good. Performing tasks manually is bad. Performing tasks manually is especially bad when the tasks are annoying. Let’s use a Ruby script to alleviate the pain of an annoying task.

Today’s annoyance: the -foreground flag used by the selenium-webdriver gem’s Firefox launcher. Here’s how I scripted away this annoying problem.

First, the problem: at some point in selenium-webdriver’s history, it started passing the -foreground flag to the Firefox launcher. This means that when your tests are running in the background, Firefox will suddenly pop up and steal your focus. This is highly annoying — after it happens to you more than once, you’ll want to pull your hair out. I do not know or care why the change was made to the gem, but I trust the author’s work and I am confident there was a good reason for it.

The fact of the matter is that the annoyance is there. So here’s my script to take care of it:

module MakeFirefoxLaunchInBackground
  module_function
  def run
    webdriver_path = `bundle show selenium-webdriver`.chomp
    launcher_path = "#{webdriver_path}/lib/selenium/webdriver/firefox/launcher.rb"
    if !File.exist?(launcher_path)
      raise "Couldn't find the Firefox launcher file at [#{launcher_path}]."
    end

    expected_launch_line = '@binary.start_with @profile, @profile_dir, "-foreground"'
    new_launch_line = '@binary.start_with @profile, @profile_dir#, "-foreground"'

    launcher_content = File.read(launcher_path)
    if !launcher_content.match(expected_launch_line)
      raise "Didn't see the expected launcher code [#{expected_launch_line}]"
    end

    new_launcher_content = launcher_content.gsub(expected_launch_line, new_launch_line)
    File.open(launcher_path, 'w') do |f|
      f.print new_launcher_content
    end
    puts 'done hacking selenium-webdriver Firefox launcher file'
  end
end

MakeFirefoxLaunchInBackground.run

The script is straightforward.

  • On lines 4-8, I use bundle to locate the file I need to patch in the gem. I also do a quick check to ensure the file I expect to be there is, in fact, there.
  • On lines 10-16, I read in the contents of the file and perform another sanity check. I want an early warning if the gem has changed and I can’t patch it the way I expect.
  • On lines 18-21, I patch up the line and rewrite the file. In my case, I comment out the -foreground flag.
  • Lastly, on line 22, I print a message to remind me of how handy this script is.

There. Done — a small script I can run with ruby background_firefox.rb. Now whenever my selenium-webdriver gem is reinstalled, I can script away the annoyance. No more need to be bothered by Firefox popping up and stealing focus. No more need to go manually patch the file myself.

Hooray for automation!
 

Matt Fletcher (73 Posts)

Matt is a software product developer and designer with Atomic Object.

This entry was posted in Web Apps and tagged , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

One Comment

  1. Posted February 12, 2013 at 2:13 am

    I believe you can override the actual firefox executable too with an environment variable or something along those lines. With that, you could just write your own script that strips out that flag and passes all others to the real firefox… one time fix.