Web Testing with Ruby and Watir

Document Sample
Web Testing with Ruby and Watir Powered By Docstoc
					Web Testing with Ruby and
          Watir
What is Watir?



      Web Application Testing In Ruby
Open Source Library (gem)
Watir drives Internet Explorer



Watir Library              Internet
                COM
  (gem)                    Explorer
How does Watir differ from other
web test methods?
• Is not based on object position
• Not an HTTP testing method
The Ruby Language

• Dynamic
• Easy to understand syntax
• Easy to use interpreter
• Object Oriented
• Powerful class libraries
  Enough Already!!

Let’s get to the Code!!
Webpage Example 1




<Html>
<head>
  <title>Boise Code Camp - Testing with Ruby and Watir</title>
</head>

<body bgcolor="antiquewhite">

<img src="BoiseCodeCamp-round.gif">
<h2>Version 2.0</h2>
<br>
<h1>Welcome to Boise Code Camp 2007</h1>
<h3>Testing Web Applications with Ruby and Watir</h3>

</body>
</html>
Watir Test Example 1




 # Load the library and Create Reference to Library object
 require 'watir'
 include Watir

 # Create a new IE object
 ie = IE.new

 # Go to the web site
 ie.goto( "http://localhost/Page1.asp“ )

 # Check for text on the web page
 if ie.contains_text( "Testing Web Applications with Ruby and Watir“ )
   puts "Test Passed: Page 1 Validated"
 end
Testing Links on a Web Page
    WebPage Example 2

…

<img src="BoiseCodeCamp-round.gif">
<h2>Version 2.0</h2>
<br>
<h1>Welcome to Boise Code Camp 2007</h1>
<h3>Testing Web Applications with Ruby and Watir</h3>

<br>
<a id="1" name="msnlink" href="http://www.msn.com">MSN</a><br>
<a id="2" name="yahoolink" href="http://www.yahoo.com">Yahoo</a><br>
<a id="3" name="slashdotlink" href="http://slashdot.org/">Slashdot</a><br>
<a id="4" name="ecode" href="http://www.elegantcode.com/">Elegantcode</a><br>
<a id="5" name=“bplink" href="http://www.pettichord.com/">Bret
Pettichord</a><br>

</body>
</html>
Watir Test Example 2

# Load the library and Create Reference to Library object
require 'watir'
include Watir

# Create a new IE object
ie = IE.new

# Go to the web site
ie.goto( "http://localhost/Page2.asp“ )

# Verify the existance of a link
if ie.link( :text, "Slashdot“ ).exists?
  puts "Test Passed: Link with Slashdot as text found"
end

if ie.link( :text, /Code/ ).exists?
  puts "Test Passed: Link with the word code found"
end
Watir Test Example 2 cont.

if ie.link( :url, /yahoo/ ).exists?
  puts "Test Passed: Link with the word yahoo found"
end

puts "Display the text for link 1: " + ie.link( :index, 1 ).text

if ie.link( :id, "1“ ).text == "MSN"
  puts "Test Passed: Link with id 1 and text containing MSN found"
end

if ie.link( :index, 1 ).href == "http://www.msn.com")
  puts "Test Passed: Link index 1 has http://www.msn.com as href"
end

# Click on a link
ie.link( :text, "Slashdot“ ).click
if ie.link( :text, "Subscribe“ ).exists?
  puts "Test Passed: Subscribe Link on slashdot found"
end
Text Boxes & Buttons
WebPage Example 3

<Html>
<head>
  <title>Boise Code Camp - Testing with Ruby and Watir</title>
</head>

<body bgcolor="antiquewhite">

<img src="BoiseCodeCamp-round.gif">
<h2>Version 2.0</h2>
<br>
<h1>Welcome to Boise Code Camp 2007</h1>
<h3>Testing Web Applications with Ruby and Watir</h3>

<p>Enter something interesting in the text box and click a button!</p>
<form name="input" action="page3next.asp" method="post">
  <p><Input type="text" name="searchbox"></p>
  <p><Input type="submit" value="submit"></p>
</form>

</body>
</html>
Watir Test Example 3

# Load the library and Create Reference to Library object
require 'watir'
include Watir

# Create a new IE object
ie = IE.new

# Go to the web site
ie.goto( "http://localhost/Page3.asp“ )

# Add text to a text box
ie.text_field( :name, "searchbox“ ).set( "Information“ )

# Click on a button
ie.button( :value, "submit“ ).click
Watir Test Example 3 cont.



# Check that the form exists
if ie.form( :name, "input“ ).exists?
  puts "Test Passed: Form input found"
end

# Click the button based the form it is in
ie.form( :name, "input“ ).button( :value, "submit“ ).click
Images
  WebPage Example 4

<Html>
<head>
  <title>Boise Code Camp - Testing with Ruby and Watir</title>
</head>

<body bgcolor="lightsteelblue">

<img src="BoiseCodeCamp-round.gif">
<h2>Version 2.0</h2>
<br>
<h1>Welcome to Boise Code Camp 2007</h1>
<h3>Testing Web Applications with Ruby and Watir</h3>

<img name="watergraphic" src="112_Water_Faucet_ph.jpg" alt="Image of water
faucet"/>
<a href="page3next.asp"><img alt="Image Button" src="bgdb.gif"/></a>

</body>
</html>
Watir Web Test Example 4

# Load the library and Create Reference to Library object
require 'watir'
include Watir

# Create a new IE object
ie = IE.new

# Go to the web site
ie.goto( "http://localhost/Page4.asp“ )

# Check that the image exists
if ie.image( :src, /Water/ ).exists?
  puts "Test Passed: Found a graphic with the word Water in the source filename"
end
Watir Web Test Example 4 cont

if ie.image( :name, "watergraphic“ ).exists?
  puts "Test Passed: Found a graphic with the name watergraphic"
end

if ie.image( :alt, "Image of water faucet“ ).exists?
  puts "Test Passed: Found a graphic with Image of water faucet as alt text"
end

# Click the image button
ie.image( :src, /bgdb/ ).click

# Check the contents of the new page
if ie.contains_text( "I hope you Enjoy Code Camp!“ )
  puts "Test Passed: Text found"
end
Check Boxes
WebPage Example 5

<Html>
<head>
  <title>Boise Code Camp - Testing with Ruby and Watir</title>
</head>

<body bgcolor="cadetblue">

<img src="BoiseCodeCamp-round.gif">
<h2>Version 2.0</h2>
<br>
<h1>Welcome to Boise Code Camp 2007</h1>
<h3>Testing Web Applications with Ruby and Watir</h3>

<form action="page3next.asp" method="post">
  <p><Input type="checkbox" name="checkthebox"></p>
  <p><Input type="submit" value="submit"></p>
</form>

</body>
</html>
Watir Test Example 5

…

# Go to the web site
ie.goto("http://localhost/Page5.asp")

…

# Check that the checkbox exists
if ie.checkbox( :name, "checkthebox").exists?
  puts "Test Passed: The check box exists"
end

# Check the checkbox
ie.checkbox( :name, "checkthebox“ ).set

# Un-Check the checkbox
ie.checkbox( :index, 1 ).set( false )
Radio Buttons
WebPage Example 6

<Html>
<head>
  <title>Boise Code Camp - Testing with Ruby and Watir</title>
</head>

<body bgcolor="antiquewhite"">

<img src="BoiseCodeCamp-round.gif">
<h2>Version 2.0</h2>
<br>
<h1>Welcome to Boise Code Camp 2007</h1>
<h3>Testing Web Applications with Ruby and Watir</h3>

<form action="page3next.asp" method="post">
  <p>Option 1<Input type="radio" name="radiobutton1"></p>
  <p>Option 2<Input type="radio" name="radiobutton2"></p>
  <p>Option 3<Input type="radio" name="radiobutton3"></p>
  <p><Input type="submit" value="submit"></p>
</form>

</body>
</html>
Watir Test Example 6

…

# Go to the web site
ie.goto("http://localhost/Page6.asp")

…

# Check that the radio button exists
if ie.radio( :name, "radiobutton1").exists?
  puts "Test Passed: Radio button found"
end

# Change the state of the Radio Buttons
ie.radio( :name, "radiobutton1” ).set
ie.radio( :name, "radiobutton3” ).set
Selection Boxes
Webpage Example 7

<Html>
<head>
  <title>Boise Code Camp - Testing with Ruby and Watir</title>
</head>

<img src="BoiseCodeCamp-round.gif">
<h2>Version 2.0</h2>
<h1>Welcome to Boise Code Camp 2007</h1>
<h3>Testing Web Applications with Ruby and Watir</h3>

<form action="page3next.asp" method="post">
  <select name="dropdown">
     <option value="1" selected >Chicken
     <option value="2">Beef
     <option value="3">Vegetarian
  </select>
  <p><Input type="submit" value="submit"></p>
</form>

</body>
</html>
Watir Test Example 7

# Load the library and Create Reference to Library object
require 'watir'
include Watir

# Create a new IE object
ie = IE.new

# Go to the web site
ie.goto("http://localhost/Page7.asp")

# Check for text on the web page
if (ie.contains_text("Testing Web Applications with Ruby and Watir"))
  puts "Text found"
end

# Interact with the drop down box
ie.select_list(:name, "dropdown").clearSelection
ie.select_list(:name, "dropdown").select("Beef")
ie.select_list(:name, "dropdown").select_value("3")
Attaching to another IE Instance
Webpage Example 9

<Html>
<head>
   <title>Boise Code Camp - Testing with Ruby and Watir</title>
<script type="text/javascript">
<!--
function myPopup() {
   window.open( "http://www.google.com/" )
}
//-->
</script>
</head>

…

<form>
<input type="button" onClick="myPopup()" value="POP!">
</form>

</body>
</html>
Watir Test Example 9

……
# Go to the web site
ie.goto("http://localhost/Page9.asp")

…

ie.button( :value, "POP!“ ).click

ie2 = Watir::IE.attach( :title, /Google/ )

ie2.text_field( :name, "q“ ).set( "Ruby“ )
ie2.button( :name, "btnG“ ).click

ie2.link( :text, "Ruby Programming Language“ ).click

if (ie2.contains_text( "Ruby is…“ ))
  puts "Test Passed: Found the Programming Ruby page"
end

# Close the second instance
ie2.close
Tables
Watir Test Example 11

…..
# Go to the web site
ie.goto( "http://localhost/Page11.asp“ )

# Click on the button
ie.button( :value, "Click Me!“ ).click

# Check that the table is there
if ie.table( :id, "booktable“ ).exists?
  puts "Test Passed: Book Table found"
end

# Check the tables
ie.show_tables
Watir Test Example 11 cont.

ie.tables.each do |t|
  t.each do |r|
    r.each do |c|
      puts c.text
    end
  end
end

if ie.tables[1][1][1] == "Book Title"
  puts "Test Passed: Book Title found in table cell"
end

if ie.tables[1][1][2] == "Author"
  puts "Test Passed: Author found in table cell"
end
Unit tests
Webpage Example 12 cont.

<Html>
<head>
  <title>Boise Code Camp - Testing with Ruby and Watir</title>
</head>

<body bgcolor="antiquewhite">

<img src="BoiseCodeCamp-round.gif">
<h2>Version 2.0</h2>
<br>
<h1>Welcome to Boise Code Camp 2007</h1>
<h3>Testing Web Applications with Ruby and Watir</h3>
<br>

<a id="1"   name="msnlink" href="http://www.msn.com">MSN</a><br>
<a id="2"   name="yahoolink" href="http://www.yahoo.com">Yahoo</a><br>
<a id="3"   name="slashdotlink" href="http://slashdot.org/">Slashdot</a><br>
<a id="4"   name="elegantcode" href="http://www.elegantcode.com/">Elegant Code</a><br>
<a id="5"   name="pettichordlink" href="http://www.pettichord.com/">Bret Pettichord</a><br>

<p>Fill out this form!</p>
<form action="page3next.asp" method="post">
  <p>Enter Stuff: <Input type="text" name="searchbox"></p>
  <p>Check the Box: <Input type="checkbox" name="checkthebox"></p>
  <p>Option 1<Input type="radio" name="radiobutton1"></p>
  <p>Option 2<Input type="radio" name="radiobutton2"></p>
  <p>Option 3<Input type="radio" name="radiobutton3"></p>
  <select name="dropdown">
     <option value="1" selected >Chicken
     <option value="2">Beef
     <option value="3">Vegetarian
  </select>
  <p><Input type="submit" value="submit"></p>
</form>


</body>
</html>
Watir Test Example 12 cont.

require 'watir'
include Watir
require 'test/unit'

class TC_ExampleTestCase < Test::Unit::TestCase

 def test_1_CheckText

   $ie.goto( "http://localhost/Page12.asp")
   # Check for text on the web page
   assert( $ie.contains_text( "Testing Web Applications with Ruby and Watir“ ) )

 end

 def test_2_Links

   $ie.goto("http://localhost/Page12.asp")
   # Check for some links
   assert( $ie.link( :text, "Slashdot“ ).exists? )
   assert( $ie.link( :text, /Code/ ).exists? )
   assert( $ie.link( :url, /yahoo/ ).exists? )
   assert( $ie.link( :id, "1“ ).text == "MSN“ )

 end

 def test_3_ClickLink

   $ie.goto( "http://localhost/Page12.asp“ )
   # Click on a link
   $ie.link( :text, "Slashdot” ).click
   assert( $ie.link(:text, "Subscribe“ ).exists? )
   $ie.back

 end

end
IRB – Ruby Interactive Shell
Now go forth and test…..
Resources

• www.openqa.org/watir
  – Downloads
  – Installation Instructions
  – Active forum
Thank you