Watir 学习笔记
HTML tag : select
e.g. <select name = "selectme"><option name =1><option name = 2>Web Testing<option name =3>in Ruby<option name=4>is fun</select>
Watir code : ie.select_list(:name, "selectme").select("is fun")
or ie.select_list(:name, "selectme").clearSelection
HTML tag : input type = submit
e.g. <form action = "submit" name = "submitform" method = "post"><input type = "submit" value = "Submit"></input></form>
Watir code : ie.button(:value."Submit").click
HTML tag : img src
e.g. <form action ="submit" name = "doitform" method = "post"><input type = "image" src = "images/doit.gif" name = "doit"></form>
Watir code : ie.button(:name, "doit").click
HTML tag : form's name , action , method
Note : Input your name and hit ENTER
e.g. <form action = "login" name = "loginform" method = "get"><input name = "username" type = "text"></input></form>
Watir code : ie.form(:name, "loginform").submit
or ie.form(:action,"login").submit
HTML tag and e.g. :
<frameset cols = "120,*">
<frame src = "menu.html" name = "menu">
<frame src = "main.html" name = "main">
</frameset>
Watir code : ie.show_frames , ie.frame("menu") ,
ie.frame("menu").link(:text, "Click Menu Item").click
Nested Frames : ie.frame("frame1").frame(:name, "nested_frame")
Within our test case class, we need to declare test cases as methods like this:
def test_myTestCase
fill in method body with Watir code and assertion here
end
Test case method names must be prefixed with the word "test".
class TC_myTest < Test::Unit::TestCase
def test_ myTestCase
Watir code and assertion here...
end
def test_anotherTestCase
Watir code and assertion here...
end
def test_aTestCase
Watir code and assertion here...
end
end
Use Assertions
Here is an example of how to print the results of an assertion:
if assert(ie.contains_text("Test verification point."))
puts("TEST PASSED. Found test string: 'Test verification point.' ")
else
puts("TEST FAILED.")
end
This is an example that will print out more information on a failure:
begin
assert(ie.contains_text("Test verification point."))
puts("TEST PASSED. Found test string: 'Test verification point.' ")
rescue => e
puts("TEST FAILED." + e.message + "\n" + e.backtrace.join("\n"))
end
class variable "@@ie", global variable "$ie"
Some web architectures dynamically generate tags, and the attributes might change each time you run a test. To work around this, try the following:
+ - Access Element Using Index
+ - Using Regular Expressions
c:\>irb
irb(main):001:0> require 'watir'
=> true
irb(main):002:0> ie = Watir::IE.start("http://mytestsite.com")
Controller : the Watir scripting package that controls a web browser
Instance : an object that has been created for use by a program and is stored in memory
Message : an action descriptor that can be sent to an object. If the object recognizes the message, it will respond and display related behavior.
Object : something that contains attributes and displays behavior according to what messages have been sent to it.
Ruby : object oriented scripting language
Watir : Web Application Testing in Ruby