Cucumber - Ruby Testing


Advertisements

Ruby language has the following advantages −

  • It is easy to understand.

  • It is an object-oriented language.

  • It is a powerful class library.

  • It has massive online support.

Following is the step-by-step process of how Cucumber works with Ruby.

Step 1 − Install Ruby.

  • Go to RubyInstaller download page.

  • Download the version best suited for your operating system (i.e. 32 or 64 bit).

  • Run the downloaded exe.

  • During the installation, tick the option “Add Ruby…” and “Associate …”, as shown in the following image.

Setup Ruby

Step 2 − Download and extract Dev kit.

  • Go to RubyInstaller download page.

  • Download the Devkit version best suited for your operating system (i.e. 32 or 64 bit).

  • Extract the devkit at c:\Ruby\Devkit folder.

  • Open the command prompt.

  • Inside the Ruby devkit, run the following command.

C:\Ruby\devkit> ruby dk.rb init 
C:\Ruby\devkit> ruby dk.rb install

Step 3 − Install Cucumber and other Ruby gem.

  • To install Cucumber, first update the current gem setup

C:\Users\Admin> gem update –system
  • Next, install the gems you need for Cucumber web testing.

C:\Users\Admin> gem install --no-ri --no-rdoc rspec 
C:\Users\Admin> gem install --no-ri --no-rdoc win32console 
C:\Users\Admin> gem install --no-ri --no-rdoc watir-webdriver 
C:\Users\Admin> gem install --no-ri --no-rdoc cucumber
  • Run Cucumber

C:\Users\Admin\Documents>cucumber –init 
C:\Users\Admin\Documents>cucumber

Step 4 − Install IDE – KOMODO.

Step 5 − Install Watir − Go to command prompt and run the following command, "gem install watir"

Step 6 − Install rspec − Go to command prompt and run the following command, "gem install rspec"

Step 7 − Create feature file.

  • Open KOMODO editor.

  • Click on new file icon.

  • Write the following text.

    Feature: Users must be able to search for content using Google.

    Scenario: Search for a term.

    Given I have entered "watir" into the query.

    When I click "search"

    Then I should see some results

  • Click save icon.

  • Give the name as CucumberRuby.feature.

  • Choose any folder, for example: “e:\WithRuby”

  • Save the file.

Step 8 − Create step definition file.

  • Open KOMODO editor.

  • Click ‘New’ file icon.

  • Write the following code.

require "watir-webdriver" 
require "rspec/expectations" 

Given /^I have entered "([^"]*)" into the query$/ do |term| 
@browser ||= Watir::Browser.new :firefox 
@browser.goto "google.com" 
@browser.text_field(:name => "q").set term 
end 

When /^I click "([^"]*)"$/ do |button_name| 
@browser.button.click 
end 

Then /^I should see some results$/ do 
@browser.div(:id => "resultStats").wait_until_present 
@browser.div(:id => "resultStats").should exist 
@browser.close 
End
  • Click save icon.

  • Give the name as CucumberRuby.rb

  • Choose any folder for example: “e:\WithRuby”

  • Save the file.

Step 9 − Create the test file.

  • Open KOMODO editor.

  • Click on ‘New’ file icon.

  • Write the following code.

require "rubygems" 
require "test/unit" 
require "watir-webdriver" 

class GoogleSearch < Test::Unit::TestCase 
def setup 
@browser ||= Watir::Browser.new :firefox 
end 

def teardown 
@browser.close 
end 

def test_search 
@browser.goto "google.com" 
@browser.text_field(:name => "q").set "watir" 
@browser.button.click 
@browser.div(:id => "resultStats").wait_until_present assert 
@browser.title == "watir - Google Search" 
end 
end
  • Click Save icon.

  • Name the file as test.rb and choose any folder for example: “e:\WithRuby”

  • Save the file.

Step 10 − Run the feature file.

  • Go to command prompt.

  • Go to directory e:\WithRuby

  • Run the following command.

e:\With Ruby>ruby test.rb

You will observe the following things upon execution −

  • A web browser instance will open.

  • Google.com webpage will get loaded.

  • Search text watir will be entered.

  • Search button will be placed.

  • Search results shall be displayed on the webpage.

  • Browser instance will get closed.

Advertisements