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:
The script is straightforward.
- On lines 4-8, I use
bundleto 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
- 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!