Sample Cucumber Test

1,327 views
Skip to first unread message

arihan

unread,
Aug 11, 2010, 11:46:07 AM8/11/10
to Cukes
Hi All,

I am completely new to the cucumber though I've good knowledge about
RUBY+watir which I've used for agile testing since long time.

I just want to make a sample cucumber test and run it.

Scenario say :- go to www.google.com
search with the term "Lancet"
click the search button
check lancet present

I've installed ruby version as ruby 1.8.7 (2010-06-23 patchlevel 299)
[i386-mingw32]
then installed gem install cucumber,
then installed gem install watir

I am using "Start command prompt with Ruby "

now i created three following things
C:\test\features\sample.feature
C:\test\features\step_definitions\sample.rb
C:\test\features\support\env.rb


contents of sample.feature

Feature: Search
In order to learn more
As an information seeker
I want to find more information

Scenario: Find what I'm looking for
Given I am on the Google search page
When I search for "Lancet"
Then I should see
"""
Lancet
"""
contents of sample.rb

require 'test/unit'
require 'test/unit/ui/console/testrunner'
require 'win32ole'

Given 'I am on the Google search page' do
@browser.goto 'http://www.google.com/'
end

When /I search for "(.*)"/ do |query|
@browser.text_field(:name, 'q').set(query)
@browser.button(:name, 'btnG').click
end

Then /I should see/ do |text|
@browser.text.should =~ /#{text}/m
end

contents of env.rb

begin require 'rspec/expectations'; rescue LoadError; require 'spec/
expectations'; end

if ENV['FIREWATIR']
require 'firewatir'
Browser = FireWatir::Firefox
else
case RUBY_PLATFORM
when /darwin/
require 'safariwatir'
Browser = Watir::Safari
when /win32|mingw/
require 'watir'
Browser = Watir::IE
when /java/
require 'celerity'
Browser = Celerity::Browser
else
raise "This platform is not supported (#{PLATFORM})"
end
end

# "before all"
browser = Browser.new

Before do
@browser = browser
end

# "after all"
at_exit do
browser.close
end


Please let me know if any wrong anywhere.

now i run from ruby command prompt
and getting following errors

C:\test>cucumber sample
no such file to load -- spec/expectations (LoadError)
C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original
_require'
C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
C:/test/features/support/env.rb:1
C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original
_require'
C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
C:/Ruby187/lib/ruby/gems/1.8/gems/cucumber-0.8.5/bin/../lib/cucumber/
rb_support/
rb_language.rb:141:in `load_code_file'
C:/Ruby187/lib/ruby/gems/1.8/gems/cucumber-0.8.5/bin/../lib/cucumber/
step_mother
.rb:108:in `load_code_file'
C:/Ruby187/lib/ruby/gems/1.8/gems/cucumber-0.8.5/bin/../lib/cucumber/
step_mother
.rb:100:in `load_code_files'
C:/Ruby187/lib/ruby/gems/1.8/gems/cucumber-0.8.5/bin/../lib/cucumber/
step_mother
.rb:99:in `each'
C:/Ruby187/lib/ruby/gems/1.8/gems/cucumber-0.8.5/bin/../lib/cucumber/
step_mother
.rb:99:in `load_code_files'
C:/Ruby187/lib/ruby/gems/1.8/gems/cucumber-0.8.5/bin/../lib/cucumber/
cli/main.rb
:53:in `execute!'
C:/Ruby187/lib/ruby/gems/1.8/gems/cucumber-0.8.5/bin/../lib/cucumber/
cli/main.rb
:25:in `execute'

aslak hellesoy

unread,
Aug 11, 2010, 12:00:30 PM8/11/10
to cu...@googlegroups.com
On Wed, Aug 11, 2010 at 11:46 AM, arihan <arihan...@gmail.com> wrote:
> Hi All,
>

You need rspec.

gem install rspec

> --
> You received this message because you are subscribed to the Google Groups "Cukes" group.
> To post to this group, send email to cu...@googlegroups.com.
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
>
>

arihan

unread,
Aug 11, 2010, 12:19:16 PM8/11/10
to Cukes
Thanks . I did that and it started working fine as I ran like as below
and got the result.

Now I want to create multiple features files and corresponding
multiple step definition files
as example

sample1.feature
sample2.feature

and in the step_defination folder I would create the sample1.rb &
sample2.rb

now how i would run it as test suite. Any step by step guide would be
highly appreciated

Regards
Arihan

C:\test\features>cucumber sample.feature
Feature: Search
In order to learn more
As an information seeker
I want to find more information

Scenario: Find what I'm looking for # sample.feature:6
Given I am on the Google search page # step_definitions/sample.rb:
6
When I search for "Lancet" # step_definitions/sample.rb:
10
Then I should see # step_definitions/sample.rb:
15
"""
TheLancet
"""

1 scenario (1 passed)
3 steps (3 passed)
0m3.469s


On Aug 11, 5:00 pm, aslak hellesoy <aslak.helle...@gmail.com> wrote:

aslak hellesoy

unread,
Aug 11, 2010, 12:24:07 PM8/11/10
to cu...@googlegroups.com
On Wed, Aug 11, 2010 at 12:19 PM, arihan <arihan...@gmail.com> wrote:
> Thanks . I did that and it started working fine as I ran like as below
> and got the result.
>
> Now I want to create multiple features files and corresponding
> multiple step definition files
> as example
>
> sample1.feature
> sample2.feature
>
> and in the step_defination folder I would create the sample1.rb &
> sample2.rb
>
> now how i would run it as test suite. Any step by step guide would be
> highly appreciated
>

cd C:\test
cucumber features

You'll find several tutorials here:
http://wiki.github.com/aslakhellesoy/cucumber/tutorials-and-related-blog-posts

You can also buy this book if you want a more detailed tutorial:
http://www.pragprog.com/titles/achbd/the-rspec-book

Aslak

gs

unread,
Aug 12, 2010, 5:00:28 AM8/12/10
to Cukes
Hi Aridah,

If you would like to have the basic watir support for actions with
cucumber I would recommend to try watircuke.
There are a lot of defined steps already which helps to write a tests
really fast.

You can find in on the github http://github.com/richdownie/watircuke.

Br,
GS

Chuck van der Linden

unread,
Aug 12, 2010, 3:09:14 PM8/12/10
to Cukes
Don't get hung up on a 1:1 relationship between features and step
files, it's not needed. I find it a lot simpler to maintain a smaller
number of step files that are grouped kinda functionally, such as
'navigation' or for a related group of features.

learn about should 'matchers' it can help to create more readable
code. for example unless I'm mis-remembering there's a 'contain'
matcher, so your Then step could be done as

Then /I should see/ do |text|
@browser.text.should contain text
end
Reply all
Reply to author
Forward
0 new messages