Using Watir-WebDriver with Safari (at last!)

Well, it’s official: Watir-WebDriver now supports Safari thanks to the release of SafariDriver.

The downside is the set up is quite lengthy at the moment as it requires a Safari extension (version 5+) and until someone publishes the extension to the online gallery, you’ll have to build it yourself.

Steps to build the extension

  1. First, create and install a Safari extension certificate at Apple. You’ll need to sign up for a (free) Safari developer account, and download your certificate locally.
  2. Now, you’ll need to build the extension. First, you’ll need to check out the selenium source code to do so:
    svn co selenium
  3. Then change into this directory and build the extension
    cd selenium
    ./go safari
  4. Finally, install your extension:
    1. Launch Safari
    2. Ensure develop menu is shown by setting it in Advanced Preferences
    3. Open the Extension Builder (Develop > Show Extension Builder)
    4. Add your new extension from: $SELENIUM_CHECKOUT_LOCATION/build/javascript/safari-driver/SafariDriver.safariextension

Using Safari with Watir-WebDriver

It’s exactly the same as any other browser:

require 'watir-webdriver'
b = :safari

Caveat Emptor

  • browser.execute_script is a little flakey
  • Can’t use local html files as extensions don’t load
  • Not sure how to configure browser options programatically like user agent strings


Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

15 thoughts on “Using Watir-WebDriver with Safari (at last!)”

  1. Thanks Alister. I tried this a couple days ago and fell short because I couldn’t not figure out the cert instructions by myself. I’m much closer now, but I still get a ArgumentError: unknown driver: :safari error. I will probably put this in Stack Overflow.


    1. Make sure you’re on the very latest selenium-webdriver gem. Run ‘bundle update’ if you use bundler.


      1. That helped a lot. I can open a browser now. Unfortunately, I what looks like a security error in the browser: “SafariDriver requesting connection at ws://” where the port seems to change from attempt to attempt.

        Error in IRB:

        Selenium::WebDriver::Error::WebDriverError: Not a valid request: GET /wd HTTP/1.1
        Upgrade: websocket
        Connection: Upgrade
        Origin: safari-extension://org.openqa.selenium-33r9m9y28c
        Sec-WebSocket-Key: NTEWWR9XxqO8Hw7C5b/T7g==
        Sec-WebSocket-Version: 13

        from /Library/Ruby/Gems/1.8/gems/selenium-webdriver-2.21.1/lib/selenium/webdriver/safari/server.rb:123:in `process_handshake’
        from /Library/Ruby/Gems/1.8/gems/selenium-webdriver-2.21.1/lib/selenium/webdriver/safari/server.rb:120:in `process_handshake’
        from /Library/Ruby/Gems/1.8/gems/selenium-webdriver-2.21.1/lib/selenium/webdriver/safari/server.rb:66:in `wait_for_connection’
        from /Library/Ruby/Gems/1.8/gems/selenium-webdriver-2.21.1/lib/selenium/webdriver/safari/bridge.rb:20:in `initialize’
        from /Library/Ruby/Gems/1.8/gems/selenium-webdriver-2.21.1/lib/selenium/webdriver/common/driver.rb:45:in `new’
        from /Library/Ruby/Gems/1.8/gems/selenium-webdriver-2.21.1/lib/selenium/webdriver/common/driver.rb:45:in `for’
        from /Library/Ruby/Gems/1.8/gems/selenium-webdriver-2.21.1/lib/selenium/webdriver.rb:63:in `for’
        from /Library/Ruby/Gems/1.8/gems/watir-webdriver-0.5.3/lib/watir-webdriver/browser.rb:35:in `initialize’
        from (irb):5:in `new’
        from (irb):5


  2. I have a similar issue as the one reported by dmcnulla. The Safari browser is open. However, it hangs up there forever. The message in the browser is “SafariDriver requesting connection at ws://”
    I Have to use ctrl-c to terminate the session. The exception is C/Users/xxxxx/.rvm/gems/ruby-1.9.3-p194/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/safari/server.rb:109:in `accept’: Interrupt


  3. im getting the same error as dmcnulla and jun. Any advice as to where I can find a resolution?


      1. Thanks for the feedback! I’ll raise a bug and post on the Selenium User Group. In the meantime, I guess I have to wait for the Driver to become more stable..


        1. Yes, as this post says, it’s very early days.
          Firefox and Chrome are much, much more stable, and Chrome uses the same rendering engine as Safari (WebKit) so you should get similar rendering.


  4. I figured out how to make it work with Safari.. with help from the Selenium WebDriver community on google groups!

    All you need is this line in the terminal:

    gem install selenium-webdriver –pre

    This solves the issue since the latest release of SafariDriver is backwards incompatible


    1. Hi sambehera.

      Could you please let me know your selenium-webdriver version and what safari extension you installed?

      I tried it several times and several different versions, but it still cannot work.

      Any comments is highly appreciate.


      1. I am getting the same issue (with or without gem install selenium-webdriver –pre) any help would be appreciated.


    2. What Sambehera says is correct, except you dont need the -pre at this point. I basically followed the instructions here and I got the same problem running cucumber tests in Safari, I’d get the browser hitting and cucumber returing an error about EOFError… But Sambehera got it. all i did was two things: 1) gem install selenium-webdriver and 2) add gem ‘selenium-webdriver’ to my env.rb file… then did a bundle update… and it worked.


Comments are closed.