Thursday, 21 July 2011

Example - Cucumber with Ruby

Aim:
Running scripts using the integration of Cucumber with Ruby.


Difficulty Level: High

Pre-requisites Steps involved:
  • Download Ruby from link http://www.ruby-lang.org/en/downloads/ and install it and set the path into environment variable.
  • Assume that all the code files save in hard disk.


Steps to Implementation:

  1. Create a directory c:\Demo.
  2. Create a subdirectory c:\Demo\features
  3. Create a 3 subdirectory into “regression”,” step_definitions” and “support” into “features” directory.
  4. Create 2 subdirectory “pages” and “patches” into “support” directory.
  5. You can aloso download the project from here


Codes:
  • Create “cucumber.yml” file under “Demo” folder and put following code into “cucumber.yml” file:

default: -r features/support/patches/ -r features/step_definitions

  • Create “Gemfile” file under “Demo” folder and put following code into “Gemfile” file and change the file extension is .FILE type.

source "http://www.rubygems.org"

gem 'activesupport'
gem 'builder'
gem 'capybara'
gem 'childprocess'
gem 'cucumber'
gem 'diff-lcs'
gem 'ffi'
gem 'gherkin'
gem 'gizmo'
gem 'json'
gem 'mime-types'
gem 'multi_json'
gem 'nokogiri'
gem 'rack'
gem 'rack-test'
gem 'rake'
gem 'rspec'
gem 'rspec-core'
gem 'rspec-expectations'
gem 'rspec-mocks'
gem 'rubyzip'
gem 'selenium-webdriver'
gem 'term-ansicolor'
gem 'tilt'
gem 'xpath'


  • Create “search.feature” file under “regression” folder and put following code into “search.feature” file:

Feature: A simple Time2test

Scenario: A simple Time2test scenario
Given I am on the main Time2test
And I using Username "admin"
And I using Password "time4test"
And I click on the Log In button


  • Create “search_steps.rb” file under “step_definitions” folder and put following code into “search_steps.rb” file:

require "selenium-webdriver"
require 'Cucumber'
Given /^I am on the main Time2test$/ do
visit "http://foundation.time2test.co.uk/wp-login.php"

end

When /^I using Username "([^"]*)"$/ do |query|
on_page_with :Time2test do |page|
page.fill_in_user_login query

end
end
Then /^I using Password "([^"]*)"$/ do |query1|
on_page_with :Time2test do |page|
page.fill_in_user_pass query1

end
end


Then /^I click on the Log In button$/ do
on_page_with :Time2test do |page|
page.click_submit_button
end
end

  • Create “page_with_qastuffs.rb” file under “pages” folder and put following code into “page_with_qastuffs.rb” file:

module PageWithQastuffs
include Gizmo::PageMixin

def valid?
title.should =~ /.*time2test.co.uk*/
end

def title
@document.xpath("//title").inner_text
end

end

  • Create “page_with_time2test.rb” file under “pages” folder and put following code into “page_with_time2test.rb” file:

module PageWithTime2test
include Gizmo::PageMixin

def valid?
if title =~ /.*time2test.co.uk*/
return true
else
return false
end
end

def title
@document.xpath("//title").inner_text
end

def fill_in_user_login query
fill_in 'user_login', :with=> query
end

def fill_in_user_pass query1
fill_in 'user_pass', :with=> query1
end

def click_submit_button
click_on 'wp-submit'
end

def qastuffs_main_css
".description"
end



end

  • Create “env.rb” file under “support” folder and put following code into “env.rb” file:

require 'rspec/expectations'
require 'uri'
require 'net/http'
require "selenium-webdriver"
require 'capybara'
require 'capybara/dsl'
require File.dirname(__FILE__) + '/patches/capybara.rb';

#CAPYBARA
require 'capybara/cucumber'
require 'capybara/session'

#require 'features/support/patches/capybara'
Capybara.default_driver = :selenium
Capybara.run_server = false
Capybara.default_selector = :css
Capybara.default_wait_time = 10
#driver = Selenium::WebDriver.for :firefox
#gizmo!!
require 'gizmo'
World(Gizmo::Helpers)

Gizmo.configure do |config|
config.mixin_dir = File.dirname(__FILE__) + '/../pages'
end

  • Create “capybara.rb” file under “patches” folder and put following code into “capybara.rb” file:


include Capybara
include Capybara::DSL
if Object.const_defined? :Capybara
module Capybara
    alias :response :page
end
end





Run the command:
  1. Open the command prompt.
  2. Select the Project directory Demo.
  3. Run the command below commands.
  • gem install bundler
  • bundle install
  • bundle exec cucumber -r features/support/ -r features/step_definitions

Helpful Video File:

3 comments:

  1. In step 7 the command should have been
    jruby setup.rb and not ruby setup.rb. It's wrong in the video too.

    ReplyDelete
  2. I don't understand where Cucumber and Ant are installed and where they are located.

    Vipin, this looks like you didn't spend enough time. Even the video is too short for a complex example like this.
    I would like to have more detailed instructions on how to install cucumber and ant.

    ReplyDelete