Skip to main content

_ruby

Example

Run the following command on the terminal:

$ /usr/bin/safaridriver --enable

After that, make sure it's enabled. To do that:

  1. open Safari
  2. go to Develop
  3. click on Allow Remote Automations

Now if we open up an interactive Ruby terminal (e.g., irb) and launch a Selenium instance, here's what we'd see.

> irb
irb(main):001:0> require 'selenium-webdriver'
irb(main):002:0> driver = Selenium::WebDriver.for :safari

A successful communication between Safari and the Selenium Driver extension has occurred.

Now let's wire up a simple test, so we can see that everything works as expect.

# filename: safari.rb

require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers

def setup
@driver = Selenium::WebDriver.for :safari
end

def teardown
@driver.quit
end

def run
setup
yield
teardown
end

run do
@driver.get 'http://the-internet.herokuapp.com'
expect(@driver.title).to eql 'The Internet'
end

Expected Behavior

When you save the file and run it (e.g., ruby safari.rb from the command-line), here is what will happen:

  • Safari opens
  • The home page of the-internet loads
  • The title of the page is checked to make sure it's what we expect
  • Safari closes

Summary

Keep in mind that Safari can load without you realizing it (since it doesn't obtain focus when launching with Selenium). When that happens you'll need to switch to Safari in order to see what the test is doing.

And if you're running Safari on a remote node (or set of nodes), you'll need to install and enable the SafariDriver browser extension on each of them.

Happy Testing!