CSC/ECE 517 Fall 2014/ch1a 19 mx: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(170 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Watir'''
'''Watir'''
----
= Introduction=
Watir('''W'''eb '''A'''pplication '''T'''est '''i'''n '''R'''uby), 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 [http://en.wikipedia.org/wiki/Internet_Explorer Internet Explorer], [http://en.wikipedia.org/wiki/FirefoxMozilla Firefox|Firefox], [http://en.wikipedia.org/wiki/Chrome Google Chrome|Chrome], [http://en.wikipedia.org/wiki/Opera Opera (web browser)|Opera] and [http://en.wikipedia.org/wiki/Safari Safari (web browser)|Safari], and is available as a [http://en.wikipedia.org/wiki/RubyGems 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.


== Introduction==
== Watir-webdriver==
Watir('''W'''eb '''A'''pplication '''T'''est '''i'''n '''R'''uby), 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]], [[Mozilla Firefox|Firefox]], [[Google Chrome|Chrome]], [[Opera (web browser)|Opera]] and [[Safari (web browser)|Safari]], and is available as a [[RubyGems]] gem. 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 people do.
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.


Watir project consists of several smaller projects. The most important ones are watir-classic, watir-webdriver and watirspec.
== Watirspec ==
Watirspec combines best features of Watir [http://en.wikipedia.org/wiki/RSpec RSpec] and Ruby for browser-based functional testing.<ref>https://rubygems.org/gems/watirspec</ref>


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


=== Watir-webdriver===
= Overview =


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


== Overview ==
== Web Application Testing ==
[http://en.wikipedia.org/wiki/Web_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>


=== 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, as well as Cookie 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:  Test for navigation, contents and other user information for user help.
*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.  
*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: Compatibility of your web site is very important testing aspect, including Browser compatibility, Operating system compatibility, Mobile browsing and Printing options.  
*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: Web application should sustain to heavy load. Web performance testing should include both Web Load Testing and Web Stress Testing.
*Performance Testing: Test for web application performance, which should include both Web Load Testing and Web Stress Testing.


=== Why Ruby ===
== 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.


== Installation ==
According to its authors, Ruby was influenced by [http://en.wikipedia.org/wiki/Perl Perl], [http://en.wikipedia.org/wiki/Smalltalk Smalltalk], [http://en.wikipedia.org/wiki/Eiffel Eiffel], [http://en.wikipedia.org/wiki/Ada Ada], and [http://en.wikipedia.org/wiki/Lisp 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>


== Make It Run ==
'''Several Features:'''<ref>http://www.starkdigital.net/ruby-programming</ref>
All examples are designed to work on the live Watir demo form: http://bit.ly/watir-example.
*Thoroughly object-oriented with inheritance, mixins and metaclasses
*Dynamic typing and duck typing
*Large standard library, including modules for [http://en.wikipedia.org/wiki/YAML YAML], [http://en.wikipedia.org/wiki/JSON JSON], [http://en.wikipedia.org/wiki/XML XML], [http://en.wikipedia.org/wiki/CGI CGI], [http://en.wikipedia.org/wiki/OpenSSL OpenSSL], [http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol HTTP], [http://en.wikipedia.org/wiki/File_Transfer_Protocol FTP], [http://en.wikipedia.org/wiki/RSS RSS], [http://en.wikipedia.org/wiki/Curse_(disambiguation) curses], [http://en.wikipedia.org/wiki/Zlib zlib], and [http://en.wikipedia.org/wiki/Tk_(software) Tk]
*Dynamic reflection and alteration of objects to facilitate [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming]
[http://www.starkdigital.net/ruby-programming See More Features.]


=== Run in .rb ===
= Installation =
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:
== 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:
<pre>
require 'rubygems'
require 'rubygems'
</pre>


Including Watir gem to drive Internet Explorer on Windows
*Including Watir gem to drive Internet Explorer on Windows
<pre>
require 'watir'
require 'watir'
</pre>


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


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


Setting a text field
*Setting a text field


[[File:Text-field.png]]
[[File:Text-field.png]]
Line 58: Line 115:
</pre>
</pre>


Setting a multi-line text box
*Setting a multi-line text box


[[File:text-box1.png]]
[[File:text-box1.png|text-box1.png]]
<pre>
<pre>
browser.text_field(:name => 'entry.1.single').set "I come here from Australia. \n The weather is great here."
browser.text_field(:name => 'entry.1.single').set "I come here from Australia. \n The weather is great here."
</pre>
</pre>


Setting and clearing a radio button
*Setting and clearing a radio button


[[File:3.radio-buttons1.png]]
[[File:3.radio-buttons1.png]]
Line 73: Line 130:
</pre>
</pre>


Setting and clearing check boxes
*Setting and clearing check boxes


[[File:4.checkboxes.png]]
[[File:4.checkboxes.png]]
Line 82: Line 139:
</pre>
</pre>


 
*Clearing, getting and selecting selection list values
 
 
Clearing, getting and selecting selection list values


[[File:5.selection-list.png]]
[[File:5.selection-list.png]]
Line 95: Line 149:
</pre>
</pre>


Clicking a button
*Clicking a button


[[File:6.button.png]]
[[File:6.button.png]]
Line 102: Line 156:
</pre>
</pre>


Checking for text in a page
*Checking for text in a page
<pre>
<pre>
puts browser.text.include? 'Your response has been recorded.'
puts browser.text.include? 'Your response has been recorded.'
</pre>
</pre>


Checking the title of a page
*Checking the title of a page
<pre>
<pre>
puts browser.title == 'Thanks!'
puts browser.title == 'Thanks!'
</pre>
</pre>


=== Run in Rspec ===
== 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.[http://rspec.info/]
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.[http://rspec.info/ More Info.]
 
''Example 2: <ref>http://watir.com/frameworks/</ref> ''


1. Install
1. Install
Line 149: Line 205:
</pre>
</pre>


=== Run in Rspec ===
== 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.[http://cukes.info/]
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.[http://cukes.info/ More Info.]
 
''Example 3: <ref>http://watir.com/frameworks/</ref>''


1. Install
1. Install
Line 159: Line 217:


2. Example
2. Example
<br>
 
<pre>
<pre>
cucumber features
cucumber features
Line 197: Line 255:
</pre>
</pre>


= Comparison with Other Tools =
There are some other framworks will do similar work as watir does, for example, [http://en.wikipedia.org/wiki/Cucumber_(software) Cucmber], [http://en.wikipedia.org/wiki/Capybara Capybara] and RSpec[http://en.wikipedia.org/wiki/RSpec RSpec]. But in comparison, they have different features and are usually used in conjecture.
In software engineering, behavior-driven development (abbreviated BDD) is a software development process based on test-driven development (TDD). <ref>http://en.wikipedia.org/wiki/Behavior-driven_development</ref> It's very important in software testing. The relationship between Watir and Cucmmber is usually designing a framework to test cucumber scenarios using Watir tool. [http://qastrategies.blogspot.com/2012/06/bdd-framework-to-test-web-application.html See Example]
The detailed characteristics and relationships are:
*Watir<br>
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.


=== Run in Cucumber===
*Cucumber<br>
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.[http://cukes.info/]
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.


1. Install
*Capybara<br>
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<br>
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>
 
 
:''Here's an video tutorial from Youtube introducing how to test with Ruby Cucumber RSpec Capybara Watir WebDriver Testing.[https://www.youtube.com/watch?v=jPH5Eiz32N0 More Info]''
 
= Pros and Cons =
== Pros ==
* No external server needed to run a test.  Just run "ruby test.rb" and it goes, this simplifies any scripts used to execute the tests through continuous integration servers like [http://en.wikipedia.org/wiki/Hudson_%28software%29 Hudson] or [http://en.wikipedia.org/wiki/Bamboo_%28software%29 Bamboo] or whatever.<ref>http://www.quora.com/What-are-the-pros-and-cons-of-different-front-end-web-testing-frameworks-such-as-Selenium-or-Watir</ref>
 
* 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, [http://watirmelon.com/2009/08/31/celerity-first-impressions/ 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 [http://en.wikipedia.org/wiki/JRuby jruby].
 
* Don't have much issue dealing with [http://en.wikipedia.org/wiki/Ajax_%28programming%29 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
 
*Provides an "attach" method which lets users programmatic access both the original domain window and the linked domain window from the same test.<ref>http://onestopqa.blogspot.com/2012/09/pros-cons-of-watir-selenium-or-sahi.html</ref>
 
== 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.


<pre>
* Doesn't seem to be as widely used as selenium, a lil bit harder to find pretty good engineers.
gem install cucumber
</pre>


2. Example
* The task of porting Watir to a new browser isn’t an easy one, and the task of keeping every port of Watir in sync, with the same API, is difficult. <ref>http://watirmelon.com/2010/04/10/watir-selenium-webdriver/</ref>


<pre>
= Reference =
cucumber features
<references></references>
</pre>
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'''
= Further Reading =
require "watir-webdriver"
1. An virtual experience on Watir, see how watir works in your eyes. https://www.youtube.com/watch?v=FxQXQGpImAo
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 ==
2. Get started with watir in one minute. https://testwisely.com/testwise/docs/tutorials/watir


== Reference ==
3. Tutorial on Ruby Cucumber RSpec Capybara Watir WebDriver Testing, know how they work here: https://www.youtube.com/watch?v=jPH5Eiz32N0

Latest revision as of 03:44, 26 September 2014

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 RSpecRSpec. But in comparison, they have different features and are usually used in conjecture.

In software engineering, behavior-driven development (abbreviated BDD) is a software development process based on test-driven development (TDD). <ref>http://en.wikipedia.org/wiki/Behavior-driven_development</ref> It's very important in software testing. The relationship between Watir and Cucmmber is usually designing a framework to test cucumber scenarios using Watir tool. See Example


The detailed characteristics and relationships are:

  • Watir

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

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

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

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>


Here's an video tutorial from Youtube introducing how to test with Ruby Cucumber RSpec Capybara Watir WebDriver Testing.More Info

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

2. Get started with watir in one minute. https://testwisely.com/testwise/docs/tutorials/watir

3. Tutorial on Ruby Cucumber RSpec Capybara Watir WebDriver Testing, know how they work here: https://www.youtube.com/watch?v=jPH5Eiz32N0