name - OpenQA Wiki

bricklayerbelchedInternet και Εφαρμογές Web

5 Φεβ 2013 (πριν από 4 χρόνια και 2 μήνες)

190 εμφανίσεις

W
eb
A
pplication
T
esting
I
n
R
uby

What is Watir?


A free, open
-
source functional testing
tool for web applications.


It is a Ruby library which drives Internet
Explorer the same way people do, clicks
links, fills in forms, and presses
buttons.


Watir also checks results, such as whether
expected text appears on the page.


Because it’s build on Ruby, you have the
power to connect to databases, read data
files, export XML and structure your code
into reusable libraries.

Why use Watir?


Free


Powerful


Simple
(easy to use and learn)


Excellent Support


It uses
Ruby
, a full
-
featured object
-
oriented
scripting language, rather than a
proprietary
vendorscript
.


Don't just take our word for it,
read what our
users are saying
.

What the Users say…


“…WATIR is by far the most complete web testing
framework out here
--
that doesn't cost an arm
and a leg.

And this forum is the most active
and well supported that I have seen. You can
post a question and is just a short period of
time get someone (most of the time the
architects of the framework!) who will steer
you in the right direct, give you a work around
to get over any roadblocks, or add it to the
framework if it doesn't currently exist. That
is the reason I switched and tell everyone
interest (and those not) that Ruby is the way
to go! Save your money, get WATIR!”


What the Users say… …


“…Ruby is an awesome language and Watir is just
too cool.
It does things that other companies
(ie. IBM
/Rational
, Mercury, Segue, et al)
charge thousands and thousands of dollars a
seat for.

Anyway, now that I'm gettin' the hang
of Ruby and Watir, I'm starting to automate
everything I do on the web. Even the simple
things like logging in to web sites, searches,
you name it.”


What the Users say… … …


“I was able to write a few unit tests using
Watir in about 15 minutes

and I’ve looked at
Ruby code for all around 48 hours. Granted, I
have been object
-
oriented programming for the
last four years so it’s not like Ruby looks
Russian to me, but I wasn’t able to figure out
JUnit that quickly by a long shot.”


How do I get started?

1.
Install Ruby: Use the
Ruby One
-
Click Installer
for Windows.

2.
Install the latest Watir “
Gem


3.
Follow this Presentation or Read the
User's
Guide
, documentation, examples and tests. Figure
out your test case, start typing your script,
and run it!



Some Helpful Tools…

Editors:


Scite

(A good light
-
weight editor installed with Ruby
-

ruby
\
scite
\
scite.exe)


RDT

(Eclipse plug
-
in for Ruby, great if you need a
graphical debugger)


Tips for finding/accessing HTML elements:


IE Developer Tool bar
(Lets you explore the HTML
elements behind the visible elements on a web page by
clicking on the elements


FireBug

(Excellent FireFox extension for identifying
HTML elements while writing scripts.)


WATIR::SupportsSubElements
(Watir API for accessing
html elements)



How do I…


…Navigate the browser?


…Find elements on the page?


…Interact with elements on the page?


…Check output on the page?


…Create and use Methods?


…Create formal test cases?

Navigating the Browser

# Always Load the Watir library at the top of your script

require 'watir'


#Start IE and navigate to a given URL


ie = Watir::IE.start('http://news.google.com')




#or..Attach to an existing IE window by title or url

ie = Watir::IE.attach(
:title
,'title')

ie = Watir::IE.attach(
:url
,/regex matching url/)


#Navigate to a different URL

ie.goto("http://mentorweb/")



#Close IE.

ie.close

Finding <HTML> Elements

Common Functions of the IE object:

TextBox

IE.text_field(
how, what
)

Button

IE.button(
how, what
)

DropDownList

IE.select_list(
how, what
)

CheckBox

IE.checkbox(
how, what
)

RadioButton

IE.radio(
how, what
)

HyperLink

IE.link(
how, what
)

Form

IE.form(
how, what
)

Frame

IE.frame(
how, what
)


And many, many more (div, label, image, etc…)…

How’s and What’s

How’s:


:name


:id


:index


:value


:text


:title


What’s:


String value of
“how”


/Regular expression/



How’s tell your method how to find the element you’re looking
for. What’s tell your method the value for “how”.


Interacting with Elements

#Set the text field (or text area) specified name specified value.


ie.text_field(
:name
,'name').set('value')




#Sets the select with to the specified value

ie.select_list(
:name
,'name').select('value')


#Click the button with the specified value (label)

ie.button(
:value
,'value').click



#Clicks the link matching 'text'


ie.link(
:text
,'text').click


#Accessing elements in a "frame" or "iframe"

ie.frame(
:name
,"someFrame").text_field(
:name
,'name').set('value')

Checking Output

#Get the title of the page

ie.title


#Get all the text displayed on the page

ie.text


#Get all the HTML in the body of the page

ie.html


#Return true if ‘text’ is displayed on the page

ie.contains_text('text')

Creating and using Methods

#Here is an example method for logging into a web
application. Its two parameters are a user and password
(both of which have defaults). It returns an instance of
IE

def

login(user=“test_admin”,password = “Password123”)


ie=Watir::IE.start(‘http://someURL.com/login’)


ie.text_field(:name,/user/i).set(user)


ie.text_field(:name,/password/i).set(password)


ie.button(:value,/login/i).click


return

ie

End


#Now we can easily login with the default user..

ie = login

#or with a unique user

ie = login(“Fred”,”flinstone”)


Creating Formal Test Cases

require 'test/unit'
#includes Ruby's test case functionality

Require ‘util’



#Assuming our login method is saved in util.rb


#Test cases are contained within classes which extend Ruby’s base test case
class

class

MyTest < Test::Unit::TestCase


def

setup
#Optional, will be run before each test method.





@ie = login()
#call our login function.



end




def

test_some_link
#Test methods must begin with "test_“



@ie.link(:text,”some_link”).click
#click on some link






#verify that the proper page loaded



assert(@ie.contains_text(“My Some Link Page”))



end




def

teardown
#Optional, will be run after each test method.



@ie.close


end

end

Debugging with IRB


IRB = Interactive Ruby Shell


Command line
-
like interface that allows immediate
running of Ruby script


Great for debugging one line at a time, rather
then having to run through an entire script


Great for testing single lines


Try ruby out using an
online IRB
. This is a great
way to learn ruby basics!

Watir & Ruby Resources



Watir


Watir main site:
http://wiki.openqa.org/display/WTR/



Watir user guide:
wtr.rubyforge.org/watir_user_guide.html


Watir API:
wtr.rubyforge.org/rdoc/index.html


Mailing List:
rubyforge.org/mailman/listinfo/wtr
-
general


Project site:
http://wiki.openqa.org/display/WTR/


User Contributions/examples:
http://wiki.openqa.org/display/WTR/Contributions


Watir FAQ:
http://wiki.openqa.org/display/WTR/FAQ



Ruby


Ruby site:
http://ruby
-
lang.org


Ruby docs:
http://ruby
-
doc.org/


Ruby Quickstart:
ruby
-
lang.org/en/documentation/quickstart/

That’s it!





Note: All information contained in the presentation
was compiled from documentation on the
official
Watir site
, or from it’s
mailing list
.