Selenium — Ruby Automation — Part I
This is part one of a series of ruby-selenium tutorial, it encompasses what I have learnt over time while automating web interfaces with Ruby. The main objective for this particular story, is to highlight on how to get started with web automation the Selenium Webdriver and ruby as the scripting language.
Prerequisites.
Before getting started, ensure that you have ruby up and running on your laptop/workstation and you have firefox installed — for this tutorial, I will use firefox, but do note that selenium supports many other drivers for browsers such as: Chrome, Internet Explorer, Safari etc.
The tutorial also assumes that you have beginner to intermediate knowledge in ruby. If not, checkout tutorials on ruby tutorial. Please note I am using Linux Debian for this tutorial.
Getting started.
Installing the selenium-webdriver gem and test-unit gem.
To install this gems on your system, simply enter the following on your console.
sudo gem install selenium-webdriver
sudo gem install test-unit
This gems will expose the selenium webdriver api and test-unit functionalities for use in your script. To check which version of ruby selenium gem you are using, use gem list selenium-webdriver
Great. So now, let’s fire up our favorite ide/text editor to get started, I am using sublime text for this particular session — which is a great editor, but feel free to use whichever editor suits you best.
Type in the following and save it in a google-test.rb
require 'selenium-webdriver'
require 'test-unit'class GoogleTest < Test::Unit::TestCasedef setup
@driver = Selenium::WebDriver.for :firefox
@url = "https://www.google.com/"
@driver.manage.timeouts.implicit_wait = 30
enddef test_google_search
@driver.get(@url)# Search for the search box using the name selector
@query_box = @driver.find_element(:name, 'q')# Input the search phrase we need to search for in the searched query_box element
@query_box.send_keys("Selenium webdriver")# Click submit button(enter to search)
@query_box.submit# Add an explicit wait to ensure the page finishes loading
wait = Selenium::WebDriver::Wait.new(:timeout => 30)wait.until{@driver.title.include? "Selenium webdriver - Google Search"}# Assert that the search bar title is what you expect
assert_equal("Selenium webdriver - Google Search", @driver.title)
enddef teardown
@driver.quit
endend
Running the script:
Run the script by navigating to the location of the folder and typing ruby google-test.rb
After the script runs, the browser exists and closes the session due to the teardown method executing the following command@driver.quit
What to expect:
We expect to see the webdriver launch the following google search page as well as run the below results in the terminal (see below).
Recap:
In the code above, we define three methods: setup, test_google_search and teardown.
def setup — This method runs before each test method in the selenium ruby runtime. It is a method that can hold what needs to be set up before the test runs, in this case, we set up the firefox driver, the url we will access and an implicit wait(how long we will wait before assuming that an element cannot be found) which is set to 30seconds in this instance.
def setup
@driver = Selenium::WebDriver.for :firefox
@url = "https://www.google.com/"
@driver.manage.timeouts.implicit_wait = 30
end
def test_google_search— It is important to note that the methods that run a test in the gem TestUnit, must start with the keyword “test”, otherwise the method will not run as a selenium test case. This method runs after setup but before teardown. It is a method that holds the steps that make up the test case steps.
def teardown — This method runs after each test in the selenium ruby test_method runtime. It is a method that can hold what needs to be run after the test runs.
Types of selectors
Selectors are what enables you to locate the elements you need to interact with. These selectors are accessed mainly through the .find_element
api method in ruby. See below for more examples.
Finding an element using an elements name
@driver.find_element(:name, "name")
Finding an element using element id
@driver.find_element(:id, "idName")
Finding an element using css
@driver.find_element(:css, "cssTag")
Finding an element using tag name
@driver.find_element(:tag_name, "tagName")
Finding an element using a class name
@driver.find_element(:class_name, "className")
Finding an element using a link text
@driver.find_element(:link_text, "linkTextName")#OR@driver.find_element(:link, "linkTextName")
Finding an element using a partial link text
@driver.find_element(:partial_link_text, "partialLink")
Finding an element using the xpath
@driver.find_element(:xpath, "//*[@id="hpG0a"]/a")
How to get the selector from a webpage.
The easiest way to get the selector you need is to view the webpage in a web browser, i.e chrome or firefox and inspect the element you need.
Conclusion:
This was an introduction to the very basics of Selenium and ruby. In part II, we shall look at the power of selenium webdriver, how we can use it to manipulate checkboxes, dropdowns, webtables, buttons, radio buttons among other awesome things. Check this out here: Selenium — Ruby Automation — Part II
To see more interesting tutorials, do check out this blog here.
Thanks!