CSC/ECE 517 Fall 2014/ch1a 19 mx

From Expertiza_Wiki
Revision as of 16:40, 18 September 2014 by Mliu9 (talk | contribs) (→‎Introduction)
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|Firefox, Google Chrome|Chrome, Opera (web browser)|Opera and Safari (web browser)|Safari, and is available as a RubyGems gem. <ref>http://en.wikipedia.org/wiki/Watir</ref>Watir is a toolkit for automated tests to be developed and run against a web browser. It is a free, open source Ruby library that drives a web browser the same way an end user would. It clicks links, fills in forms, presses buttons. Watir also checks results, such as whether expected text appears on the page, or whether a control is enabled. Watir is a Ruby library that can test web applications written in any language.

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

Watir-classic

Watir Classic is a Watir driver for automating Internet Explorer on Windows.

Install the gem:

gem install watir-classic

Watir-Classic is supported on Ruby 1.8.7, 1.9.3 and 2.0.0. It should work on Internet Explorer 8, 9 and 10.

Watir-webdriver

Watir implementation built on WebDriver's Ruby bindings. Any browser that WebDriver supports: namely Firefox, Chrome and IE. Safari support is gladly now available.

Install the gem:

gem install watir-webdriver

Example:

require 'watir-webdriver'
browser = Watir::Browser.new :firefox
browser.goto "http://google.com"
browser.text_field(:name => 'q').set("WebDriver rocks!")
browser.button(:name => 'btnG').click
puts browser.url
browser.close

Watirspec

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

Overview

Web Application Testing

Web testing is the name given to software testing that focuses on web applications. Complete testing of a web-based system before going live can help address issues before the system is revealed to the public. Issues such as the security of the web application, the basic functionality of the site, its accessibility to handicapped users and fully able users, as well as readiness for expected traffic and number of users and the ability to survive a massive spike in user traffic, both of which are related to load testing.

Checklist:

  • 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: Test for navigation, contents and other user information for user help.
  • Interface Testing: The main interfaces are: Web server and application server interface Application server and Database server interface. Check if all the interactions between these servers are executed properly.
  • Compatibility Testing: Compatibility of your web site is very important testing aspect, including Browser compatibility, Operating system compatibility, Mobile browsing and Printing options.
  • Performance Testing: Web application should sustain to heavy load. Web performance testing should include both Web Load Testing and Web Stress Testing.

Ruby

Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. According to its authors, Ruby was influenced by Perl, Smalltalk, Eiffel, Ada, and Lisp. It supports multiple programming paradigms, including functional, object-oriented, and imperative. It also has a dynamic type system and automatic memory management.

Features:

  • Thoroughly object-oriented with inheritance, mixins and metaclasses
  • Dynamic typing and duck typing
  • Everything is an expression (even statements) and everything is executed imperatively (even declarations)
  • Succinct and flexible syntax that minimizes syntactic noise and serves as a foundation for domain-specific languages
  • Dynamic reflection and alteration of objects to facilitate metaprogramming
  • Lexical closures, iterators and generators, with a unique block syntax
  • Literal notation for arrays, hashes, regular expressions and symbols
  • Embedding code in strings (interpolation)
  • Four levels of variable scope (global, class, instance, and local) denoted by sigils or the lack thereof
  • Strict boolean coercion rules (everything is true except false and nil)
  • Built-in support for rational numbers, complex numbers and arbitrary-precision arithmetic
  • Custom dispatch behavior (through method_missing and const_missing)
  • Native threads and cooperative fibers (fibers are 1.9/YARV feature)
  • Initial support for Unicode and multiple character encodings (no ICU support)
  • Centralized package management through RubyGems
  • Large standard library, including modules for YAML, JSON, XML, CGI, OpenSSL, HTTP, FTP, RSS, curses, zlib, and Tk

Installation

Installing Behind an Authenticating Proxy

Watir drivers are packaged as gems, Ruby libraries that can be installed over the internet. If installing behind an authenticating proxy, first set the HTTP_PROXY environment variable:

Windows:

set HTTP_PROXY=http://username:password@your.proxy.com:80

Linux:

export http_proxy=http://username:password@your.proxy.com:80

Install Ruby on Linux

Use your relevant package manager to update or install Ruby with the following at a terminal window:

sudo apt-get install ruby  # for Ubuntu / Debian users
sudo yum install ruby      # for Red Hat / Fedora users

Update RubyGems on Linux

Make sure you have the latest rubygems version 1.3.7 or above installed. Use your relevant package manager to install the latest version with the following at a terminal window:

sudo apt-get install rubygems # for Ubuntu / Debian users
sudo yum install rubygems  # for Red Hat / Fedora users

Install a HTML Inspector

To use Watir effectively, you’ll need to be able to browse through the structure of your application’s HTML pages. These tools help you do that.

  • On Firefox, use Firebug
  • On Internet Explorer, use the IE Developer Toolbar

Installing or Updating RubyGems

While updating RubyGems, or installing any of the gems, you can use the following additional flags:

--no-rdoc --no-ri

For example:

gem update --system --no-rdoc --no-ri
gem install watir --no-rdoc --no-ri

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