In the first part of Selenium with Ruby training, we learned how to setup Ruby’s environment, got to know Interactive Ruby, how to select and install IDE. In this part, we’ll learn about ‘Gems’, install Selenium through it; we’ll see how to start writing Selenium code in IDE and finish off with a first test automation case.
Installing Selenium through Gems
Gems are libraries that are connected to Ruby’s environment and Ruby Gems is Ruby’s libraries and addons manager. Through Gems, we are able to install Selenium (or any other library that supports the language).
First thing, we’ll go to https://rubygems.org and enter search the value ‘Selenium WebDriver”. There are many libraries and extensions to Selenium WebDriver in the site and many more libraries and extensions to Selenium itself.
A click on the first result – Selenium WebDriver. On the right side we’ll see a text field and on top of it: ‘INSTALL’, we’ll copy the link.
We’ll go back to Eclipse, notice that now (after installing successfully Ruby’s plugin), we created new views in IDE. We’ll open the Terminal and paste the link we copied before. Press ‘Enter’ and wait until Selenium installation is complete, the screen is supposed to look like this:
Automatic function in Selenium with Ruby
The first program we’ll write in Selenium is going to be very simple: we’ll automate few procedures such as entering a search engine, entering text in a search field and printing the website’s title (the search result).
First, we’ll declare we’re working with Selenium’s package (or better yet, we’ll configure our program to look for the work references with Selenium libraries) as follows:
require “selenium-webdriver”
Configuring the driver for Firefox is very simple and is performed as follows:
driver = Selenium::WebDriver.for :firefoxz
Configuring the driver for Chrome browser is a little more complex and is performed as follows:
chromedriver_path = File.join(File.absolute_path(”, File.dirname(“C://Projects/Selenium/Drivers”)),”Drivers”,”chromedriver.exe”) puts chromedriver_path Selenium::WebDriver::Chrome.driver_path = chromedriver_path driver = Selenium::WebDriver.for :chrome
We can immediately identify the rest of the commands, overall when we are talking about Selenium’s domain, Ruby’s syntax is not much different than the ones we already know from Java or C#, here is the full program:
require "selenium-webdriver" chromedriver_path = File.join(File.absolute_path('', File.dirname("C://Projects/Selenium/Drivers")),"Drivers","chromedriver.exe") puts chromedriver_path Selenium::WebDriver::Chrome.driver_path = chromedriver_path driver = Selenium::WebDriver.for :chrome driver.navigate.to "http://google.com" element = driver.find_element(:name, 'q') element.send_keys "TestProject.io" element.submit puts driver.title driver.quit
The result will obviously be Google.
Automated test in Selenium with Ruby
The next step is to write a program which demonstrates a test while using well known test automation tools (classes, annotations, assets etc.).
In the next automated test we’ll perform a simple system Login, make sure we entered the site and eventually Logout.
First off, we’ll configure a division Test::Unit::TestCase and work inside of it.
We’ll configure SetUp and tearDown annotations as well as Test; the program is expected to look as the following:
require "selenium-webdriver" require "test/unit" class LoginClass < Test::Unit::TestCase def setup Selenium::WebDriver::Chrome.driver_path = File.join(File.absolute_path('', File.dirname("C://Projects/Selenuim/Drivers")),"Drivers","chromedriver.exe") @driver = Selenium::WebDriver.for :chrome @driver.get('http://blog.yoniflenner.net/demo') @driver.manage.window.maximize end def teardown @driver.quit end def test_login @driver.find_element(:name, "username").send_keys "admin" @driver.find_element(:name, "password").send_keys "demo" @driver.find_element(:id, "submit").click sleep 0.3 assert(@driver.find_element(:id => "loggedin").text.include?("You Are Logged in"),"Assertion Failed") @driver.find_element(:id, "logout").click end end
From here on, it’s possible to expand your framework to even more tests that will spread on different elements with error fixes, correct hierarchical configuration of classes… as a matter of fact, endless possibilities.