googleads
A Guide for Web Automation Testing With Ruby Cucumber And Watir
preloder
Quick Tips

A Guide for Web Automation Testing With Ruby Cucumber And Watir

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

 

Then move the executable file to /bin folder

 

Setup

First, we need to create a new project for 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

 

Then run bundle install to install gems for our testing

 

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

 

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,

 

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.

 

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

 

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

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

The following two tabs change content below.

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.

3 thoughts on “A Guide for Web Automation Testing With Ruby Cucumber And Watir”

  1. looks like the feature test is inconsistent with the implementation.
    for example:
    correct: /^a user searches for “([^”]*)”$/
    wrong: /^a user search for “([^”]*)”$/
    correct: /^Amazon should return the result for “([^”]*)”$/
    wrong: /^amazon should return result for “([^”]*)”$/
    can you please fix?

  2. Hi,
    I am very much grateful for your efforts put on this article about web automation testing tool. This article is very informative and transparent. Can I expect you will post this type of another article in a near future? Thank you for such an great article.

Leave a Reply

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