Ruby is a widely popular language used extensively for web development. It’s popular due to its simple, expressive nature—however, it’s also extraordinarily powerful. Ruby is extremely well supported with lots of content available online. Moreover, it’s very easy to pick up the basics from online tutorials such as RubyKoans.com.
This article is one in a series showing how to get WebDriver working in various editors and language platforms.
For an overview of how WebDriver works, please see the section “WebDriver Overview” in the blogpost “Getting Started with Webdriver/Selenium for Java in Eclipse” here.
To create and run WebDriver tests in Ruby you’ll need the following components:
Ruby (This article uses 2.4.1 for examples)
A test framework (We’ll use RSpec; there are many you can use)
WebDriver for Ruby
Mozilla’s Gekodriver proxy for Firefox
Like Java, C#, Python, and most all other languages, Ruby’s code files are simply text files. You can create and edit the .rb files in any plain text editor you like. There are a tremendous number of great editors for OSx/*nix, including Vim, Emacs, TextMate, Sublime, and many others.
We’ll stay away from the Editor Wars and just suggest you try several to find one that matches your style and needs!
Installing Ruby can be done many different ways. Somewhat complicating matters is Apple using an older version of Ruby as part of their OSx system. You’ll need a Ruby environment manager to enable you to set up a specific working environment for your own development. Again, there are many different ways to accomplish this.
We’ll use Homebrew, a package manager for OSx, to install rbenv, a Ruby environment manager. Then we’ll get the other bits and pieces wired up.
From a Terminal prompt, use
brew install rbenv ruby-build
Once that’s complete install the specific Ruby environment and make it the default:
rbenv install 2.4.1
rbenv global 2.4.1
Next you’ll need WebDriver and RSpec, the test framework we’ll use.
From the same terminal run
gem install web driver
gem install rspec
As noted in the WebDriver Overview in the first post of this series, you’ll need to have a proxy for your test to talk to the actual browser. This example uses Firefox, so you’ll need to grab the appropriate proxy. Proxies for all WebDriver-supported browsers are listed on the SeleniumHQ’s list of Third Party Drivers. Firefox’s driver is part of Mozilla’s Gecko Driver releases. Make sure to grab the driver that’s appropriate for the version of Windows you’re running (x32, x64).
Download the zip file and extract the driver to a location on your system. You’ll need to add that location to your system’s PATH environment variable.
Creating your first test in Ruby is as simple as opening a new text file. The mechanics of that are specific to the editor you’re using. In Vim you would use something akin to
This would open a new buffer for your test file in the editor.
Below is a complete test case that starts a browser locally, executes a very simple test, then closes out the browser instance. The example is extremely simple and doesn’t follow normal practices like using Page Object Patterns. This is example code, not production code!
@browser = Selenium::WebDriver.for :firefox
header = @browser.find_element(id: 'site-header')
expect(header.displayed?).to be true
Running the test is a matter of simply typing
from the directory where you created the file.
You’ll see Firefox start, navigate to the Sauce Labs home page, and close. Note that you will not see a message that the test has passed&emdash;the extremely simple example doesn’t provide feedback unless something fails. There are a plethora of different approaches for running larger test suites that give more precise results for production use.
In this post you learned a bit about installing Ruby on your OSx system, and writing a very simplistic WebDriver test.
Good luck with your explorations of WebDriver!