1
Automation testingQa

Selenium Ruby tutorial — How to get started Part I

4 Mins read

This is part one of a series of selenium ruby 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 on this selenium ruby tutorial, 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, check out tutorials on ruby tutorial. Please note I am using Linux Debian for this tutorial.

Selenium ruby tutorial – Getting started.

Installing the selenium-webdriver gem and test-unit gem.

To install these gems on your system, simply enter the following on your console.

sudo gem install selenium-webdriver
sudo gem install test-unit

These gems will expose the selenium webdriver api and test-unit functionalities for use in your script using, use gem list selenium-webdriver` – to check the selenium-webdriver version installed. test-unit is a simple testing framework that will help us organize our tests better.

Great. So now, let’s fire up our favourite 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::TestCase
def setup
  @driver = Selenium::WebDriver.for :firefox
  @url = "https://www.google.com/"
  @driver.manage.timeouts.implicit_wait = 30
 end

def 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)
  
 en
def teardown
  @driver.quit
  
 end
end

The assert_equal – helps to verify whether a test has passed or failed by checking whether the first input matches the second input in the method. For example if

Running the script:


Run the script by navigating to the location of the folder via your command line and type the following.

 ruby google-test.rb

After the script runs, the browser exists and closes the session due to the teardown method executing the following command. Basically, selenium will open the web pages and look for the specified elements and perform the steps we have told it to and finally give us the test results.

@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 on the left).

Recap:


In the code above, we define three methods:

  • setup
  • test_google_search
  • teardown

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.

@driver.find_element(:name, "name")
@driver.find_element(:id, "idName")
@driver.find_element(:css, "cssTag")
@driver.find_element(:tag_name, "tagName")
@driver.find_element(:class_name, "className")
@driver.find_element(:link_text, "linkTextName")
#OR
@driver.find_element(:link, "linkTextName")
@driver.find_element(:partial_link_text, "partialLink")
@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.

#Finding an element using an elements name
#Finding an element using element id
#Finding an element using css
#Finding an element using tag name
#Finding an element using a class name
#Finding an element using a link text

#Finding an element using a partial link text
#Finding an element using the xpath

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

Don’t miss amazing tips!

1
Related posts
Automation testingQaRuby

Selenium Ruby Example And Deployment To The Cloud Grid

13 Mins read
Table of contents0.1 What is Selenium?0.2 How does Selenium WebDriver in Ruby work?0.3 How to use Selenium with Ruby?0.4 How to run…
General testingInterestingQa

Available Career Growth Paths In Software Testing

3 Mins read
Table of contents0.1 What Are The Available Career Growth Paths In Software Testing?0.2 Junior Level Possible careers:0.3 Mid Level Possible careers:0.4 Senior…
General testingQa

If You Want To Find Bugs - Do More Exploratory Testing

3 Mins read
Table of contents0.1 Exploratory testing basics0.2 Tips to get started with exploratory testing0.3 1) Set the purpose of your exploratory testing0.4 2)…

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 71 = 73

×
General testingQa

How to become a software tester - Getting Started