Cucumber and Watir – A quick tutorial for testers

There tends to be a strong ongoing association between Agile and Open-Source automation tools today. Many make good inroads on the attempt to connect the elaboration of requirements with automated tests, step for step. This way, the test is performing as closely as possible, what the client actually asked for, or agreed in the elaboration process.
One such tool is Cucumber. It is designed to allow you to execute feature documentation written in plain text. In this article, I will detail a concise example, hopefully demonstrating the simplicity and effectiveness of this tool as well as value to the agile methodology.

Prerequisites: It is assumed that before using this example you have the following Installed:
• Ruby
• Watir gem for Ruby
• Cucumber gem for Ruby

I will use a simple example that uses the Google search engine in this case:
Let’s say the user story is something like:

‘When I go to the Google search page, and search for an item, I expect to see some reference to that item in the result summary.’

Cucumber uses the keywords “Given”, “When”, “And” “Then” to build this into useable syntax.

So I create for example, a file, called GoogleSearch.feature containing:

‘When I go to the Google search page, and search for an item, I expect to see some reference to that item in the result summary.’

Given that I have gone to the Google page
When I add “cats” to the search box
And click the Search Button
Then “cats” should be mentioned in the results

Given that I have gone to the Google page
When I add “dogs” to the search box
And click the Search Button
Then “dogs” should be mentioned in the results

Now I create a file GoogleSearch.rb containing the following

require “rubygems”
require “watir”

Given /^that I have gone to the Google page$/ do

When /^I add “(.*)” to the search box$/ do |item|
@browser.text_field(:name,”q”).set( item)


And /^click the Search Button$/ do
@browser.button(:name, “btnG”).click

Then /^”(.*)” should be mentioned in the results$/ do |item|
@browser.text.should include(item)

This gives us executable code for every Given, When and Then statement we have used.

To run it we issue the following command line

cucumber GoogleSearch.feature

or for a nice html formatted result

cucumber GoogleSearch.feature –format html –out result.html


13 thoughts on “Cucumber and Watir – A quick tutorial for testers

  1. Hi Keith,

    My name is Mauri, I am a non-technical tester who’s trying to start doing some automation and find it quite confusing to understand what tools do what. In this case, why do you need Watir? Is Cucumber not able to manage browsers by itself? Are they complementary tools or basically can do the same?



    • Hi Mauri,

      Thank you for your question.
      WATIR (Web application Testing in Ruby), by itself, will allow you to use code to interact with web applications, by controlling the browser.
      Cucumber is a framework that controls how and when to run that code. You could write cucumber tests that work with non-web applications, hence no need for Watir.

      For example a watir script without a test framework could be:
      (checking to see if the google homepage loads)
      require 'watir'
      @Browser.goto ""
      if not @Browser.text_field(:name,"q").exists?
      puts "I can't see that textfield, page must not have loaded correctly"
      puts "found it , test passed"

      all that if not/then/else/end syntax would be tiresome and messy for a large set of tests. So we could use something like test-unit to manage it
      require 'test/unit'
      require 'watir'

      class Test_google <Test::Unit::TestCase

      def test_homepage
      @Browser.goto ""
      assert(@Browser.text_field(:name,"q").exists?,"Error opening Homepage")

      When we run this to pass we get :
      Finished in 3.792807 seconds.

      1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

      We have more more control over the test process here, but to achieve this you see that we have to combine WATIR with some kind of test framework.
      Cucumber is simply a more elaborate test framework than test-unit.

      I would suggest getting familiar with WATIR without cucumber to begin, parhaps with test unit as in the second example..

  2. Thanks a lot for the answer! The more I read it, the clearer I see it. Also, thanks for the tip on starting with Watir first, I’ll give it a try.

  3. hey Keith, your reply to Mauri was really precise and upto the point. Even I had the same doubt and your reply cleared me everything. 🙂

  4. Hi guys,
    Thanks for the help, I am also messed with the mauri’s question.. and got excellent answer from here

    Thanks a lot!!

  5. Hi,

    I tried to run example but it always fails with this error:

    Then “cats” should be mentioned in the results # GoogleSearch.rb:16
    undefined method `include’ for # (NoMethodError)
    ./GoogleSearch.rb:17:in `/^”(.*)” should be mentioned in the results$/’
    GoogleSearch.feature:8:in `Then “cats” should be mentioned in the results’

    It happens on the line “@browser.text.should include(item)”
    I tried googling it but I didn’t find anything (I couldn’t even find a reference for this ‘include’ method)

  6. I was suggested this website by my cousin. I am not sure whether
    this post is written by him as nobody else know such detailed about my problem.
    You are wonderful! Thanks!

  7. Hey Keith!
    Correct me If I’m wrong, but could we say that Cucumber Framework basically allows the tester to set one script for several scenarios instead of having to write down one by one?

  8. Hi Keith,
    I’m a newbie to Cucumber and WAITR. I’m would like to install it onto my PC laptop. Have any suggestions where I can get a tutorial and some instructions on how to initialize? I’d also like to write a test case to it and try and run it. I’m so excited to try something new to me like this!!!


  9. Hi Keith,

    Great post.

    One thought though, is it correct to use ‘@browser.text.should include’ to assert that search results have a reference for the asked keyword? Since we have entered it in the search box, ‘@browser.text’ will always have the keyword, hence the test would always pass, irrespective of whether results contain the term or not.

    Might be worth adding a more specific assert, only on the relevant search results section.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s