Web Application Testing In Ruby

W A eb pplication T esting I R n 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 Excellent Support learn) • 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. 2. 3. Install Ruby: Use the Ruby One-Click Installer for Windows. Install the latest Watir “Gem” 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 Elements Common Functions of the IE object: TextBox IE.text_field(how, what) Button DropDownList CheckBox RadioButton HyperLink Form Frame IE.button(how, what) IE.select_list(how, what) IE.checkbox(how, what) IE.radio(how, what) IE.link(how, what) IE.form(how, what) IE.frame(how, what) And many, many more (div, label, image, etc…)… How‟s and What‟s How‟s tell your method how to find the element you‟re looking for. What‟s tell your method the value for “how”. How‟s: • :name • :id • :index • :value • :text • :title What‟s: • String value of “how” • /Regular expression/ 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' Require „util‟ #includes Ruby's test case functionality #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.

Related docs
Ruby
Views: 154  |  Downloads: 8
Ruby-On-Rails
Views: 10  |  Downloads: 2
Introduction to Ruby on Rails
Views: 324  |  Downloads: 25
DanLynn-Resume-Ruby
Views: 19  |  Downloads: 0
Ruby on Rails Cheat Sheet
Views: 795  |  Downloads: 65
ruby
Views: 413  |  Downloads: 28
The Road to Ruby.pdf
Views: 131  |  Downloads: 13
Testing for web
Views: 9  |  Downloads: 6
An Introduction to Ruby on Rails
Views: 33  |  Downloads: 6
Web Application Penetration Testing (WAPT)
Views: 204  |  Downloads: 17
premium docs
Other docs by amberp
Batmobile Dashboard
Views: 680  |  Downloads: 9
alspaugh-all
Views: 567  |  Downloads: 4
Stock Subscription Agreement
Views: 1003  |  Downloads: 52
Accenture Partnership Agreement
Views: 290  |  Downloads: 13
Settlement of Disputed Account
Views: 154  |  Downloads: 4
adopt215
Views: 112  |  Downloads: 0
Notice Calling Meeting of Board of Directors
Views: 271  |  Downloads: 6
Hazard communication program package
Views: 326  |  Downloads: 4
Background Check Permission (Simple)
Views: 351  |  Downloads: 24