Getting chromedriver to work with Ruby selenium-webdriver under OSX Mavericks

I interrupt my soprano-saxophone-and-bass-guitar-playing evening to write very quickly about something that annoyed me today. I can’t find the fix anywhere else on the internet, so i guess it’s up to me to provide the solution.

Yesterday i upgraded to OS X Mavericks. Which is nice. Free upgrade, woo! Most things work well, though i had a few Java version issues.

Today i found that selenium-webdriver was not able to launch Chrome with chromedriver. It would open and immediately quit.

A quick google reveals that the workaround is to start chromedriver with the --verbose flag and then it magically works again. Which is true in my experience. But all the examples are for selenium wedriver called by Java or Python and i couldn’t find one for Ruby.

The first thing i did was upgrade selenium-webdriver gem to 2.37.0 and chromedriver to 2.4. I checked my version of Chrome; it is 30.0.1599.101 which is apparently up to date. When none of those things made any difference, I investigated the verbose option.

I thought i would just have to pass --verbose to the Capybara::Selenium::Driver via the args or switches option. But no. That will send the flag to Chrome when it launches, but i need the flag to be set on chromedriver.

Ruby open source to the rescue!

I poked around in the source code for service args and found this:

# selenium/webdriver/chrome/bridge.rb

module Selenium
  module WebDriver
    module Chrome

      class Bridge < Remote::Bridge

        # ...

        def extract_service_args(opts)
          args = []

          if opts.has_key?(:service_log_path)
            args << "--log-path=#{opts.delete(:service_log_path)}"
          end

          args
        end

        # ...

      end
    end
  end
end

Hmm, so the only service arg you can set is the --log-path. That’s no good.

Ruby monkey patching to the rescue!

In my cucumber features folder i made a support file to monkey patch that method, and add a possibility of sending any service args i like.

# features/support/selenium_webdriver_monkey_patch.rb

module Selenium::WebDriver::Chrome
  class Bridge
    def extract_service_args(opts)
      args = []
      args += opts.delete(:service_args) if opts.has_key?(:service_args)
      args << "--log-path=#{opts.delete(:service_log_path)}" if opts.has_key?(:service_log_path)
      args
    end
  end
end

Now in my env.rb file i just have to require the monkey patch file and i’m able to send the verbose flag.

# features/support/env.rb

# ...

require 'selenium-webdriver'
require 'selenium_webdriver_monkey_patch'

# ...

Capybara.register_driver :selenium_chrome do |app|
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    service_args: ["--verbose"])
end

# ...

Hey presto, now i can set the verbose flag on chromedriver, and sure enough, it works. I am testing in Chrome again.

It seems a really horrible hack, and i hope a better solution is found soon, either from a Mavericks update, or chromedriver, or maybe selenium-webdriver will add this option for service args in a future version.

I still don’t really know why verbose works when normal mode does not. But there we go. I can test again tomorrow, and that makes me happy!

Now, back to my bass guitar! :)

Automating Cucumber test scripts on a Mac

I’m really enjoying my work at the BBC on the Olympics team! I’m primarily implementing Cucumber scenarios to verify the work that the developers are doing. I’m also using other tools for stress testing and performance monitoring.

My work here is leading me to use Cucumber in weird and wonderful ways! Here’s an example. Yesterday i was asked to run a script every half an hour that tests the Olympics video player to see if it’s playing successfully, and if not, to email people to say that it’s broken. Not your usual Cucumber scenario, but amazingly enough, it can be used to do that!

Forgive the jargon below. PID is a programme identifier, and IVP is the Interactive Video Player that we are building.

  Scenario: Check LIVE video player
    Given a live PID 
    When I check the IVP 
    Then I should email people if anything goes wrong

I can use a combination of javascript hooks and taking screenshots a few seconds apart and comparing them to tell whether the video is running. I decided to run some step definitions that i’ve already defined and rescue any errors to be emailed. There are a few more steps in here but i’ve stripped it down a bit for simplicity.

When /^I check the IVP$/ do                                                                         
  if @pid
    begin
      step 'I attempt to view the IVP host page with that PID'
      step 'the video should be ready'
      step 'the video should be playing'
    rescue Exception => e
      @error = e.message
    end
  end
end

Notice that there might not actually be a live PID at the moment. It feels very weird, to put conditional logic in Cucumber step definitions like this!

Now comes the email part. I used Pony and sendmail as a simple email sending mechanism.

Then /^I should email people if anything goes wrong$/ do
  if @pid && @error
    subject = "Assurance test failed in #{ENV['ENVIRONMENT']}!"
    body = "#{page.current_url} caused this error: #{@error}"
    recipients = ['send_to@me.co.uk']

    Pony.mail(:to => recipients,
              :subject => subject,
              :body => body,
              :via => :sendmail)
  end
end

So, with that working, i set about putting it on a schedule. I thought i could just call the script from a cron task, but it turns out that wouldn’t work because i need to launch Firefox with Selenium to test the flash player.

I found this cool built-in Mac tool called Automator. You can set up all sorts of tasks into a workflow. I just needed a simple shell script:

And now, we can call that from a cron job every 30 minutes! You might have to hunt around a bit for where it saves the file. For me it’s in my Library/Services.

*/30 * * * * automator /Users/daniea16/Library/Services/IVP\ Test.workflow

Another creative alternative to the cron job is to set up recurring calendar appointments in iCal. You can set an alarm that runs a script. Pretty awesome, hey?! :)

I know Cucumber is meant to be a BDD framework, and was never intended to be used for automated regression testing or assurance testing, but isn’t it cool that with a little bit of imagination you can bend it to fit these alternative uses? :)

OS X: So you think you’re password protected?

To quote Bob Marshall: “Security is always relative, never absolute”

When i started contracting, i thought it would be a good idea to make my macbook require a password on booting up or waking up from screensaver. For weeks i’ve been using it fine coming out of screensaver, but today i rebooted. I couldn’t log in. I think it must be something to do with the colemak keyboard layout. I entered the correct password, in colemak and qwerty, but it was having none of it.

Slightly flustered i turned to my phone and searched for “forgot osx password”. Very quickly i found a few articles on how to restart, hold down Cmd + s to get into single user command line mode, and then mount the filesystem for reading and writing.

Without entering a password, you now have superuser access to the whole system. You can reset people’s passwords. You can view and modify files. You can wipe the whole computer if you want to.

All i’m saying is, if you think an account password will protect you, you’re wrong. It may act as a deterrent, but if someone really wants access to your mac, they coud get it in less than 5 minutes.

It’s not just macs either: How To Reset Admin/Root Password gives easy to follow instructions for FreeBSD, Linux, OS X, Solaris and Windows. Ironically, Windows is the hardest one to crack on this point!

It’s a bit of a wake-up call for me.

Tip of the day

When using a package manager it is helpful to know how to search for available packages. You can search for part of the package name, so that mysql- returns both mysql-server and mysql-client

On Debian-based systems:

sudo apt-cache search mysql-

On Fedora-based systems:

sudo yum list "*mysql-*"

On Mac with MacPorts:

port search mysql-

I'm a free-thinker

I find it odd that Microsoft seem to be dedicating so much TV air time to their “I’m a PC” response advertisments to the Mac vs PC ads of a few years back. The odd thing is, i never remember seeing any of these ads on television. And i would remember because they were done by Mitchell and Webb in the UK. I’ve only seen them on Youtube when browsing for amusement’s sake. I reckon there could be plenty of people who are completely bemused by all these people saying, “I’m a PC” when they have never seen the original ads for reference. Maybe Microsoft will awaken people up to the realisation that actually, Windows is not the only option.

Here’s one of the Mitchell and Webb ads: The Naughty Step.

I actually thought it was illegal to make direct comparisons between your product and a direct competitor’s product in an advertising campaign, so i don’t know how Apple got away with this whole series of advertisements.

Somebody should do an ad called “I’m a free-thinker!” I haven’t sold my soul to Microsoft or to Apple. I don’t owe anybody any money. I just owe a debt of gratitude to thousands of volunteers who willing donated their time, knowledge and expertise to create the world’s best, most secure, most reliable, most flexible, freely available, freely modifiable and freely redistributable operating system.

Actually, IBM did a Linux ad, ages ago. Linux: The Future is Open. Has anyone done anything like that since?

Macs and TextMate

Before i started using TextMate i heard that some people switch to Macs just so that they can use TextMate! I didn’t believe a word of it … but now i am in that exact position! Ruby on Rails is so much easier to program in TextMate, particularly writing and running RSpec tests. Just to be able to press Cmd+Shift+Down to switch to the spec (and create it if necessary) and using Ctrl+r to run the specs, or Ctrl+Shift+R to run a singe spec. Last night i was writing some test specs and it was unbelievably painful without TextMate!

But i have long been a fan of opensource, free software. I don’t want to spend hundreds of pounds on expensive hardware and expensive operating system just so that i can use TextMate! Somebody seriously needs to make a Linux version of TextMate. People would pay for it! I have been using gedit as best i can but it’s the snippets and macros that make TextMate really good.

One of my colleagues uses and recommends jEdit. I’m going to give it a try. Apparently you can run specs quite easily within it, so i reckon it’s got to be worth a go.

Oh yeah, and i can borrow my boss’s old MacBook to use at RailsConf so i’ll look like one of the cool kids! ;)

Work going very well

I feel i have settled in extremely well at my first week of my new job. I love the people, the environment, the work we do … it’s fun and enjoyable, challenging, inspiring, makes me feel worthwhile. Most of the week i have been learning and paring with others, watching what they do, asking questions and making suggestions. Today we switched around and i was doing the typing, with help from one of the others.

They are very opinionated about how to do things – what works and what doesn’t. We follow test-driven development rigidly: before we start any task we write a story about it. We run the story to see that it fails which means we need to write code. But before we do we write specs for the code. We run those to see them fail, then we write just enough code to make it pass. We keep iterating in small steps until we have it working, then we go back to the story which tells us what is to be done next. It sounds long-winded but it really works well. I am embarrassed at how haphazardly i wrote the code for MyChores … but i didn’t know any better! MyChores is going to become so much more reliable, i can tell!

The other great thing is we are using Git and, now that i understand the power of branching, merging and rebasing, i realise what an awesome tool it is to use! It’s so exciting when we’re all working on the same project, Git allows us some very fast simultaneous development … and gitk makes it very exciting to be able to visualise what is going on! :)

Anyway, this is not interesting for the vast majority of my readers, so i will stop now!! Instead i will give you some pictures!

Here is a nice picture i took of the poppy fields near where i work. I love the accidental over-exposure of the sky!

Views of Winchester

This evening, inspired by the iMacs we use at work, i made a Leopard-style wallpaper!

Leopard-style explosion wallpaper

Good, good. That’s all for now. I’m going to go watch someone get evicted from Big Brother! :)

Windoze

Have you ever done a search in Windows, and kept the search window open for a really long time? That ridiculous dog starts going crazier than usual!

Here is the dog, not looking at its best …

Dozy dog

In case anyone is worried i drugged the dog, or forgot to feed it or something, don’t worry! It perked up later on!

Give the dog a bone

I don’t want to be accused of cruelty to pixellated animals, heh!

Today i found out that a 24″ iMac is waiting for me at my new job! WOoooHOooo!! I have been watching videos, boffing up on how Macs work! I have never seriously used one before, but i’m very excited! I’ll be able to honestly compare Windows, Linux and Mac. It also means that Microsoft products will no longer be a part of my life!

This final week seems to be going terribly slowly. I can’t wait to get started at my new job!