Automate your Feature and Acceptance Tests in Four Easy Steps

Step 3. Write and run couple of simple tests

What to do:

If you have existing manual test cases, it is quite simple to turn them into Cucumber scenarios:

Feature: General user's home page
  As a general user
  I want to see the home page with the list of carts
  So I can start working with this system

  # Test Case 1.0.1. Check if a user, set up as a General User, can log into the application and the proper General User
  # screen displays showing the user as a (General User) and the My Open carts, My Submitted Carts and
  # the new Cart button are displayed.

  Scenario: General user logs in or goes to the home page
    When I go to the home page
    Then I should see the welcome message
      And I should see my user type: "General User"
      And I should see "My Open Carts"
      And I should see a carts' table
      And I should see "My Submitted Carts"
      And I should see the "New Cart" image

Save it into features/home_page.feature.

In order to write scenarios like this, it’s enough to have some screen mockups in front of you, not even real screens. Compare the manual test case 1.0.1 included as a comment with the executable Gherkin scenario. They are as close as possible and this is important to keep in mind: keep your Cucumber scenarios on the highest possible level. Don’t use implementation details, anything really that is not visible on the screen. If you keep scenarios at a business user’s level, they will be less brittle and you can directly turn Cucumber tests and test results into documentation and items for discussion with business users.

Now, to drive the browser and pass or fail the above scenario you will need to write glue code. You can read on selected drivers’ APIs, but rather than starting from scratch, I would highly recommend to use cucumber_watir project as the starting point.

Start with a very simple “hello world” Watir test like this one for Firefox:

require 'watir-webdriver'
browser = Watir::Browser.new(:firefox)
browser.goto("http://google.com")
if browser.url.include?("google.com")
  puts "Navigated to Google"
end
browser.goto("http://apple.com")
if browser.url.include?("apple.com")
  puts "Navigated to Apple"
end
browser.close

Save it into watir-fx.rb and run it:

>ruby watir-fx.rb

If it drives your target browser successfully, place watir_steps.rb from cucumber_watir in features/step_definitions/ and write couple of real step definitions for your scenario:

# An example: Then I should see the welcome message
Then /^I should see (the )?(.*) message$/ do |article, message_name|
  Then %(I should see "#{msg_text(message_name)}")
End

# An example: Then I should see my user type: "General User"
Then /^I should see my user type: "(.*)"$/ do |user_type|
  @@browser.html.should match( Regexp.new("\(#{user_type}\)", Regexp::IGNORECASE) )
  #html() in Watir/IE returns internal IE DOM tree representation in uppercase
  #See more at http://www.ruby-forum.com/topic/72261
end

Save it into features/step_definitions/app_steps.rb.

The first step definition above reuses cucumber_watir’s step definition by calling it via Then %(…) construct and also uses a helper method to map business-level message name to the actual message. We are going to define this helper method along with couple of others now:

module AppHelpers
  # Maps a logical page name to a path
  def path_to(page_name, app_home = ENV["APP_ROOT_URL"])
    case page_name
      when "home"
        "#{app_home}/"
      else
        raise "Can't find path mapping for the {#{page_name}} page"
    end
  end

  # Maps a logical message name to the actual text
  def msg_text(message_name)
    case message_name
      when "welcome"
        'Welcome '
      else
        message_name
    end
  end

  # Maps a table type to the XPath expression that selects matching table(s)
  def table_xpath(table_type)
    case table_type
      when "carts'"
        "//table[thead/tr/th[text()='Cart Name']][thead/tr/th[text()='Created Date']]" +
        "[thead/tr/th[text()='Last Updated Date']][thead/tr/th[text()='Actions']][thead/tr/th[text()='Status']]"
      else
        raise "Can't find XPath mapping for the {#{table_type}} table"
    end
  end
end

World(AppHelpers)

Save it into features/support/app_helpers.rb.

You would need one more file to initialize Cucumber and launch your target browser. Copy env.rb from cucumber_watir into features/support/ and tweak it for your environment. Finally, run Cucumber:

>cucumber
Advertisements

One Comment

Leave a Reply

Please log in using one of these methods to post your comment:

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