You are on page 1of 24

Introduction to Web

Application Testing in Ruby


(WATiR)
Presented by Justin Long
My Introduction
For those of you that may not know me
• I’m currently a software architect for
Teleperformance USA
• I was recently a developer for 1-800
Contacts that focuses primarily on
their web site
• I’m part of the leadership of the Utah
.Net Users Group.
• I was born on Mac OS, raised by open
source and adopted by Microsoft.
Slides and Demo code
• My slides and demo code will be
available online at:
http://www.dukk.org/downloads.aspx
so don’t worry if you miss any links
What is WATiR?
• Web Application Testing In Ruby

• It is library for the Ruby scripting


language used to automate Internet
Explorer

• It can be used to test all types of


web applications (ASP.Net, JSP, PHP,
Rails, etc…)
Setting up WATiR
• Download the Ruby and WATiR one-click
installers from: http://wtr.rubyforge.org/

• Steps:
1. Install Ruby (I’ll be using version: 1.8.6-25)
2. Install WATiR (I’ll be using version: 1.4.1)

You can alternatively install WATiR though


GEM by typing “gem install watir” at
a command line after Ruby has been
installed
Learning WATiR
I will only be chipping away at the
surface in this presentation. I would
highly recommend checking out the
documentation section online at
http://wtr.rubyforge.org/

And for those of you that are new to


Ruby the best place to learn is
probably at
http://www.ruby-doc.org/
Development Environments
(IDE’s) for Ruby
• I would recommend using one of the many
Ruby IDE’s for your WATiR scripts. Here’s a
list of some of your selection:

– ScITE (Free)
• In the Ruby Windows installer

– Komodo IDE ($295) / Komodo Edit (Free)


• http://www.activestate.com/

– Ruby In Steel (Free - $199) (Add-on to VS.Net


2k5)
• http://www.sapphiresteel.com/
Using WATiR
• Using the WATiR API is very easy. Just
reference the WATiR API using the
“require” keyword and start coding

require ‘watir’
include Watir

ie =
IE.start(“http://www.dukk.org/”)

The Watir::IE Class
• This is the heart and sole of WATiR
(from the users point of view)
• Contains all the methods needed to
create, navigate and “probe” the IE
browser window
Demo: Quick lap around
my site
Using Ruby’s Interactive
Command Interpreter (IRB)
Use it to:
• Attach to IE windows and quickly identify
browser objects
• Run quick experiments to see if things will
work in your tests

• irb(main):001:0> require ‘watir’


• irb(main):002:0> ie =
Watir::IE.attach(:title, “My Page")
• irb(main):003:0> ie.show_all_objects
Demo: Using IRB
• Run IRB from a command line by
typing “irb”
Using The Ruby Unit
Testing API
• Create a class that extends
Test::Unit::TestCase
• Defined test methods by prefixing them
with “test”
• Setup your assertions
class MyTest < Test::Unit::TestCase
def test_MyPageTest_01
assert(true, “Always Passes”)
end
def test_MyPageTest_02
assert(false, “Always Fails”)
end
end
TestCase Startup &

Teardown
Use the startup method to get you to the page you
want to test when state matters
• Use the teardown method to clean up, close IE, etc…

def setup()
@ie = Watir::IE.start(“http://myapp.com/”)
@ie.link(:text, “Login”).click()
end
def teardown()
@ie.close()
end
Application Data
• I do not recommend trying to
manage the state of your
applications data using the startup
and teardown methods if it can be
avoided!

• Use a test data source when ever


possible that will allow you to simply
rollback all the data at once
Demo: Testing my site using
the Unit Test API
Introducing FireWATiR
• FireWATiR is a spin off of the WATiR project
that uses Firefox instead of IE

• Should eventually work with minimal code


changes from normal WATiR scripts

 Should eventually be platform


independent as long as Firefox works in
that platform (Though it may not be
supported or documented)
Current limitations of
FireWATiR
• Object collections are missing
– No links, buttons, text_fields, etc…

• It currently uses the windows registry


to know how to launch Firefox so
running it on other platforms will be
difficult

Hopefully this stuff will be fixed in


the near future.
Setting up FireWATiR
• Download the gem and JSSh XPI from:
http://code.google.com/p/firewatir/

• Install JSSh XPI (add-in) for Firefox


– This is how FireWATiR talks to Firefox

• Install the FireWATiR gem


– This gem isn’t on rubyforge.org so run the
gem command from the same directory as
the .gem file you downloaded
Using FireWATiR
• Basically the same as WATiR only IE
becomes Firefox

require ‘firewatir’
include FireWatir

ff =
Firefox.start(“http://www.dukk.org/”)

Demo: Quick Demo of
FireWATiR
Questions / Comments?
Contact Information
• Feel free to contact me if you have
questions, comments or suggestions

– Web: http://www.dukk.org/

– Email: dukk@dukk.org
Thanks to our Sponsors

You might also like