How to Use Selenium with Ruby (Part 1)
Ruby is a well-known object oriented programming language specifically made for developing client environments. In 2004, a framework named RoR or ‘Ruby on Rails’ was developed for this language which ignited Ruby’s success in the applications market and the lean startup methodology. Many link the language (Ruby) to the framework (Rails), which is wrong especially, in test automation with Selenium. In this article we will learn how to setup Selenium with Ruby, our tests will be written in Ruby while interfacing to Selenium’s API through ‘Gems’ on top of a specific IDE (which I’ll go into detail later on!).
These are the steps to run a test in Selenium with Ruby:
Part 1 (Current):
- Environment Setup – Ruby
- Get to know Interactive Ruby
- Selecting and installing IDE
Part 2 (Next):
4. Installing Selenium thorough Gems
5. Automatic function in Selenium with Ruby
6. Automated test in Selenium with Ruby
1. Environment Setup – Ruby
There are a number of ways to install Ruby, in this post I’ll choose the simplest and will demonstrate how to install it on Windows OS through RubyInstaller. To do so, enter: http://rubyinstaller.org/downloads
And download the current version:
When the installation is over, you’ll notice a new library in C: drive – Ruby 22 (the number indicates the version). You will notice a new section:
2. Get to know Interactive Ruby
*In this part we will use Ruby’s syntax, the purpose of this post is not to teach you to program with Ruby. In case you’d like to expand your knowledge, there are plenty of tutorials online to learn from.
The Interactive Ruby (irb) is an external interactive user interface (Shell) through which we’ll be able to write different pieces of code, execute them and get a result without installing an IDE.
The activation document is in: C:\Ruby22\bin\ruby.exe
Which we’ll activate and then move along to the following window:
From this point forward, we can begin writing a code in Ruby. This is the first and the most simple example- we’ll write a command that we’ll print “Hello World” on the screen (using ‘puts’ command):
*‘nil’ means an empty value.
Now we’ll insert the simplest command into a function (configured under ‘def’) by entering parameters:
We’ll be able to use Object Oriented:
It’s possible to view the program better (not through the irb) like so:
3. Selecting and installing IDE
Obviously better ways exist for editing and writing Ruby rather than the command shell, the popular IDE’s are RubyMine by JetBrains, NetBeans plugin, the Komodo or the free tool Aptana Studio.
I chose to focus on Aptana which offers 2 options:
- To download and install a StandAlone version.
- To download a plugin for Ecliplse.
We’ll choose Eclipse since it’s quite popular. First let’s download Eclipse from its website: http://www.aptana.com/products/studio3/download.html
And select the plugin version that needs to be downloaded:
After clicking on ‘Download’, we won’t need to click on the link, we’ll copy it and save it for later.
Now, when we open our Eclipse IDE, we’ll click on Help>Install new software.
In the new window that will open, we’ll paste the link.
Accept the terms:
Wait until the installation finishes and restart Eclipse:
In the next step, again we’ll click Help>Install New Software, this time selecting ‘All Available Sites’ (a process that takes time).
Scroll down until you’ll reach ‘Programming Languages’ and select ‘Dynamic Languages Toolkit- Ruby Development Tools’
At this point as well, we’ll need to accept the terms, wait until the installation finishes and restart Eclipse.
Next stage is to configure Ruby’s Interpreter that will be working with Eclipse. In order to do so, in Eclipse we’ll click on Window>Preference and choose Ruby’s Library>Interpreters>Add> Ruby.exe under C:\Ruby22\bin
Stay tuned for the the next part in learning how to use Selenium with Ruby: Configure Selenium, assign it to an environment just created and start writing automated tests.