App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide Getting Started With Automation Testing Using Selenium Ruby: Tutorial

Getting Started With Automation Testing Using Selenium Ruby: Tutorial

By Gurudatt S A, Community Contributor -

Selenium is a popular automation framework library for running end to end tests against applications which run inside browsers with browser automation. Selenium supports various language bindings and this article discusses how to set up Selenium Ruby to test browser applications.

Selenium Ruby Installation

  1. Install Ruby from here
  2. Install Selenium Webdriver by executing command gem install selenium-webdriver in command line

Install testing framework library by executing command gem install test-unit -v 1.2.3 in command line

Understanding the Selenium Ruby Webdriver API

Before writing the test, let us see prerequisites needed from Selenium which is crucial to write the Selenium Ruby test:

  • Selenium driver object initialization

Whenever, creating a driver object in webdriver, it is always specified for which browser (Firefox, chrome etc.). Similarly in Selenium Ruby, create webdriver object for chrome using the command below:

driver = Selenium::WebDriver.for :chrome
  • Accessing URL

In this article we will be using BrowserStack’s Demo url https://www.bstackdemo.com/

And our rube code will look like :

driver.navigate.to "https://www.bstackdemo.com/"
  • Element Access

To interact with our element using selenium we have to be first finding the element using find method and then perform action like click, below is the ruby way of finding element

driver.find_element(:xpath => "//*[@class='checkmark' and contains(text(),'Apple')]")

With this basic understand let’s start writing a test 

Writing First Test in Selenium Ruby: Example

To understand Selenium Ruby let’s consider below scenario and it’s steps

Scenario: Validate the search result displayed when user selects Apple Vendor

Test Steps: 

1. Access BrowserStack URL which looks like below:

Selenium Ruby Test example on BStackDemo

2. Click on Apple Vendor and Validate the Text which shows the number of products found.

Selenium Ruby Example Test on BStack Demo Search Page

Test Script for Selenium Ruby Test

Create a folder and create a test file ending with .rb extension (You can use any IDE, but i will be using Visual Studio Code)

Folder Structure of Selenium Ruby Example

Run Selenium Ruby Tests on Real Devices

Add the following code in the .rb file created.

require "selenium-webdriver"
require "test/unit"

class SeleniumRubyTest < Test::Unit::TestCase
@@driver

def setup
# create Driver object for Chrome
@@driver = Selenium::WebDriver.for :chrome
# Navigate to URL
@@driver.navigate.to "https://www.bstackdemo.com/"
end

def test_login
@@driver.manage.timeouts.implicit_wait = 10 # seconds
# Find the element using driver object
element = @@driver.find_element(:xpath => "//*[@class='checkmark' and contains(text(),'Apple')]")
wait = Selenium::WebDriver::Wait.new(timeout: 30)
# Wait until the element is displayed
wait.until { element.displayed? }
@@driver.find_element(:xpath => "//*[@class='checkmark' and contains(text(),'Apple')]").click
# Wait for the condition
wait.until { @@driver.find_element(:css, ".products-found span").text == "9 Product(s) found." }
actualValue = @@driver.find_element(:css, ".products-found span").text
# Assert the expected text with actual text
assert_equal(actualValue, "9 Product(s) found.")
end

def teardown
# Quit the driver
@@driver.quit
end

end

The above test consist of three parts, that are:

  • Setup: We are initializing chrome driver and access our BrowserStack demo application
  • Test: Contains Element actions and Assertion
  • Teardown: Driver quit

Run the above Selenium Ruby test by executing below command:

ruby first-test.rb

As the tests executes, below would be the result in terminal:

Run Results of Selenium Ruby Tests

Check out the Official Documentation to learn how to Run Selenium Ruby Tests on real devices using BrowserStack.

Conclusion

This article covered how to Install Ruby, Selenium and test-unit gems, and also written the first test explaining how the Ruby Selenium APIs works. With this basic understanding, you can write our framework/page objects using Ruby Selenium.

Selenium is a reliable automation tool, but it is important to test the application on real device cloud for more accurate test results. By testing under real user conditions you can identify the bottlenecks in the real user experience and rectify them in time before release. Selenium tests can take leverage of BrowserStack’s powerful cloud platform to run tests in a faster and efficient way.

Test on Real Devices for Free

Tags
Automated UI Testing Selenium Selenium Webdriver UI Testing

Featured Articles

How to start with Selenium Debugging

Top Selenium Reporting Tools

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack