in

Tutorial: Up and Running with Cucumber and Watir-webdriver in a few min.

Making a New Project with Watir-Webdriver and Cucumber

UPDATE: By request, I’ve added some more detail of the full step_def file, feature file and results to the end of this post.

This tutorial uses the awesome RubyMine IDE by Jetbrains.

Step 1: Have Rails installed.  Go to your console and do a Rails new [project name]

Step 2: If you get a SSL issue…

Then edit the gemfile and change the https to http for the rubygems.org call

Step 3: Set up depdancies, in the above screen shot I’ve added a block for watir-webdriver and cucumber, like so:
group :test, :development do
gem ‘cucumber-rails’
gem ‘database_cleaner’
gem ‘rspec’
gem ‘spork’
gem ‘watir-webdriver’
gem ‘gherkin’
gem ‘rest-client’
end

Step 4:  Bundle… you can do this in RubyMine or command line… in ruby mine you do it like this:

Step 5: Configure the project with Cucumber… You do this with the Rails Generator.  You could use the command line, but I’m going to show the steps in screenshots from RubyMine:

 

It will now configure the project – an update will be displayed at the bottom of the IDE.
You’re all set to write/run tests now!

Creating a Sample Test

Step 1: Right-click the Features folder and select “new”

Give it a name ending with.feature:

Step 2: BDD
Once you have a .feature file, lets give it some BDD’s… in this example I used a scenario of searching for cucubmers on amazon.  So I typed:
Feature: lets search amazon for cucumbers
Scenario: A user goes to amazon.com and searches for cucumbers
Given a user at amazon.com
When they search for “cucumbers”
Then the results return for “cucumbers”

Notes the words in quotes?  Cucumber is smart enough to use that as a dynamic replacement.  In other words, if you say later, well i want to test that i can find onions, you replace “cucumbers” with “onions” and the variable is passed through dynamically without rewriting code.

Step 3: Create a step definition file.  This is where we put our code to drive the BDD elements.  You can follow these screenshots:

 

 

Step 4: writing the code
I need to now define the steps.  I define a class variable of @browser to be equal to Watir::Browser.new :firefox
Now whenver @browser is used it calls firefox (we can change the browser dynamically later)

So @browser.goto “http://www.amazon.com” goes to that URL.

For the When statement of supplying a value in the search box, I used firebug on amazon.com and saw this for the search input:

We can use any unique identifying element within the input tag.  Note that watir webdriver uses the term “text_field” for inputs that take text… like: @browser.text_field(:name=>”field-keywords”).set “cucumber”

We could have also used title, or id.  it’s up to you.

My when clause looks like this:
When /^they search for “([^”]*)”$/ do |arg|
@browser.text_field(:name=>”field-keywords”).set “#{arg}”
@browser.send_keys :return
end

So remember, if a person changes the BDD value in quotes from “cucumber” to “onion” it will pass into this, setting the value in that input field.

For the Then, i did this:
Then /^the results return for “([^”]*)”$/ do |arg|
@browser.h1(:id=>”breadCrumb”) == arg
end

there’s a variety of ways you could assert, I choose this.

There’s so much you can do… you can send key commands like Control A, Right click, you can close and open pop ups… use the env.rb file to set default properties like logging in… all sorts of great stuff.

Update: Here’s the code for this tutorial:

 

Step Definition File:

 

Given(/^a user at amazon.com$/) do
  @browser = Watir::Browser.new :firefox
  @browser.goto "http://www.amazon.com"
end
When(/^they search for "([^"]*)"$/) do |arg|
  @browser.text_field(:name=>"field-keywords").set "cucumber"
  @browser.send_keys :return
end
Then(/^the results return for "([^"]*)"$/) do |arg|
  @browser.h1(:id=>"breadCrumb") == arg
end

 

Feature File:

Feature: lets search amazon for cucumbers
Scenario: A user goes to amazon.com and searches for cucumbers
Given a user at amazon.com
When they search for “cucumbers”
Then the results return for “cucumbers”

This is just starter code, as you get going, you’ll want to move the browser definition into the env.rb file… and make the browser a variable, so you can easily switch the tests from IE to FF, to Chrome, etc.  I have posts on doing just that.

PASS and Fail.

If you right click and run the test in RubyMine, it will give you the results in the console. It will look like this:
1 scenario (1 passed)
3 steps (3 passed)
0m10.526s

Similarly, if you run the tests from the command line using the command “cucumber” in your folder… it will output the same results.

Lastly, if you do want the results as a report, you can use a cucumber reports plugin with Jenkins, and when you build the jobs in Jenkins the output is added to a HTML report.  This last reporting method takes more work to set up and I have a post on setting it up.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Written by Admin

I work for a Telecom company writing and testing software. My passion for writing code is expressed through this blog. It's my hope that it gives hope to any and all who are self-taught.

Comments

Leave a Reply

Loading…

Using Rsync to Pull and Monitor remote logs

Distributed Tests