Lauren's Ruby Cheatsheet
To find out what version of Watir you are running execute the following:
Go to command prompt and type this:
ruby -e 'require "watir"; puts Watir::IE::VERSION
You should get something like this:
1.5.1.1029
To find out what version of ruby you are running type:
ruby -v
To view a list of installed gems on your local system:
gem list – local or gem list
To Install/Uninstall Watir gem:
To Install type:
gem install watir-1[1].5.1.1145.gem Replace watir-1[1].5.1.1145 with the version you wish to install
To Uninstall:
gem uninstall watir
To Search the Mail Archives for answers to your questions:
Go to Google and type the following:
site:rubyforge.org "[wtr-general]" javascript
Replace the word 'javascript' with the search term you are searching for
How to Identify an object when you have 2 or more objects with the same name:
This will click the first link that is found on the page:
ie.link(:text, 'Employees').click
Use this syntax for the second (or more):
ie.link(:text⇒'Employees', index⇒2).click
How to indentify all objects of the same type
This is how you can flash all radio buttons in a form.
ie.form(:index, 1).radios.each { |radio| radio.flash }
Concurrent Tests (using parallel threads)
require 'thread'
require 'watir'
def test_google
ie = Watir::IE.start('http://www.google.com')
ie.text_field(:name, "q").set("pickaxe")
ie.button(:value, "Google Search").click
end
Run the same test three times concurrently in separate browsers
threads = []
3.times do
threads « Thread.new {test_google}
end
threads.each {|x| x.join}
In this example 3 browser connection are open. Are these 3 browser connection open at same time or one after the another.
Per Bret Pettichord: Concurrent threads run in parallel.
Manipulating Strings
1:#!/usr/bin/ruby -w
2:puts "Hello World!\n"
3:
4:# Print a variable
5:test = "human events"
6:puts test
7:
8:# Combining or concatenating two strings
9:puts "1: When in the course of " + test
10:
11:# Printing a value within a String. Interpolation like Perl
12:puts "2: When in the course of #{test}"
13:
14:# Create a value using a here docment
15:heredoc = <<END_OF_STRING
16:-------------------------------
17:Calling the test value again:
18:#{test}
19:End of the text
20:-------------------------------
21:END_OF_STRING
22:
23:puts heredoc
24:
Assertions
- The methods that hold the assertions must have names that start with test
- Test::Unit uses reflection to find tests to run, and only methods whose names start with test are eligible
- You can not use "if" and "assert" together (per comment on watir forum)
- To use "Verify" methods:
require 'watir/assertions'- Verify methods will be counted as assertions in the test unit run without exiting out of the calling method.
| Method Examples |
|---|
assert(ie.contains_text("text"))assert(ie.contains_text(/Hello Friends/i))puts ("Test passed") |
def test_assertButtonassert($ie.button(:caption, "Click Me").enabled?)end |
def test_assertLinkassert($ie.link(:text, "Click Me").exists?)end |
def test_assertTextfieldassert($ie.text_field(;name, "field1").exists?)end |
def test_assertRbuttonassert($ie.radio(:value, "radio button").isSet?end |
def assertButtonverify($ie.button(:caption, "Click Me").enabled?)end |
Test::Unit
- The classes that represent test cases must be subclasses of Test::Unit::Testcase
- Nathaniel Talbot, author of Test::Unit says,
- Test Cases are in files named: tc_xxx
- Test Suites are in files named: ts_xxx
- #file ts_dbaccess.rb
- require 'test/unit'
- require 'tc_connect'
- require 'tc_query'
- require 'tc_update'
- require 'tc_delete'
Find really good examples...of just about everything you would want to do in the:
unittests directory of your watir installation.
(C:\ruby\lib\ruby\gems\1.8\gems\watir-1.5.1.1145\unittests)
Methods, Classes, Modules
- Modules are a way of grouping together methods, classes and constants.
- Modules are name with an initial Uppercase letter
Arrays
To get a concise list of _everything_ an array can do, try this from irb: <
any_old_array = [] puts any_old_array.methods
Test Case Architecture / Organization
| root | parent | child | What is this file? |
|---|---|---|---|
| Readme.txt | This is where you describe your package. | ||
| Rakefile | The rakefile makes it easier to do tasks like run tests. | ||
| Setup.rb | |||
| bin/ | bin is where to put complete scripts that you run from the command line. | ||
| bin-skeleton | |||
| test/ | test is for tests. | ||
| test-skeleton | |||
| lib/ | lib is for all the other Ruby files in your project (ones that are required by a bin script or other Ruby file. | ||
| default-project.rb | |||
| default-project/ | |||
| lib-skeleton | |||
| third-party | |||
| s4t-utils.rb | |||
| s4t-utils.rb |
On organizing test cases using test::unit:
A. If you have the following methods in a testcase using test::unit →
def setup
def test_01
def test_02
def test_03
def teardown
They will execute in this order (every test method is wrapped with the 'setup' and 'teardown' methods)
def setup
def test_01
def teardown
def setup
def test_02
def teardown
def setup
def test_03
def teardown
You'll want to write your testcases such that you are not launching your ie browser 66x or 180x, etc... use less methods and more assertions within your methods to prevent this...
B. Keep your test cases organized by objective or method of testing.
For example: If it's a simple navigation test,
1. Keep all the sidebar navigation elements testing in one case.
2. & the main 'div' elements in another.
[Watir] Objects
| Object | Object Identifiers | Common Methods |
|---|---|---|
| Button | :id | .attach |
| Radio | :name | .back |
| Checkbox | :value | .bring_to_front |
| Textfield | :index | .checked? |
| Hidden | :title | .clear |
| SelectList | :url | .click |
| Label | :class | .close |
| Span | :text | .contains_text |
| Div | :beforeText | .each |
| P | :afterText | .enabled? |
| Link | .exists? | |
| Table | .flash | |
| Image | .focus | |
| .front? | ||
| .isSet? | ||
| .new | ||
| .readonly? | ||
| .screen_capture | ||
| .select | ||
| .set | ||
| .show_all_objects | ||
| .to_a | ||
| .to_s | ||
| .wait | ||
| .wait_for_browser |
