Selenium Automation Framework with Cucumber in Ruby
Hello! In this blog, We will be exploring how to automate tests using selenium with Cucumber in Ruby.
What is Selenium?
How does the selenium web driver in Ruby work?
How to use selenium with ruby?
Ruby is a scripting language designed by Yukihiro Matsumoto, also known as Matz. It runs on a variety of platforms, such as Windows, Mac OS, and various versions of UNIX. Ruby is a pure object-oriented programming language.
Advantages of Ruby:
- Open source and community support.
- Flexibility.
- Suitable for beginners.
- Supports Web development and Desktop Applications.
- Command line programs- Ruby can be used to create command line programs. These are programs that are run from the terminal, and they can be very powerful and efficient. You can explore command line programs more at https://www.cloudbees.com/blog/creating-powerful-command-line-tools-in-ruby.
- Ruby is a general-purpose, interpreted programming language.
- Ruby also provides a great number of helpful tools and libraries.
- Ruby has a clean and easy syntax that allows a new developer to learn very quickly and easily.
What is Selenium:
Selenium is an open-source tool that automates web browsers. Selenium and Ruby can be used together, by using a gem called selenium-web driver. With this gem, you can easily automate your test cases. Selenium is a powerful tool as it supports major web browsers and supports almost all OS platforms- Windows, Linux, and Mac. You can explore selenium more on
How does the selenium web driver in Ruby work?
Selenium WebDriver is a Gem that wraps the WebDriver server and makes it easier to drive a browser. WebDriver is a program that can control a web browser.
- Installing selenium-web driver via gem: simply run this command on the terminal.
“gem install selenium-webdriver”
What is Ruby Gem:
Ruby gems are simply open-source libraries that contain Ruby code and are packaged with a little extra data. Using a gem allows a programmer to use the code within the gem in their program, without explicitly inserting that code.
Gems can be used for all sorts of purposes, and you can explore different gems at https://rubygems.org/. To get a better idea of what gems can do, here are a couple of popular gems and their functionality:
- Bundler — Provides a consistent environment for Ruby projects by tracking and installing the needed gems and versions. It is the #1 downloaded gem of all time, but more on Bundler later.
- RSpec — A testing framework that supports Behavior Driven Development for Ruby.
- Devise — Devise works with authentication. For any website that needs to user login, Devise handles sign-in, sign-up, reset password, etc.
- JSON — Provides an API for parsing JSON from text.
Installing Gems:
Installing gems locally is as simple as a single command: gem install [gem_name]. The install command fetches the code, downloads it to your computer, and installs the gem and any necessary dependencies. Finally, it will build documentation for the installed gems.
How to use selenium with Ruby:
In Ruby, you can create an instance of the WebDriver class by either instantiating an object or by starting a session. The instantiated object returns a session object of the WebDriver.
To instantiate a driver with the session object, we would need to do the following:
• @driver = Selenium::WebDriver.for :chrome
How to install ruby setup:
• Ruby installer: Ruby+Devkit-3.2.2-1
Here is the link from where you can download the ruby installer: https://rubyinstaller.org/downloads/
After installing ruby we need to install a few gems to start:
• gem install selenium-webdriver
• gem install selenium-cucumber
Download IDE for writing the code
• IDE : Intellij Idea Ultimate-2022.1.2
Steps to create files:
- Create Project: File-New-Project
- Create feature File: Right Click on project folder(Calculator)-New-Directory-features(name of folder)-Calculator.feature(File)
- Create Step Definition File: Right Click on features(folder)-New-Directory-step_definitions(name of folder)-CalculatorSteps.rb(File)
- Create Page File- Right Click on features(folder)-New-Directory-CalculatorPage(Folder)-Calculator.rb(File)
What is Feature File:
Feature Files are used to write acceptance steps during automated testing. A Feature File is a common file containing the feature description, scenarios, and steps for testing a feature. It serves as an entry point to the Cucumber tests. It is the file where your test scenarios are written in the Gherkin language. These files have a “.feature” extension, and users should create a separate Feature File for each software functionality.
A simple feature file consists of the following keywords/parts:
- Feature − Name of the feature under test.
- Description (optional) − Describe the feature under test.
- Scenario − What is the test scenario?
- Given − Prerequisite before the test steps get executed.
- When − Specific condition which should match in order to execute the next step.
- Then − What should happen if the condition mentioned in WHEN is satisfied?
We will create a project first, so click on File and choose “new project” from the menu that appears.
After creating the project we will create a feature file, so right-click on the project name and create a folder- feature and then create the file name as “Calculator. feature”.
Here is the code snippet for the feature file:]
Feature: Calculator
Scenario: Addition of two numbers
Given I launch calculator application
When I click on number 4
And I click on operator +
And I click on second number 5
Then I verify the result is 9
Now here all the steps are undefined, so we need to add a step definition file, so right click on the features folder and create a folder named “step_definitions” and in this folder create a file name “CalculatorSteps.rb”
What is Step Definition File:
The steps definition file stores the mapping between each step of the scenario defined in the feature file with a code of the function to be executed. So, now when Cucumber executes a step of the scenario mentioned in the feature file, it scans the step definition file and figures out which function is to be called. So for each step mentioned in the feature file (GIVEN, WHEN, THEN) which you want to execute you can write code within the step definition file.
Given(/^I launch calculator application$/) do
@Homepage = Homepage.new(@driver)
@Homepage.visit
end
When(/^I click on number (.*)$/) do |num1|
@Homepage.getnum1(num1);
end
And(/^I click on operator (.*)$/) do |oprt|
@Homepage.getoperator(oprt);
end
And(/^I click on second number (.*)$/) do |num2|
@Homepage.getnum2(num2);
sleep 2
end
Then(/^I verify the result is (.*)$/) do |result|
expected_result=@Homepage.verify_result()
expect(expected_result).to eq(result)
puts(result)
puts(expected_result)
puts "result match successfully"
end
However, this is not the complete job done.
Now if you hover over a method it shows cannot find method_name, so we need to create a method for every action, so right click on the features folder and create a folder name as “CalculatorPage” and in that folder create “Calculator. rb”
Here is the code snippet for the page file where we can have all methods:
require 'selenium-webdriver'
class Homepage
def initialize(browser)
@driver= Selenium::WebDriver.for :chrome
@driver.manage.window.maximize
end
def visit
@driver.get "https://www.calculator.net/"
puts "launch application successfully"
end
def getnum1(num1)
@driver.find_element(:xpath,"//span[@onclick='r(#{num1})']").click
end
def getoperator(oprt)
@driver.find_element(:xpath,"(//span[contains(text(),'#{oprt}')])[1]").click
end
def getnum2(num2)
@driver.find_element(:xpath,"//span[@onclick='r(#{num2})']").click
end
def verify_result()
@driver.find_element(:id,'sciOutPut').text.lstrip()
end
end
What is Page File:
In the above code snippet, methods contain actions like opening the browser, visiting a website, CSS selectors, Xpaths, and actions to be performed on it (click), and verifying. Simply page file is where you write all the code in detail, all the actions you want to execute in the step definition file. By creating methods you can avoid messy code and can show only necessary things in the step definition file.
To run the feature file we need to configure the project so,
Here is the snippet for configurations:
Go to Run on the top bar and click on plus icon select cucumber and then add the following details for configurations:
After completing the configuration now run the feature file with the command on the IntelliJ terminal:
Before running this command make sure you are at the correct feature file path so here you need to move to the Calculator folder.
• cucumber features/Calculator.feature
here is the snippet of output:
Here is the cucumber report: you can get this report by running the command on the IntelliJ terminal-
- cucumber features –format HTML –out reports
Conclusion:
Like Java and Python, Ruby is a general-purpose programming language. however, it focuses on simplicity, precision, and productivity. Ruby being an array-based language, is very powerful for specific uses and works particularly well with ordered datasets.
Pranali has over 1.10+ years of experience in QA, with a strong foundation in both manual and automation testing. I’m a versatile engineer skilled in Selenium, TestNG, Cucumber, Java, Ruby, POSTMAN, SitePrism, Capybara, Jira, SQL, and GitHub.