Home / 

BDD approach is counted as one of the most desired/intended techniques in both software development and automation testing. It finalizes the behavior of an application (i.e., how it will work) on the basis of which it starts to build the application. Similarly, BDD testing helps in writing tests and, then, in writing automation code to verify them. The testing scenarios are written in plain English sentences that are directly mapped/glued with Ruby code, which will be discussed later with an example in this blog.

What is Web Application Testing in Ruby (WATIR)?

WATIR is an open-source bundle of Ruby libraries (gems) that is used for automating web browsers. It can automate all popular browsers like Google Chrome, Mozilla Firefox, Internet Explorer, Safari, and run Headless. WATIR is a Ruby gem (or a Ruby API) which is easily scalable to various platforms like different operating systems, cloud testing, and continuous integration. It is fast and can set up your automation project in no time. Faster scripting and amazing out-of-the-box features makes WATIR the first choice of many Test Automation developers.

Pros and Cons of WATIR

Let’s understand a few Pros and Cons of the tool:

    Pros:

  1. Being an open-source tool with a vast community support, the community answers all scripting-related queries and fixes bugs (if any) in no time.
  2. WATIR uses Ruby to write scripts (Ruby is one of the languages which supports duck-typing).
  3. It performs faster in comparison to its counterparts available in the industry.
  4. WATIR has 100s of light-weight ruby gems that makes scripting easier, faster, and maintainable.
  5. Easy integration with cloud testing tools like Sauce Labs, BrowserStack, etc., supports automation on all latest versions of mobile and headless browsers.

    Cons:

  1. Doesn’t automate mobile-native applications.
  2. WATIR only supports Ruby.

Connection between Ruby, Gems, WATIR, Cucumber, and Ruby Version Manager (RVM)

Ruby is an interpreted, high-level, general-purpose programming language. It is a module/library that helps in enhancing the already-written ruby code. Programmers in the ‘Ruby world’ keep these ready for their own use and for the rest of the world. Whenever they need a functionality, they simply install that gem and start working on it. For example, “faker” gem - this gem generates real-looking test data such as usernames, email addresses, phone numbers, etc. Hence, next time when you run your tests cases with unreadable, poor test data like “Test123”, “test123@test.com” etc., download this gem to generate some good test data for you, like “Christophe Bartell”, “kirsten.greenholt@corkeryfisher.info”, etc.

WATIR is another gem from Ruby’s amazing arsenal, just like “faker”, “pretty_face”, etc. This gem helps in running test automation scripts across different browser and operating systems.

Cucumber is a Ruby gem that helps in writing test scripts in plain English language or in Domain Specific Language (DSL ) using Gherkin syntax – Given, When, And, and Then.

RVM or Ruby Version Manager helps in managing different Ruby projects within the same machine.

A developer can switch between different versions to work on several projects with different version requirements. RVM functions as an installer for various implementations of Ruby, such as Matz's Ruby Interpreter (MRI), JRuby, mruby, MacRuby, IronRuby, Maglev, Rubinius, Ruby Enterprise Edition, Topaz, and GoRuby (an interpreter optimized for code golf).

Hence, it can be concluded that Ruby is a scripting language, WATIR is one of Ruby’s gems, Cucumber writes BDD test scenarios and RVM manages gems and project.

Let’s better understand this better by the following picture.

Ruby, Gems, Cucumber, WATIR, and RVM

How to install WATIR on different operating systems?

Windows Installation:

    Ruby installation on windows is straight-forward. To download the latest version of the installer click on https://rubyinstaller.org/downloads/ and start by double-clicking on it. Ruby installer comes with a development kit that helps in building a new project and maintain ruby gems.

Macintosh Installation:

    Generally, Ruby comes pre-installed on MacOS, but it is usually the old version. To update Ruby, you can follow the steps below:

  • Install X code
    • Launch Terminal
    • Type
      xcode-select–install and hit return/enter.
      It will give you a pop prompting to install it. Accept it and you are done.

  • Install RVM – Ruby Version Manager
    Launch Terminal and type
    curl -L https://get.rvm.io | bash -s stable

  • Install Ruby
    Launch Terminal and type
    rvm install ruby
    rvm --default use ruby

Linux Installation:

    Let’s take Ubuntu as an example and learn installation through its ‘apt’ package manager. Launch terminal and type following commands sequentially:

  • First, update the package
    sudo apt update

  • Install Ruby
    sudo apt install ruby-full
  • Ruby installation is completed, now verify it by typing:
    ruby --version

Installing WATIR Gem

    Before installing WATIR, let’s install another important gem called ‘Bundler’. This helps in updating gems and installing multiple gems in one go.

    Launch terminal or command window and type
    gem install bundler

    now let’s install WATIR
    gem install watir-webdriver

WATIR Implementation

After understanding the BDD approach, it's important to finalize the behavior and then write tests, that to be verified using WATIR. Let’s say, there are multiple open vacancies that need to be verified. In that case, the test scenario will be to ‘verify that there are more than one opportunities at GSPANN.’

Test steps:

  • Go to GSPANN.com
  • Mouse hover on the careers tab
  • Click on ‘Find a position’ tab
  • Assert that total opportunities are more than 1

The execution of these test steps would mean that ruby functions are running behind the scenes (since it's already discussed that test steps are glued/mapped with a Ruby function). At this point, there are 2 possible queries. Both are mentioned below with their resolution:

Query 1: How does the code gets to know that there are English sentences to run?

Resolution:

Ruby is a scripting language which doesn’t understand English sentences but a code written in its own syntax. To let Ruby understand what is being said or map English sentences with Ruby functions, another gem is used which is called Cucumber. Cucumber comes with its own syntax - Gherkin syntax - which is nothing but certain formatting standards and annotations. So, let us first install Cucumber install by launching the terminal and typing:

    gem install cucumber

Let’s see the test case again in Cucumber gherkin syntax. Cucumber wants us to define the tests in the following order:

  • Define Feature i.e., what is your test cases about. This is defined only once

  • Define Scenario i.e., one out of many scenarios that you can test around this feature.

  • Use Given, When, And, Then annotations to write test steps.
    • Feature: Verify available opportunities at GSPANN

      Scenario: Verify that there are more than one full-time opportunities at GSPANN

      Given I am on GSPANN website

      When I mouse hover on CAREERS tab

      And I click on Find a position tab

      Then I see more than 1 open opportunities

    This makes the first test case which is called a feature file and it ends with feature extension

Query 2: While running Ruby functions, why is it necessary test in English sentences instead of writing Ruby code straight away?

Resolution: Writing tests in Gherkin syntax helps in understanding what requires to be verified, especially in reports. Also, when our test reports go to a stakeholder or a person, who is not aware of the “Steps to Execute” or only interested whether the functionality is passing or not, they can read it and get confidence in what is passing and what is not.

Open-source integrated development environment (IDE) for Ruby: It’s suggested to use sublime text for Ruby since it is light-weight and can be customized with the use of a vast range of available plugins.

Let’s get hands-on with the example:

Let’s understand the project structure first. The project has following directories and files in the order shown below:


- DemoAutomation
        - features
            - pages 
            - step_definitions
    -support
            -env.rb
            -hooks.rb
    cucumber.yml
    Gemfile
    Gemfile.lock


Start with the name of the project as root directory, let’s say ‘DemoAutomation’. There is a feature directory that keeps all feature files. Create two more directories in the same directory step_definitions.

Pages directory consists of all the pages that are visited during the entire test flow, e.g., Homepage, Careers page, etc. Step definitions directory has the mapping of English sentences that was discussed above in Ruby code.

Parallel to features directory, there is a support directory that contains env.rb and hooks.rb. These two files execute what needs to run before every test. Gemfile contains a list of gems that is required for a project. While making a bundle installation, ruby looks for gems present in this file and installs them together, plus other dependent gems. Once these gems are installed, Gemfile.lock is created automatically so that it is clearly known with which the project is running smooth and the locking prevents an automatic upgrade of any gem.

env.rb:

    require 'page-object'
    require 'page-object/page_factory'
    
    World(PageObject::PageFactory)

    The first two lines adds the page-object gem and page-factory gems, these two gems help in writing less code and makes it readable with less maintenance.

    Example:

    
    @browser.goto https://www.gspann.com
    
    

    It can be simply written as visit_page homepage where visit_page is the out-of-the-box function in page factory and the homepage is a ruby class, which has the page object page_url defined.

    The last line is a way of initializing/calling PageFactory from PageObject.

hooks.rb:

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

It also contains the items that were needed before the test started. Watir web driver was needed to launch the browser before test starts, and it required to be closed thereafter.

Hence, both the files’ were keeping the prerequisites ready.

cucumber.yml

It keeps the configuration running. For example, which browser needs to run, which test needs to run, etc.

Let's keep it simple for this tutorial and run the tests on chrome:

    default: BROWSER=chrome
    

Note:To run tests in Chrome, keep Chrome binary in your environment properties.

Hence, the feature file is put inside the features folder as careers_tab.feature. A feature file must end with ‘.feature’ extension.

Every step or sentence has a ruby function defined. Keeping such files in a ruby (.rb extension) file called ‘step definitions’. To store all these at one place, a specific folder is there which is called ‘step_definitions’. Just go to this folder and create one .rb file. Name of this ruby file will be the same as the feature, i.e., careers_tab.rb

Now, let’s run this feature file. Open the terminal, command prompt, go to the project folder and type the following command:

    cucumber features/careers_tab.feature
    

You can see that it gives and shows the steps that are not defined along with showing how the steps can be defined, just like it is shown in the image below.

Cucumber Features

Let’s copy and paste all these functions in the steps file careers_tab.rb. Now, remove pending lines and call ruby functions instead. Create another .rb file inside step_definitions folder career_tab_helper.rb. The first method is created in this case is

    go_to_gspann_website

    def go_to_gspann_website
    end
    

    After the first ruby function is created, write Ruby code inside this function.

    def go_to_gspann_website
    visit_page gspann_home_page
    end
    

    Here, visit_page is an out-of-the-box function which comes with another gem called page_object. Let’s install this gem by typing

    gem install page_object

    and gspann_home_page is the page class, i.e., another ruby file insides pages folder

    class GspannHomePage
    include PageObject
    page_url "http://www.gspann.com"
    end
    

Before checking everything that is done, there is a small change. It's important to call a helper class in step_definition. For that, go to careers_tab.rb and on top mention

    require_relative "career_tab_helper"
    

    and, hence, the step definition will look like this

    require_relative "career_tab_helper"
    Given("I am on GSPANN website") do
      goto_gspann_homepage
    end
    

Run this feature file again and you’ll see that first step does not appear as pending instead it launches the browser and go to GSPANN’s homepage. By now, you must have understood the working of this amazing tool. You can also write ruby functions for other steps also and run the feature file.

Thus, as an open-source tool, WATIR is most suitable for browser automation, whether it is on desktop or mobile devices. Vast gems repository can help you in writing less code and are less maintenance. Although WATIR has a limitation that it cannot be used for “Native mobile app automation” and “API testing”, it can be overcome by using Ruby and Cucumber without using WATIR by making some tweaks in the project. Finally, it is fun coding in Ruby. Happy Coding!