CSC/ECE 517 Fall 2014/ch1a 19 mx

From Expertiza_Wiki
Jump to navigation Jump to search

Watir


Introduction

Watir(Web Application Test in Ruby), pronounced water, is an automated test tool which uses the Ruby scripting language to drive the Internet Explorer web browser. It allows you to write tests that are easy to read and maintain. It is simple and flexible. Watir was primarily developed by Bret Pettichord and Paul Rogers. It drives Internet Explorer, Firefox, Chrome, Opera and Safari, and is available as a RubyGems gem. Watir is a toolkit for automated tests to be developed and run against a web browser.

Watir project consists of several smaller projects. The most important ones are watir-classic, watir-webdriver and watirspec.

Watir-classic

Watir-webdriver

Watirspec

Watirspec is executable specification of the Watir API, like RubySpec is for Ruby.

Overview

Web Application Testing

Functionality Testing

Test for – all the links in web pages, database connection, forms used in the web pages for submitting or getting information from user, Cookie testing.

Usability Testing

Interface Testing

Compatibility Testing

Performance Testing

Security testing

Why Ruby

Installation

Make It Run

All examples are designed to work on the live Watir demo form: http://bit.ly/watir-example.

Run in .rb

You can require Ruby Watir gem first via the -rubygems command line option or by using the RUBYOPT environment variable. You can also require it manually in your script: require 'rubygems'

Including Watir gem to drive Internet Explorer on Windows require 'watir'

Including Watir-WebDriver gem to drive Firefox/Chrome on Windows/Mac/Linux require 'watir-webdriver'

Starting a new browser & and going to our site browser = Watir::Browser.new browser.goto 'http://bit.ly/watir-example'

Setting a text field

browser.text_field(:name => 'entry.0.single').set 'Watir'

Setting a multi-line text box

browser.text_field(:name => 'entry.1.single').set "I come here from Australia. \n The weather is great here."

Setting and clearing a radio button

browser.radio(:value => 'Watir').set
browser.radio(:value => 'Watir').clear

Setting and clearing check boxes

browser.checkbox(:value => 'Ruby').set
browser.checkbox(:value => 'Python').set
browser.checkbox(:value => 'Python').clear



Clearing, getting and selecting selection list values

browser.select_list(:name => 'entry.6.single').clear
puts browser.select_list(:name => 'entry.6.single').options
browser.select_list(:name => 'entry.6.single').select 'Chrome'

Clicking a button

browser.button(:name => 'submit').click

Checking for text in a page

puts browser.text.include? 'Your response has been recorded.'

Checking the title of a page

puts browser.title == 'Thanks!'

Run in Rspec

RSpec is a Behaviour-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design, and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD. The following code demonstrates using Watir and a basic RSpec example. More Info.[1]

1. Install

gem install rspec

2. Example

rspec test_spec.rb

will execute the following rspec example:

test_spec.rb

require "rubygems"
require "rspec"
require "watir-webdriver"
 
describe "google.com" do
  let(:browser) { @browser ||= Watir::Browser.new :firefox } 
  before { browser.goto "http://google.com" } 
  after { browser.close }
 
  it "should search for watir" do
    browser.text_field(:name => "q").set "watir"
    browser.button.click 
    browser.div(:id => "resultStats").wait_until_present
    browser.title.should == "watir - Google Search"
  end
end

Run in Rspec

Cucumber lets software development teams describe how software should behave in plain text. The text is written in a business-readable domain-specific language and serves as documentation, automated tests and development-aid – all rolled into one format.More Info.[2]

1. Install

gem install cucumber

2. Example

cucumber features

will execute the following cucumber feature: features/example.feature

Feature: Search In order to use Google users must be able to search for content 
Scenario: Search for a term
Given I have entered "watir" into the query
When I click "search"
Then I should see some results

features/step_definitions/example_steps.rb

require "watir-webdriver"
require "rspec/expectations"
 
Given /^I have entered "([^"]*)" into the query$/ do |term|
  @browser ||= Watir::Browser.new :firefox
  @browser.goto "google.com"
  @browser.text_field(:name => "q").set term
end
 
When /^I click "([^"]*)"$/ do |button_name|
  @browser.button.click
end
 
Then /^I should see some results$/ do
  @browser.div(:id => "resultStats").wait_until_present
  @browser.div(:id => "resultStats").should exist 
  @browser.close
end


Run in Cucumber

Cucumber lets software development teams describe how software should behave in plain text. The text is written in a business-readable domain-specific language and serves as documentation, automated tests and development-aid – all rolled into one format.More Info.[3]

1. Install

gem install cucumber

2. Example

cucumber features

will execute the following cucumber feature: features/example.feature Feature: Search In order to use Google users must be able to search for content

 Scenario: Search for a term
   Given I have entered "watir" into the query
   When I click "search"
   Then I should see some results

features/step_definitions/example_steps.rb require "watir-webdriver" require "rspec/expectations"

Given /^I have entered "([^"]*)" into the query$/ do |term|

 @browser ||= Watir::Browser.new :firefox
 @browser.goto "google.com"
 @browser.text_field(:name => "q").set term

end

When /^I click "([^"]*)"$/ do |button_name|

 @browser.button.click

end

Then /^I should see some results$/ do

 @browser.div(:id => "resultStats").wait_until_present
 @browser.div(:id => "resultStats").should exist 
 @browser.close

end

Comparison with Other Tools

Reference