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:

Feature:
‘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.’

Scenario:
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

Scenario:
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
@browser=Watir::IE.new
@browser.goto(“www.google.com”)
end

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

end

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

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

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

About these ads

7 responses to “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?

    Thanks!

    Mauri

    • 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=Watir::IE.new
      @Browser.goto "http://www.google.com"
      if not @Browser.text_field(:name,"q").exists?
      then
      puts "I can't see that textfield, page must not have loaded correctly"
      else
      puts "found it , test passed"
      end

      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=Watir::IE.new
      @Browser.goto "http://www.google.com"
      assert(@Browser.text_field(:name,"q").exists?,"Error opening Homepage")
      end
      end

      When we run this to pass we get :
      Started
      .
      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
    im new in cucumber and watir…can you help me please ?
    regards

  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!

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