Installation

Installation is not complicated, but unfortunately, it is not trivial either.

The tools that we will be using in the book are available on Windows, Mac and Linux, and each of them has several releases currently in use (or in case of Linux, both distributions and releases). Just the most recent releases of each operating system will be covered in the book.

Everything is tested using VirtualBox 4.3.6 virtual machines, except Mac OS, since it does not want to be virtualized. Host OS for VirtualBox is Mac OS X 10.9.1.

You will probably be bored to tears if you read all installation chapters. A lot of stuff is repeated. Read only the chapters you need.

Windows 8.1

You will need internet access if you want to follow examples. All examples are tested with Microsoft Windows 8.1 Pro. All browsers are English (US) version.

Windows 8.1 default desktop

Windows 8.1 default desktop

Ruby

You probably do not have Ruby installed. To make sure, open Command Prompt application and type ruby -v. If you are not familiar with Command Prompt, see Command-line interface chapter.

1 >ruby -v
2 'ruby' is not recognized as an internal or
3 external command, operable program or batch file.

If you get the same thing as I did, you do not have Ruby installed.

Download the latest Ruby 2.0 from rubyinstaller.org/downloads. At the moment it is Ruby 2.0.0-p353. Execute the file.

Ruby Installation

Ruby Installation

You can leave all settings at default values, except at the Installation Destination and Optional Tasks screen check Add Ruby executables to your PATH checkbox. Installation should take you just a few seconds.

Let’s check if Ruby is installed. You will have to open another command prompt, because the one you have opened does not see Ruby.

1 >ruby -v
2 ruby 2.0.0p353 (2013-11-22) [i386-mingw32]

Congratulations! You now have the latest and greatest Ruby!

RubyGems

Software written in Ruby is usually distributed as RubyGems (colloquial name is gem), Ruby package manager. Sometimes Ruby installations do not have the latest versions of RubyGems, so we will first update it. RubyGems is also a gem, (a bit recursive, right?) and we get it’s version with gem -v.

1 >gem -v
2 2.0.14

You should update it with gem update --system:

1 >gem update --system
2 (...)
3 RubyGems system software updated

selenium-webdriver

Let’s try selenium-webdriver gem.

1 >gem install selenium-webdriver --no-ri --no-rdoc
2 (...)
3 Successfully installed selenium-webdriver-2.38.0
4 (...)

Internet Explorer

Internet Explorer 11

Internet Explorer 11

Since Internet Explorer (tested with version 11) is already installed, we will start with it. We will be using IRB (Interactive Ruby Shell). If you are not familiar with it, see IRB chapter.

 1 >irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for
 7 :internet_explorer
 8 Selenium::WebDriver::Error::WebDriverError: Unable
 9 to find standalone executable. Please download the
10 IEDriverServer from
11 http://code.google.com/p/selenium/downloads/list
12 and place the executable on your PATH.
13 (...)

Windows Firewall popup appeared letting me know that it has blocked Ruby.

Windows Firewall has blocked some features of this program

Windows Firewall has blocked some features of this program

For now just close the popup, let’s see how to fix the error message. Follow the instructions from the error message. Go to code.google.com/p/selenium/downloads/list and download 32-bit or 64-bit IEDriverServer. Extract the downloaded zip file (with mouse right click and then Extract All.., for example). Let’s find out what is on the PATH.

1 >PATH
2 PATH=C:\Windows\system32;C:\Windows;
3 C:\Windows\System32\Wbem;
4 C:\Windows\System32\WindowsPowerShell\v1.0\;
5 C:\Ruby200\bin

C:\Ruby200\bin (or where ever you have installed Ruby) looks like a good place to me. Move the IEDriverServer file there.

Let’ try again:

 1 >irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for
 7 :internet_explorer
 8 Started InternetExplorerDriver server (32-bit)
 9 2.38.0.0
10 Listening on port 5555
11 => #<Selenium::WebDriver::Driver:0x5469141e
12 browser=:internet_explorer>
13 
14 > browser.get "http://watir.com"
15 => nil

Firefox

Firefox 26

Firefox 26

If it is not installed (tested with version 25.0.1), download it from mozilla.com/firefox.

 1 >irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :firefox
 7 => #<Selenium::WebDriver::Driver:0xdc8ad4a
 8 browser=:firefox>
 9 
10 > browser.get "http://watir.com"
11 => ""

Chrome

Chrome 31

Chrome 31

If it is not installed (tested with version 31), download it from google.com/chrome.

 1 >irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :chrome
 7 Selenium::WebDriver::Error::WebDriverError: Unable
 8 to find the chromedriver executable. Please
 9 download the server from
10 http://chromedriver.storage.googleapis.com/
11 index.html and place it somewhere on your PATH.
12 More info at
13 http://code.google.com/p/selenium/wiki/
14 ChromeDriver.
15 (...)

You have to download ChromeDriver. Unzip the file and put it in any folder that is in your PATH. If you do not know what is PATH, see Internet Explorer chapter. In short, move the file to Ruby folder, probably C:\Ruby200\bin.

Let’s try again:

 1 >irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :chrome
 7 Starting ChromeDriver (v2.7.236900) on port 9515
 8 => #<Selenium::WebDriver::Driver:0x..fc23f2ebe
 9 browser=:chrome>
10 
11 > browser.get "http://watir.com"
12 => nil

I got Windows Firewall popup again letting me know that it has blocked ChromeDriver. Just close it for now, I have no idea what to do with it.

Chromium

Chromium 34

Chromium 34

First make sure that you can drive Chrome. (See Chrome chapter.) Then download Chromium. (Tested with version 33.) Replace C:\chrome\chrome.exe with path to Chromium executable.

 1 >irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > Selenium::WebDriver::Chrome.path =
 7 'C:\chrome\chrome.exe'
 8 => "C:\\chrome\\chrome.exe"
 9 
10 > browser = Selenium::WebDriver.for :chrome
11 Starting ChromeDriver (v2.7.236900) on port 9515
12 => #<Selenium::WebDriver::Driver:0x..f9d38b82e
13 browser=:chrome>
14 
15 > browser.get "http://watir.com"
16 => nil

PhantomJS

To drive PhantomJS (tested with version 1.9.2) download it, unzip the file and put it in any folder that is in your PATH. If you do not know what is PATH, see Internet Explorer chapter. In short, move phantomjs.exe file to Ruby folder, probably C:\Ruby200\bin.

Let’s try driving it:

 1 >irb
 2 
 3 > browser = Selenium::WebDriver.for :phantomjs
 4 (...)
 5 => #<Selenium::WebDriver::Driver:0x442ed372
 6 browser=:phantomjs>
 7 
 8 > browser.get "http://watir.com"
 9 => {}
10 
11 > browser.save_screenshot "phantomjs.png"
12 => #<File:phantomjs.png (closed)>

The last command saves screenshot of the page. A screenshot from a headless browser. Nice, right?

PhantomJS

PhantomJS

Safari

Safari 5.1.7

Safari 5.1.7

To drive Safari (tested with version 5.1.7), make sure you have it installed.

1 $ irb
2 
3 > require "selenium-webdriver"
4 => true
5 
6 > browser = Selenium::WebDriver.for :safari
7 Selenium::WebDriver::Error::TimeOutError: timed out waiting for Safari to respond
8 ...

Looks like driving Safari on Windows does not work. Safari does open, with this text is displayed on the screen:

1 SafariDriver Client
2 
3 [  0.114s] [safaridriver.client] Requesting connection at ws://127.0.0.1:49349/w\
4 d...
5 [  2.488s] [safaridriver.client] Request acknowledged; connecting...

But, nothing else happens, except the timeout. Well, it is not a big surprise. The current version of Safari for Mac OS is 7.0.2 and the latest supported version for Windows is 5.1.7, so it is not surprise that Selenium has trouble driving it.

Java

To drive Opera, you will have to install Java first. Let’s check if Java is already installed with java -version:

1 >java -version
2 'java' is not recognized as an internal or
3 external command, operable program or batch file.

Looks like we will have to install Java. There is big Free Java Download button at java.com. Execute downloaded file and install Java. Let’s check if Java is really installed with java -version:

1 >java -version
2 java version "1.7.0_45"
3 Java(TM) SE Runtime Environment
4 (build 1.7.0_45-b18)
5 Java HotSpot(TM) Client VM (build 24.45-b08,
6 mixed mode, sharing)

Looks good to me!

Opera

Opera 18

Opera 18

And finally, let’s drive Opera. If you do not have it installed, you can get it at opera.com (tested with version 18.0).

 1 >irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :opera
 7 Selenium::WebDriver::Error::WebDriverError: Unable
 8 to find the Selenium server jar. Please download
 9 the standalone server from
10 http://code.google.com/p/selenium/downloads/list
11 and set the SELENIUM_SERVER_JAR environmental
12 variable to its location. More info at
13 http://code.google.com/p/selenium/wiki/OperaDriver.
14 (...)

Download selenium-server-standalone-2.39.0.jar (or newer version) from Google Code and put it in C:\Ruby200\bin. Then make SELENIUM_SERVER_JAR environmental variable and set it to C:\Ruby200\bin\selenium-server-standalone-2.39.0.jar. (If you have a newer version of selenium-server-standalone file, replace 2.39.0 appropriately.)

If you just want to try driving Opera, typing this into Command Prompt will do the trick:

1 >set SELENIUM_SERVER_JAR=C:\Ruby200\bin\
2 selenium-server-standalone-2.39.0.jar

Let’s try driving Opera again.

 1 >irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :opera
 7 Selenium::WebDriver::Error::UnknownError: Could
 8 not find a platform that supports bundled
 9 launchers, please set it manually
10 Build info: version: '2.39.0', revision: 'ff23eac',
11 time: '2013-12-16 16:11:15'
12 System info: host: 'IE11WIN8_1', ip: '10.0.2.15',
13 os.name: 'Windows 8',
14 os.arch: 'x86', os.version: '6.2', java.version:
15 '1.7.0_45'
16 Driver info: driver.version: OperaDriver
17 (org.openqa.selenium.WebDriverException)

Looks like Selenium can not drive Opera on Windows 8.1.

If I ever manage to drive Opera on Windows, to create a permanent environmental variable, use setx:

1 >setx SELENIUM_SERVER_JAR C:\Ruby200\bin\
2 selenium-server-standalone-2.39.0.jar
3 SUCCESS: Specified value was saved.

Open new command prompt, the old one will not see SELENIUM_SERVER_JAR variable.

Mac OS X 10.9

You will need internet access if you want to follow examples. All examples are tested with Mac OS X 10.9. All browsers are English (US) version.

Mac OS X 10.9 default desktop

Mac OS X 10.9 default desktop

Ruby

Good news is that Ruby is already installed by default. To check Ruby version, open Terminal application any type ruby -v. If you are not familiar with Terminal, see Command-line interface chapter.

You should get this:

1 $ ruby -v
2 ruby 2.0.0p247 (2013-06-27 revision 41674)
3 [universal.x86_64-darwin13]

RubyGems

It is already installed, but an old version. Let’s see which version is here with gem -v.

You should get this:

1 $ gem -v
2 2.0.3

Fortunately, it is easy to upgrade RubyGems with sudo gem update --system:

1 $ sudo gem update --system
2 (...)
3 RubyGems 2.1.11 installed
4 (...)

selenium-webdriver

Let’s try selenium-webdriver gem. Install it with sudo gem install selenium-webdriver --no-ri --no-rdoc.

You will probably get this:

1 $ sudo gem install selenium-webdriver
2 (...)
3 Fetching: ffi-1.9.3.gem (100%)
4 Building native extensions.  This could take a
5 while...
6 ERROR:  Error installing selenium-webdriver:
7 ERROR: Failed to build gem native extension.
8 (...)

Fortunately, it is easy to fix. Install command line developer tools. To install them, type this into Terminal.

1 $ git --version

A popup will appear asking if you would like to install command line developer tools.

Install command line developer tools popup

Install command line developer tools popup

Click Install. After the installation is finished, try again:

1 $ sudo gem install selenium-webdriver --no-ri
2 --no-rdoc
3 (...)
4 Successfully installed selenium-webdriver-2.37.0
5 (...)

Safari

Safari 7

Safari 7

Since Safari (tested with version 7.0) is already installed, all you have to do is to open IRB and type this. If you are not familiar with IRB, see IRB chapter.

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :safari
 7 => #<Selenium::WebDriver::Driver:0x..
 8 f93d5546968bec45e browser=:safari>
 9 
10 > browser.get "http://watir.com"
11 => nil

Firefox

Firefox 26

Firefox 26

To drive Firefox (tested with version 25.0.1), make sure you have it installed. Open our old friend IRB and type this:

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :firefox
 7 => #<Selenium::WebDriver::Driver:
 8 0x10e1416dd9107ffe browser=:firefox>
 9 
10 > browser.get "http://watir.com"
11 => ""

Great! We can drive Firefox.

Homebrew

To drive Chrome, you need Homebrew. To install it, paste this into Terminal. You will have to type your password during installation.

1 $ ruby -e "$(curl -fsSL
2 https://raw.github.com/mxcl/homebrew/go/install)"
3 (...)
4 ==> Installation successful!
5 You should run `brew doctor' *before* you install
6 anything.
7 Now type: brew help

To check if everything is set up correctly, type brew doctor:

1 $ brew doctor
2 Your system is ready to brew.

Everything looks good!

(You can thank me later for Homebrew.)

Chrome

Chrome 31

Chrome 31

To drive Chrome (tested with version 31), make sure you have it installed.

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :chrome
 7 Selenium::WebDriver::Error::WebDriverError: Unable
 8 to find the chromedriver executable. Please
 9 download the server from
10 http://code.google.com/p/chromedriver/downloads/
11 list and place it somewhere on your PATH. More
12 info at
13 http://code.google.com/p/selenium/wiki/
14 ChromeDriver.
15 (...)

Looks like we have to install something called ChromeDriver executable. The easiest way to install ChromeDriver is via Homebrew.

1 $ brew install chromedriver
2 (...)

Let’s try again:

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :chrome
 7 => #<Selenium::WebDriver::Driver:
 8 0xec6568f803e9898 browser=:chrome>
 9 
10 > browser.get "http://watir.com"
11 => ""

Finally! It works!

Chromium

Chromium 34

Chromium 34

Let’s try driving Chromium (tested with version 33) too, just for fun. Download zip file from download-chromium.appspot.com. Unzip the file and move Chromium.app file to /Applications folder. If you did not already install ChromeDriver, see Chrome chapter. To open the browser for the first time you will have to right click it while holding control, then click Open from context menu. The next time you can open it in an usual way.

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > Selenium::WebDriver::Chrome.path =
 7 "/Applications/Chromium.app/Contents/MacOS/
 8 Chromium"
 9 => "/Applications/Chromium.app/Contents/MacOS/
10 Chromium"
11 
12 > browser = Selenium::WebDriver.for :chrome
13 => #<Selenium::WebDriver::Driver:
14 0x1e35d5faa9511ec6 browser=:chrome>
15 
16 > browser.get "http://watir.com"
17 => nil

PhantomJS

To drive PhantomJS (tested with version 1.9.2), make sure you have it installed. The easiest way to install it is via Homebrew. (You can thank me now for Homebrew. You are welcome.)

1 $ brew install phantomjs
2 (...)

Let’s try driving it:

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :phantomjs
 7 => #<Selenium::WebDriver::Driver:0x..
 8 fbdc736b89bb162d0 browser=:phantomjs>
 9 
10 > browser.get "http://watir.com"
11 => ""
12 
13 > browser.save_screenshot "phantomjs.png"
14 => #<File:phantomjs.png (closed)>

The last command saves screenshot of the page. A screenshot from a headless browser. Nice, right?

PhantomJS

PhantomJS

Opera

Opera 18

Opera 18

To drive Opera (tested with version 12.16), make sure you have it installed.

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :opera
 7 Selenium::WebDriver::Error::WebDriverError: Unable
 8 to find the Selenium server jar. Please download
 9 the standalone server from
10 http://code.google.com/p/selenium/downloads/list
11 and set the SELENIUM_SERVER_JAR environmental
12 variable to its location. More info at
13 http://code.google.com/p/selenium/wiki/OperaDriver.
14 (...)

Error message similar to the one when we first tried to open Chrome. The solution is similar too. Install selenium-server-standalone via Homebrew! (If you did not thank me for Homebrew, you can do it now. You are welcome.)

1 $ brew install selenium-server-standalone
2 (...)

Let’s try again:

1 $ irb
2 
3 > require "selenium-webdriver"
4 => true
5 
6 > browser = Selenium::WebDriver.for :opera
Install Java

Install Java

A popup window will appear saying To open “java,” you need a Java SE 6 runtime. Would you like to install one now?. Click buttion Install, agree with license agreement and Java will install.

Check if Java is installed. Open new Terminal window or tab (it is important to open new window or tab, Terminal will not see Java otherwise) and type java -version:

1 $ java -version
2 java version "1.6.0_65"
3 Java(TM) SE Runtime Environment
4 (build 1.6.0_65-b14-462-11M4609)
5 Java HotSpot(TM) 64-Bit Server VM
6 (build 20.65-b04-462, mixed mode)

The last step is setting SELENIUM_SERVER_JAR environmental variable.

If you just want to try driving Opera, typing this into Terminal will do the trick (if you have a newer version of selenium-server-standalone file, replace 2.37.0 appropriately):

1 $ export SELENIUM_SERVER_JAR=
2 /usr/local/opt/selenium-server-standalone/
3 selenium-server-standalone-2.37.0.jar

Let’s drive Opera, finally! (Following steps will work only in Terminal tab or window where you have exported SELENIUM_SERVER_JAR environment variable.)

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :opera
 7 Selenium::WebDriver::Error::UnknownError: Invalid
 8 service list received:
 9 (...)
10 (java.lang.IllegalStateException)
11 (...)

If you get above error message, install an older version of Opera. Looks like Selenium can not drive newer versions. The last version that I managed to drive was 12.16. You can get older versions at opera.com/download/guide/?os=mac&list=all or arc.opera.com/pub/opera.

Let’s try again.

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :opera
 7 => #<Selenium::WebDriver::Driver:0x..
 8 fc28c93dae7536a48 browser=:opera>
 9 
10 > browser.get "http://watir.com"
11  => nil

Success!

If you plan to drive Opera frequently, you should add SELENIUM_SERVER_JAR to .bash_profile file. Create (if the file does not exist) or edit .bash_profile file in your home folder (/Users/zeljko/.bash_profile in my case, or shorter ~/.bash_profile) with your favorite text editor. Add export SELENIUM_SERVER_JAR... line to the file.

This is how to do it with GNU nano. Type type nano ~/.bash_profile. GNU nano text editor will open. Paste (cmd-v, for example) export SELENIUM_SERVER_JAR... line. Exit GNU nano and save the file with control+x. Press y when it asks Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES)? and press Enter when it displays File Name to Write: .bash_profile or Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES)? (text is different if the file already exists or not).

GNU nano asking should it save changes to `.bash_profile` file

GNU nano asking should it save changes to .bash_profile file

If you have done everything right, GNU nano will close and you will see normal Terminal window. We can check if the line is written to .bash_profile file:

1 $ cat ~/.bash_profile
2 export SELENIUM_SERVER_JAR=
3 /usr/local/opt/selenium-server-standalone/
4 selenium-server-standalone-2.37.0.jar

Open new Terminal window or tab (this is important, already opened windows or tabs would not see SELENIUM_SERVER_JAR variable). Run the same commands again and everything should just work.

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :opera
 7 => #<Selenium::WebDriver::Driver:0x..
 8 fc28c93dae7536a48 browser=:opera>
 9 
10 > browser.get "http://watir.com"
11  => nil

Ubuntu Linux 13.10

You will need internet access if you want to follow examples. All examples are tested with Ubuntu Linux 13.10 32-bit. All browsers are English (US) version.

Ubuntu Linux 13.10

Ubuntu Linux 13.10

Ruby

Let’s see if Ubuntu comes with Ruby installed. Open Terminal and type ruby -v:

1 $ ruby -v
2 The program 'ruby' can be found in the following
3 packages:
4  * ruby1.8
5  * ruby1.9.1
6 Try: sudo apt-get install <selected package>

Install it with sudo apt-get install ruby1.9.1:

1 $ sudo apt-get install ruby1.9.1
2 (...)
3 Setting up ruby1.9.1 (1.9.3.194-8.1ubuntu2.1) ...
4 (...)

Check the version with ruby -v:

1 $ ruby -v
2 ruby 1.9.3p194 (2012-04-20 revision 35410)
3 [i686-linux]

RubyGems

Let’s see the version of RubyGems we got with Ruby with gem -v:

1 $ gem -v
2 1.8.23

Update RubyGems with gem update --system:

 1 $ gem update --system
 2 ERROR:  While executing gem ... (RuntimeError)
 3 gem update --system is disabled on Debian, because
 4 it will overwrite the content of the rubygems
 5 Debian package, and might break your Debian system
 6 in subtle ways. The Debian-supported way to update
 7 rubygems is through apt-get, using Debian official
 8 repositories. If you really know what you are
 9 doing, you can still update rubygems by setting
10 the REALLY_GEM_UPDATE_SYSTEM environment variable,
11 but please remember that this is completely
12 unsupported by Debian.

Since I do not really know what I am doing, I will leave RubyGems at current version and hope everything will work.

selenium-webdriver

Let’s try selenium-webdriver gem. Install it with sudo gem install selenium-webdriver --no-ri --no-rdoc.

1 $ sudo gem install selenium-webdriver --no-ri
2 --no-rdoc
3 (...)
4 Fetching: ffi-1.9.3.gem (100%)
5 Building native extensions.  This could take a
6 while...
7 ERROR:  Error installing selenium-webdriver:
8 ERROR: Failed to build gem native extension.
9 (...)

This should fix the problem:

1 $ sudo apt-get install ruby1.9.1-dev
2 (...)
3 Setting up ruby1.9.1-dev (1.9.3.194-8.1ubuntu2.1)

Let’s try installing selenium-webdriver again:

1 $ sudo gem install selenium-webdriver --no-ri
2 --no-rdoc
3 (...)
4 Successfully installed selenium-webdriver-2.37.0
5 (...)

Sucess!

Firefox

Firefox 25

Firefox 25

Since Firefox (tested with version 25.0.1) is installed by default on Ubuntu, you do not have to install it.

Let’s check if it can drive Firefox:

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :firefox
 7 => #<Selenium::WebDriver::Driver:0x21564606
 8 browser=:firefox>
 9 
10 > browser.get "http://watir.com"
11 => ""

No problem here, works just fine.

PhantomJS

To drive PhantomJS (tested with version 1.9.0), install it via apt-get:

1 $ sudo apt-get install phantomjs
2 (...)
3 Setting up phantomjs (1.9.0-1) ...

Let’s try driving it:

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :phantomjs
 7 => #<Selenium::WebDriver::Driver:0x..fa5f26f44
 8 browser=:phantomjs>
 9 
10 > browser.get "http://watir.com"
11 => {}
12 
13 > browser.save_screenshot "phantomjs.png"
14 => #<File:phantomjs.png (closed)>

The last command saves screenshot of the page. A screenshot from a headless browser. Nice, right?

PhantomJS 1.9.0

PhantomJS 1.9.0

Chrome

Chrome 31

Chrome 31

Now, lets see if it can really drive Chrome (tested with version 31) too. Ubuntu does not have Chrome installed by default, so you have to install it yourself. Download it from google.com/chrome.

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :chrome
 7 Selenium::WebDriver::Error::WebDriverError: Unable
 8 to find the chromedriver executable. Please
 9 download the server from
10 http://code.google.com/p/chromedriver/downloads/
11 list and place it somewhere on your PATH. More
12 info at
13 http://code.google.com/p/selenium/wiki/
14 ChromeDriver.
15 (...)

Looks like we have to install something called ChromeDriver executable. Fortunately, the error message is pretty clear. code.google.com/p/chromedriver/downloads/list will let you know that ChromeDriver executable is now located at chromedriver.storage.googleapis.com. Download the latest version of chromedriver_linux32.zip or chromedriver_linux64.zip (check if you have 32-bit or 64-bit operating system) and unzip it (mouse right click and then Extract Here, for example). You will get a file named chromedriver. Put it somewhere on your PATH, as the error message said.

Let’s find out what is on our PATH.

1 $ echo $PATH
2 /home/zeljko/bin:/usr/local/sbin:/usr/local/bin:
3 /usr/sbin:/usr/bin:/sbin:/bin:/usr/games

/usr/bin looks like a nice place, so let’s move chromedriver there. You will have to provide your password to move the file there.

1 $ sudo mv chromedriver /usr/bin

Let’s drive Chrome, finally:

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :chrome
 7 => #<Selenium::WebDriver::Driver:0x5b77effe
 8 browser=:chrome>
 9 
10 > browser.get "http://watir.com"
11 => nil

And it really works!

Chromium

Chromium 30

Chromium 30

Let’s try driving Chromium (tested with version 30) too, just for fun. Install it with sudo apt-get install chromium-browser:

1 $ sudo apt-get install chromium-browser
2 (...)
3 Setting up chromium-browser
4 (30.0.1599.114-0ubuntu0.13.10.2) ...
5 (...)

If you did not already install ChromeDriver, see Chrome chapter.

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > Selenium::WebDriver::Chrome.path =
 7 "/usr/bin/chromium-browser"
 8 => "/usr/bin/chromium-browser"
 9 
10 > browser = Selenium::WebDriver.for :chrome
11 => #<Selenium::WebDriver::Driver:0x..fb4c9860a
12 browser=:chrome>
13 
14 > browser.get "http://watir.com"
15 => nil

Java

To drive Opera, you will have to install Java first. Let’s check if Java is already installed:

1 $ java -version
2 The program 'java' can be found in the following
3 packages:
4  * default-jre
5  * gcj-4.6-jre-headless
6  * gcj-4.7-jre-headless
7  * openjdk-7-jre-headless
8  * openjdk-6-jre-headless
9 Try: sudo apt-get install <selected package>

Looks like we will have to install Java. Install it with sudo apt-get install openjdk-7-jre-headless:

1 $ sudo apt-get install openjdk-7-jre-headless
2 (...)
3 Setting up openjdk-7-jre-lib (7u25-2.3.12-4ubuntu3)
4 (...)

Opera

Opera 12.16

Opera 12.16

To drive Opera (tested with version 12.16) make sure you have it installed.

Let’s see how it drives Opera. Open our old friend, IRB:

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :opera
 7 Selenium::WebDriver::Error::WebDriverError: Unable
 8 to find the Selenium server jar. Please download
 9 the standalone server from
10 http://code.google.com/p/selenium/downloads/list
11 and set the SELENIUM_SERVER_JAR environmental
12 variable to its location. More info at
13 http://code.google.com/p/selenium/wiki/OperaDriver.
14 (...)

Error message similar to the one when we first tried to open Chrome. The solution is similar too. We have to download a file, put it somewhere and point a variable to it. Do not worry, it sounds more complicated than it really is. Fortunately again, the error message says it all. Go to code.google.com/p/selenium/downloads/list and download selenium-server-standalone-2.39.0.jar (or newer version, the description should be Use this if you want to use the Selenium RC or Remote WebDriver or use Grid 2 without needing any additional dependencies).

The last step is setting SELENIUM_SERVER_JAR environmental variable. If you just want to try driving Opera, typing this into Terminal will do the trick (assuming that the file is located in Downloads folder, if you have a newer version of selenium-server-standalone file, replace 2.39.0 appropriately):

1 $ export SELENIUM_SERVER_JAR=
2 ~/Downloads/selenium-server-standalone-2.39.0.jar

Let’s drive Opera, finally! (Following steps will work only in Terminal tab or window where you have exported SELENIUM_SERVER_JAR environment variable.)

 1 $ irb
 2 
 3 > require "selenium-webdriver"
 4 => true
 5 
 6 > browser = Selenium::WebDriver.for :opera
 7 => #<Selenium::WebDriver::Driver:0x..fb4bb92ce
 8 browser=:opera>
 9 
10 > browser.get "http://watir.com"
11 => nil

If you plan to drive Opera frequently, you should add SELENIUM_SERVER_JAR to .bashrc file. Create (if the file does not exist) or edit .bashrc file in your home folder (/home/z/.bashrc in my case, or shorter ~/.bashrc) with your favorite text editor. Add export SELENIUM_SERVER_JAR... line to the file.

1 $ nano ~/.bashrc

Save the file (ctrl+x) and close all Terminal windows. Open Terminal again. To check if the variable is set, try printenv | grep SELENIUM:

1 $ printenv | grep SELENIUM
2 SELENIUM_SERVER_JAR=/home/zeljko/bin/
3 selenium-server-standalone-2.39.0.jar

Looks good to me!