At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

A Guide for Web Automation Testing With Ruby Cucumber And Watir

  • By Arjunan Subramani
  • May 24, 2018
  • 1830 Views

Cucumber is a testing framework, used to write acceptance testing for the web application. Thus follows basic concept of writing every test cases in human language. Here the term human language defines the plain form of structured languages called Gherkin.
Gherkin used for writing cucumber feature files. It has been translated over 70 languages. If you ask what’s the role of Cucumber here, Then we can finish it with one phrase that cucumber is an open source tool that acts as a strong bridge between the test result and human languages. It mainly follows Behavior Driven Development (BDD) methodology. Cucumber support many languages like Ruby, Python, Java, etc.
Identically, Cucumber is integrated and supported by many frameworks like Watir, ruby on rails, Selenium, Capybara and so on. In this tutorial, we had focused on integrating cucumber with watir.

Prerequisites

Make sure you have installed ruby on your system.
You can Download chrome driver here
Now, Extract the zip file and make it executable using the following command

chmod +x chromedriver

 
Then move the executable file to /bin folder

sudo mv chromedriver /bin

 

Setup

First, we need to create a new project for cucumber-test

$ mkdir cucumber-test
$ cd cucumber-test

 
Then create a file name with ‘Gemfile’ and then add the following gems to Gemfile
In addition, You can get the Gems here https://rubygems.org

gem 'watir'
gem 'rspec'
gem 'cucumber'

 
Then run bundle install to install gems for our testing

$ bundle install

 
cucumber –init to set up the directory structure for cucumber project

$ cucumber --init
create features
create features/step_definitions
create features/support
create features/support/env.rb

 
Henceforth, Add the following lines to features/support/env.rb file.
Now, in env.rb file we are initializing the Before and After hooks.
Initially, Before hook will run the first step of each scenario and After hook will run the last step of each scenario even when steps are failed, pending and skipped.
env.rb file should have following codes,

require 'watir'
Before do
@browser = Watir::Browser.new :chrome
@browser.window.maximize
end
After do
@browser.close
end

 
Ofcourse, We already added watir in our Gemfile.
Note: @browser = Watir::Browser.new :chrome -> invoking/opening chrome browser

Gherkin Keywords

Feature
Background
Scenario
Given
When
Then
And
But

Feature:

Furthermore, the Gherkin documents will start with Keyword called “Feature”. In this section, we would describe the functionality of software feature. The description in Feature has no special meaning, it will hold only the important aspects of software features. And, it will not have any impact on run-time and the descriptions can be ignored.

Background:

In general, “Background” keyword is used to repeat the same set of steps before starting all Scenario in a feature and after the Before hooks.
In feature file, we write the keyword Background before the first Scenario. Here the feature file contains only one set of Background steps.

Scenario:

A scenario is one of the core of Gherkin structures. Every scenario starts with the keyword Scenario followed by an optional scenario title. It describes the specific test of the software function. It contains one or more steps. Cucumber executes each step but once at a time.

Given:

Given step describes the initial state of the system. When cucumber executes the step of Given, it will put the system in a known state before user interacting the system.

When:

When is used to describe an action or event.

Then:

Then is used to describe the expected result or outcome. Then step is use assertion to compare the actual outcome and expected outcome.

And & But:

Can be used when we use multiple Given, When and Then statements.

Feature file

In cucumber, all test cases are written in feature files. Feature file will have the extension of .feature. The first line of the feature file starts with keyword Feature. Plus, Every feature file contains single feature. Now, the feature contains one or more Scenarios and each scenario contains a list of steps. Likewise, Feature keyword used to describe the functionality of test.
Create a file amazonsearch.feature under features/ directory and add the following lines.

Feature: Amazon Search functionality
Scenario: searching mobile phones on Amazon
Given a user goes to Amazon home page
When a user search for "mobile phones."
Then amazon should return result for "mobile phones."

 
When we run cucumber features/amazon_search.feature it displays the following message.

Step Definition

In cucumber, each step in the scenario would look for matching step definition. A step definition is a Ruby method with an expression that links to one or more gherkins steps.
Create a file amazon_search_step.rb under features/step_definitions directory and add the following lines

Given(/^a user goes to Amazon home page$/) do
@browser.goto("http://www.amazon.com")
end
When(/^a user search for "([^"]*)"$/) do |arg|
@browser.text_field(:id => "twotabsearchtextbox").wait_until_present
@browser.text_field(:id => "twotabsearchtextbox").set "#{arg}"
@browser.send_keys :return
end
Then(/^amazon should return result for "([^"]*)"$/) do |arg|
@browser.link(:id => "bcKwText").wait_until_present
page_output = @browser.link(:id => "bcKwText").text.include? "#{arg}"
expect(page_output).to eql(true)
@browser.close
end

 
Great, Now run the cucumber features/amazon_search.feature it passes the all the steps.

test passed

Yes, We are done!
Similarly, Want to explore more about web development and other technologies. You are at the right place to explore both present and future technologies, Our tech geeks at Agira Technologies excels in evolving technologies up-to-date, stay updated with us for more technical updates and blogs. Don’t forget to post your interest and impressions in the comment section.
For more details reach us at info@agiratech.com

Arjunan Subramani

Senior Software Engineer | Around 4 Years of experience in Web development and Testing arena. Plus, hands on expertise in Ruby, Ruby on Rails, Watir, Ruby Cucumber.