Selenium — Ruby Automation — Part II

Roselyne Makena
4 min readSep 26, 2020

--

I hope you have had a chance to go through Selenium — Ruby Automation — Part I in order to catch up with what we will be doing in this tutorial and the basics to get started on selenium ruby automation — if you need to.

In part II, we will go through using ruby to do slightly more complex actions such as hover/mouse over an element, click items from a dropdown list, and getting data from webtables. I will be using a stock chart website for listed stocks in Kenyan companies here: live stocks. (This is a random site I have chosen to demonstrate the concepts).

PS: Feel free to use any other website, so long as it contains the elements we will be automating above.

By the end of this tutorial, we should be able to:

  • Configure and use chrome webdriver as the default test browser — in Part I, we used firefox as the default web browser. To switch things up a little bit, we will use chrome as the browser for these of tests.
  • Use selenium driver to hover/mouse over an element
  • Select table elements and looping through the text elements

a) Configuring chrome webdriver for our tests: To install, follow the

  • Installing chrome-webdriver on macOS: check this link
  • Installing chrome-webdriver on windows:

Our script:
Lets start off with typing in the following in our script — save it as selenium_part_II.rb

require 'selenium-webdriver'
require 'test-unit'
class SeleniumPartIITest < Test::Unit::TestCase# Setup that runs before each test
def setup
@url = "https://live.mystocks.co.ke"
@driver = Selenium::WebDriver.for :chrome
action = Selenium::WebDriver::ActionBuilder
@driver.get(@url)
@driver.manage.window.maximize
@driver.manage.timeouts.implicit_wait = 30
@wait = Selenium::WebDriver::Wait.new(:timeout => 7)enddef test_market_watch_drop_downmarket_watch = @wait.until {@driver.find_element(:link, "Market Watch")}
puts "#{market_watch.text}"
assert_equal market_watch.text, "Market Watch"
# Hover over to market watch and hold so that we can view the options
@driver.action.move_to(market_watch).click_and_hold
sleep 5
@driver.action.move_to(market_watch).perform
# We click the mnuPrice list
price_list = @wait.until{@driver.find_element(:id, "mnuPricelist")}
price_list.click
sleep 3# select the table by it's id
the_table = @wait.until{@driver.find_element(:id, "pricelist")}
trs = the_table.find_elements(:tag_name, "tr")
trs.each_with_index do |row, index|
# We will filter out the first five rows to simplify our testing.
# The reason we filter out rows (0,1,2) is because we want
# to skip out the title header rows - see diagram
if(index < 8 && index > 2)
puts "#{index}"
tds = row.find_elements(:tag_name, "td")
# loop through the columns
puts "------------------"
tds.each_with_index do |td, index|
puts "#{index} : #{td.text}"
end
else
#let's ignore rows outside our loop count by doing nothing.
end
end
end
#quit after the test runsdef teardown
@driver.quit
end
end

Running the script:

Run the script by navigating to the location of the folder and typing ruby selenium_part_II.rb

As we discussed in part I, the browser exists and closes the session due to the teardown method executing the following command@driver.quit

What to expect after running our script

The test will open the site below, and then hover over Market Watch. We will then click on the price-list menu and navigate to a different page with a table where we do a couple of more functions with the data from the table.

a) Holding and clicking the drop-down menu.

A quick inspection of the dropdown element under Market Watch, will reveal the id as mnuMarket as shown in the image above and is activated by hovering over the element.

If you have a quick look at our test, we hover over the element, to reveal a list of select options and we click on it.

# Hover over to market watch and hold so that we can view the options
@driver.action.move_to(market_watch).click_and_hold
sleep 5
@driver.action.move_to(market_watch).perform
# We click the mnuPrice list
price_list = @wait.until{@driver.find_element(:id, "mnuPricelist")}
price_list.click

b) Select table rows and print results for analysis

After our script navigates to the pricelist, we can now see a table with an id pricelist.
We use this id to select our table and loop through the elements to print out the text in first row of each element.

Ignoring indexes 0–2 to avoid printing out the table header rows is a neat trick to print out just the contents of the table.

# select the table by it's id
the_table = @wait.until{@driver.find_element(:id, "pricelist")}

trs = the_table.find_elements(:tag_name, "tr")
trs.each_with_index do |row, index|
# We will filter out the first five rows and
# ignore the rest to simplify our testing.
# The reason we filter out rows (0,1,2) is because we want
# to skip out the title header rows - see Diagram — T1

if(index < 8 && index > 2)
puts "#{index}"
tds = row.find_elements(:tag_name, "td")
# loop through the columns
puts "------------------"
tds.each_with_index do |td, index|
puts "#{index} : #{td.text}"
end
else
end
end

Diagram — T1

Conclusion:

This were just a few things that selenium can do, other things include — checking and unchecking checkboxes/radio buttons, drag and dropping elements, taking screenshots, executing javascript code..etc. I hope to cover some of these in another tutorial!

To see more interesting tutorials, do check out this blog here.

references:
https://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/ActionBuilder — Action builders in selenium ruby. Read more about

Hope this was insightful!
Thanks for reading. Feel free to drop questions/comments below :)

--

--

Roselyne Makena
Roselyne Makena

Written by Roselyne Makena

Design thinking analytic. Born creative with design edge. Software QA Engineer. I write articles at: senseitechtips.com

No responses yet