Cucumber — DRY up your Steps

Way 1:

Feature File:

Scenario: Google
Given I am in Google Search page
When the user clicks on the next button
When clicks next
When selects Next
Then the title should be “Raveendran – Watir”

In your ruby file,

Given /^I am in Google Search page$/ do
puts “I am in google page”
end

When /^(?:the? )?(?:user? )?(?:clicks|selects)?(?: on? )?(?:the?)?(?: next?)?(?: button?)?$/i do
puts “The same script working for 3 step files 🙂 ”
end

Then /^the title should be (.*)$/ do |arg1|
#Code for verification
puts “It works”
end

Solution :

Only 1 step is enough Instead of 3 steps if it targets same script.

Way 2:

Feature File:

Scenario: Google
Given I have verified the result page
When nothing changed
Then the title should be “Raveendran – Watir”

In Your Ruby file,

Given /^I have verified the result page$/ do
Given “I am in Google Search page”
And “the user clicks on the next button”
And “clicks next”
And “selects Next”
And “the title should be “Raveendran – Watir””
end

When /^nothing changed$/ do
#Some Code
end

Then /^the title should be “([^\”]*)”$/ do |arg1|
#Some Code
end

Advertisements

One thought on “Cucumber — DRY up your Steps

  1. dhanveer singh says:

    Hi Raveendran.

    I like your cuRF – It will create Cucumber feature files with different set of test data. very much and trying to execute the sample you have provided, but getting below error message :

    C:\ruby-project\cuRF>rasta data.xls -f script.rb
    C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/dependency.rb:315:in `to_specs’: Could not find ‘rasta’ (>= 0) – did find: [rasta-0.1.10-x86-mswin32-60] (Gem::LoadError)
    Checked in ‘GEM_PATH=U:/.gem/ruby/2.2.0;C:/Ruby22-x64/lib/ruby/gems/2.2.0′, execute `gem env` for more information
    from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/dependency.rb:324:in `to_spec’
    from C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_gem.rb:64:in `gem’
    from C:/Ruby22-x64/bin/rasta:22:in `’

    Can you please guide me to get rid of this error message.

    Thanks in advance….

    Regards,
    Dhanveer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s