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 as 'water', is an automated test toolkit which uses the Ruby scripting language for automated tests to be developed and run against a web browser. <ref>http://www.httpwatch.com/rubywatir/</ref>It allows users to write tests that are easy to read and maintain, also simple and flexible. Watir was first 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> which is a free, open source Ruby library that can test web applications written in any language<ref>https://rubygems.org/gems/watir-classic</ref> and drive a web browser the same way an end user would. It clicks links, fills in forms, presses buttons. Watir can also check results, such as whether expected text appears on the page or not, or whether a control is enabled. <ref>http://watir.com/</ref>

Watir project consists of several smaller projects, including watir-classic, watir-webdriver and watirspec.

Watir-classic

Watir-classic is a Watir driver for automating only Internet Explorer on Windows.<ref>https://github.com/watir/watir-classic</ref>

Watir-classic is able to work on Internet Explorer 8, 9 and 10<ref>http://rubydoc.info/gems/watir-classic/3.7.0/frames</ref>, and is supported by Ruby 1.8.7, 1.9.3 and 2.0.0.

Watir-webdriver

Watie-webdriver is an implementation built on WebDriver's Ruby bindings. It supports not only the Internet Explorer, but also namely Firefox, Chrome and Safari,<ref>https://github.com/watir/watir-webdriver</ref>which is a main difference compared with watir-classic.

Watirspec

Watirspec combines best features of Watir RSpec and Ruby for browser-based functional testing.<ref>https://rubygems.org/gems/watirspec</ref>

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

Overview

Web Application Testing

Web testing is a 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 basic functionality of the site, the security of the web application, 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.<ref>http://en.wikipedia.org/wiki/Web_testing</ref>

Web Testing Checklist:<ref>http://www.softwaretestinghelp.com/web-application-testing/</ref>

  • 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, as well as Cookie testing.
  • Usability Testing: Test for navigation, contents and other user information for user help.
  • Interface Testing: Test for main interfaces, such as Web server, application server interface and Database server interface. Check whether all the interactions between these servers are executed properly or not.
  • Compatibility Testing: Test for the compatibility of your web site, which is a very critical testing aspect, including Browser compatibility, Operating system compatibility, Mobile browsing and Printing options compatibility.
  • Performance Testing: Test for web application performance, which should include both Web Load Testing and Web Stress Testing.

Ruby

Ruby is a dynamic, designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan, is a reflective, object-oriented, general-purpose programming language. Like other programming languages, Ruby gives you the power to connect to databases, read data files and spreadsheets, export XML, and structure your code as reusable libraries. Unlike other programming languages, Ruby is concise and often a joy to read.

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.<ref>http://en.wikipedia.org/wiki/Ruby_(programming_language)</ref>

Several Features:<ref>http://www.starkdigital.net/ruby-programming</ref>

See More Features.

Installation

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

After everything required installed, we can run the watir tests in several ways. Below three ways are listed, and each of them is designed to work on the live Watir demo form: http://bit.ly/watir-example.

Run in .rb

First of all, let's see how to conduct the test in ruby on rails. The example was based on the example listed on a google form, it demonstrated watir's functionality such as entering text into a text field, clicking a button like a user does, checking to see if a page contains text and so on. Below is the step-by-step command to finish the test in .rb file, which means you can simply save these commands in a .rb file and run it with ruby.

Example 1: <ref>http://watir.com/examples/</ref>

  • 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

text-box1.png

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.

Example 2: <ref>http://watir.com/frameworks/</ref>

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 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.

Example 3: <ref>http://watir.com/frameworks/</ref> 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

There are some other framworks will do similar work as watir does, for example, Cucmber, Capybara and RSpec. But in comparison, they have different features and are usually used in conjecture.

Put simply, Watir is a browser driver. Lightweight and simple to use, Watir excels at automating modern browsers. Built in Ruby, it does not have an IDE of it's own, but it works well with other testing frameworks and solutions in the Ruby ecosystem.

Cucumber lets software development teams describe how software should behave in plain english, it is designed to allow you to execute automated integration tests. It is a tool for implementing Behavior Driven Development.It can work nicely in conjecture with Capybara and RSpec.

Capybara is a library written in the Ruby programming language which makes it easy to simulate how a user interacts with your application. It can talk with many different drivers which execute tests through the same clean and simple interface, including Selenium, Webkit or pure Ruby drivers.<ref>http://jnicklas.github.io/capybara/</ref>

RSpec helps you do the TDD(Test-Driven Development) part of the whole development (combines Test-Driven Development, Domain Driven Design, and Acceptance Test-Driven Planning), focusing on the documentation and design aspects of TDD. A combination of RSpec and Watir makes really readable test scripts.<ref>http://www.ibmwcs.com/2010/08/testing-with-rspec-cucumber-selenium.html</ref>

Pros and Cons

Pros

  • Rich API, which is pretty easy and once the users get the basics they can just guess how to write the code without needed alot of documentation.
  • Tests can often be run through alternative tools, such as watir-webdriver, celerity and more, which are all more-or-less support the watir API. This means that if the users write their framework carefully, they would not be locked into watir.
  • Watir-webdriver does work in both ruby and jruby.
  • Don't have much issue dealing with AJAX, watir autumatically knows when the page finished loading.
  • Typically runs faster than a comparable selenium test.
  • Better support for headless browsers, like Celerity.
  • Multi browser (& OS) support

Cons

  • Each browser's implementation is a little bit different, and the driver for each browser is written independently. This means inconsistent test results between browsers, which could be an issue sometime.
  • API is ruby only, users have to learn Ruby first.
  • Doesn't seem to be as widely used as selenium, a lil bit harder to find pretty good engineers.

Reference

<references></references>

Further Reading

1. An virtual experience on Watir, see how watir works in your eyes. https://www.youtube.com/watch?v=FxQXQGpImAo