watir教程实例(zz)

上一篇 / 下一篇  2008-11-12 17:08:11 / 个人分类:技术&其他

watir教程实例
 
开发测试案例(DevelopingTestCases)51Testing软件测试网(@u6f s!Y&Cok
1.打开编辑器51Testing软件测试网cd:o"~!b
2.以.rb为你的文件扩展名51Testing软件测试网p^mQM2Fk
3.在测试文件的第一句写上“require 'watir'”,确保可以访问Watir工具。
.g;nN)n hX;T.^04.打开浏览器并转到要测试的应用
pH @Nz05.与之交互并设计你的testcase
Y g/g XZ}06.在测试脚本中使用Watir方法51Testing软件测试网K.Ep/Qo
7.验证结果
cTl(_2E-e!|2?)rF0
|bJ1yG0与网页交互(Interacting With a Web Page)51Testing软件测试网m6l;y @l.k/F7vW/~d
当使用Watir开发测试脚本的时候,通过给网页上的对象发送消息来与之交互。51Testing软件测试网Q!~$wv4B4R
51Testing软件测试网~1?%x?ilN5F
ie.text_field(:name , "q").set("bluescorpio")
a:m/_u~n0ie.button(:value , "Click Me").click
!ZX5}.S9T D%x0X7R7T0
s h O5STRC/i051Testing软件测试网6|w|1R7^*k7[Z \ ];V
Watir语法(Watir Syntax)
?)Ib0bB01.使用Watir工具,需要在脚本中加上
2de7?b{0require 'watir'51Testing软件测试网1\s)WB-gM

Zm~3])t02.创建一个IE的测试实例51Testing软件测试网(j@.T(b(T+j8Y(E
ie = Watir::IE.new
z:?+E |W6OJ8oT:O1^0或者在创建的同时直接转到页面51Testing软件测试网:|z6p!f+u1L/@2O'qv9o
ie = Watir::IE.start("http://mytestsite";)51Testing软件测试网,_)EPh([ C(v7b6D3o
Watir使用start方法同时创建一个浏览器实例并转到一个页面。51Testing软件测试网Q!g\]V&MH

/e4u9Pvz-sqiE03.页面导航
Z/U)gdQ-E/{m g0ie.goto("http://mytestsite";)
F^ S8T]qD0
T*k3gG1V Z04.操纵Web页面对象51Testing软件测试网G&U2f+O4?lF;ja
4.1超链接51Testing软件测试网0I@$Fh ^s(g-W
4.1.1使用Text属性点击超链接
SBftz^x0ie.link(:text , "Pickaxe").click
"n~;Xl%{(h(Q T0对应的HTML代码为:
nx5f ~S4B-r`i0<a href="http://pragmaticprogrammer.com/titles/ruby/";>Pickaxe</a>
8R$Tg ^t*m04.1.2使用URL属性点击超链接51Testing软件测试网8S3U![l A+ND
ie.link(:url , "http://pragmaticprogrammer.com/titles/ruby/";).click51Testing软件测试网*AY8|1q4ohJZ
对应的HTML代码为:51Testing软件测试网o2n)[ L5CG qq'Uk _2P
<a href="http://pragmaticprogrammer.com/titles/ruby/";>Test Site</a>51Testing软件测试网z _4M%OQ(A
51Testing软件测试网XEw-x,zd].f
4.2复选框
Zv{6d'D2?lT04.2.1使用name属性设置复选框
F5W+Z[{5F|N0ie.checkbox(:name, "checkme").set
$Eg*R.G)CT Q04.2.2使用name属性清除复选框
%G\%U"dN"S:Zr0ie.checkbox(:name, "checkme").clear51Testing软件测试网g_)ne${^z
4.2.3使用name和value属性设置复选框51Testing软件测试网B-t5L%H0SA.R
ie.checkbox(:name, "checkme", "1").set51Testing软件测试网9JC kp'kc8z
4.2.4使用name和value属性清除复选框51Testing软件测试网,x6Ms^N!|.z3[
ie.checkbox(:name, "checkme", "1").clear
:d I0b+jot^r1l-SS0对应的HTML代码为:
J6DA"v)iRsZM0<input type = "checkbox" name = "checkme" value = "1">51Testing软件测试网 o!pR_n_

d#w EwP9D u {a04.3单选框51Testing软件测试网+g:Nj(`|Qa
4.3.1使用name属性设置单选框51Testing软件测试网G @z okCb+W6pZ!W
ie.radio(:name, "clickme").set51Testing软件测试网W"h+~*W8gB
4.3.2使用name属性清除单选框51Testing软件测试网(?%~ |Ku K@a
ie.radio(:name, "clickme").clear51Testing软件测试网$x b2M"aY'zx F
4.3.3使用name和id属性设置单选框51Testing软件测试网"OZH6~#E
ie.radio(:name, "clickme", "1").set
2iS6U)W%I}` W o8z04.3.4使用name和id属性清除单选框
h"JMiY\0ie.radio(:name, "clickme", "1").clear
+ljtqp[+q+X,k]0对应的HTML代码为:51Testing软件测试网(I-~8[ j8c:P'm(}y!~
<input type = "radio" name = "clickme" id = "1">51Testing软件测试网^o.D igE%Y U4Gw
51Testing软件测试网 p8C~v-y}H
4.4下拉框51Testing软件测试网f P%a%ReGwB
4.4.1使用name属性和值来设置下拉框51Testing软件测试网z$_/uR'I.o)LA.V'O
ie.select_list( :name , "selectme").select("is fun")51Testing软件测试网"y hAQ/?&}9M
4.4.2使用name属性和值来清除下拉框
0h GRO[0ie.select_list( :name , "selectme").clearSelection51Testing软件测试网_xe6JM JJ7GA
对应的HTML代码为:51Testing软件测试网!d5t!?[s9@ Mc
<select name = "selectme" > <option name=1> <option name=2>WebTesting<option name=3>inRuby<option name=4>is fun </select>
1p6j/s4qsS'k051Testing软件测试网0v4k1E,`?T9XL
4.5在Web页面中输入数据51Testing软件测试网ZW0W*p.k9sm)D'[
51Testing软件测试网gTs/n2f
4.5.1使用文本输入框的那么属性设置输入内容
!]:Vk ~ hk0e0ie.text_field(:name, "typeinme").set("Watir World")51Testing软件测试网zJ%A q.Iy
4.5.2清空文本输入框51Testing软件测试网DzL EBP o6B$A)Pl
ie.text_field(:name, "typeinme").clear51Testing软件测试网Y-{LY9v#r
对应的HTML代码为:
;m^ `9R@:N8}[;p0<input type = "text" name = "typeinme" >51Testing软件测试网I Wm~&h.F!Mn
51Testing软件测试网J,t#v%l%R
4.6从Web页面上提交数据
&h,w7AH!~:} M F MX6p04.6.1按钮51Testing软件测试网x3g:x ?#m
4.6.1.1通过值或标题属性点击按钮51Testing软件测试网2n~#~ G#KPO(Lkd@
ie.button(:value, "Click Me").click
)vlF W5Y"M kKBt04.6.1.2通过name属性点击按钮
_!C+C x|8mluE0ie.button(:name, "clickme").click51Testing软件测试网(c'Q g {,t;S B7_(RU
51Testing软件测试网RT XWG*k
对应的HTML代码为:
:?Y0Eq-Q.Nw0<input type = "button" name = "clickme" value = "Click Me">51Testing软件测试网z*W)y.D,n

/kJB(Roe[A9g0
$QbX/r+Bl/C04.6.2表单51Testing软件测试网,Bzm4^*v$q*e
4.6.2.1表单中的按钮
)o#Hj:IW$]8V0使用value或标题属性51Testing软件测试网 |iSQ5Q(s
ie.button(:value, "Submit").click
$AiM2Gjk*h9J8Z0b0对应的HTML代码为:51Testing软件测试网~d6O+iS^+{
<form. action = "submit" name = "submitform" method="post"><input type = "submit" value = "Submit"></input></form>
6uMV H!G,nhR!g'm051Testing软件测试网F8a:fX5qb
4.6.2.2表单中的图片按钮51Testing软件测试网8}(avUA*L C
使用那么属性
~:o~%NuG0ie.button(:name, "doit").click
4A7y}l6?r4e0对应的HTML代码为:
md"t%Pol0<form. action = "submit" name = "doitform" method="post"><input type="image" src = "images/doit.gif" name = "doit"></form>51Testing软件测试网x}"A1|%[T Ie)e
51Testing软件测试网$s8SB&VK@_
4.6.2.3没有按钮的表单
u/`tFu g0Watir can submit a form. by identifying it by its name, action and method attributes.
l%h)h6Q4f^1ok3Q0可以通过name、action以及method属性来提交表单
*yX*q3uv(Vbz Ih1y0ie.form(:name, "loginform").submit51Testing软件测试网co7s n+o9\l/t L
ie.form(:action, "login").submit51Testing软件测试网Jf:GI N/K|
对应的HTML代码为:51Testing软件测试网!MNz$R[
<form. action = "login" name = "loginform" method="get"><input name="username" type="text"></input></form>51Testing软件测试网R-L6x_j+y |W v4Z*n T
##  获取隐含对象值
<INPUT type=hidden value="您的Email" name="field1">                  51Testing软件测试网/K$TpHY4b1oF
     方法:values = ie.hidden(:name,'field1').value
                                             51Testing软件测试网"RZj1G"K9lV
                                                                         

0TqZ&tE;b04.6.3框架
c:q3r$uv F]J(X s0ie.show_frames可以打印出当前页面框架的数量和名称51Testing软件测试网H2H3m$tk o ?*L,F
Watir允许通过名称属性来访问框架,如ie.frame("menu")
X:N7Nu#_Vn.o"uv0如果要访问menu框架中的一个超链接,可以ie.frame("menu").link(:text, "Click Menu Item").click
t-ZW lc8QM!Z0
O*wu8RLk7y04.6.4嵌套框架51Testing软件测试网%s _5QR4G7l1Y4d+U
ie.frame("frame1").frame(:name, "nested_frame")
$q U I8le#? ms0
/P8T8oBA1ghg\04.6.5新窗口
5B g DlV~3`N,[!s aX0一些Web应用会弹出新窗口或打开一个新窗口,可以使用attach方法来访问并控制新窗口。通过标示新窗口的URL或者title来访问。51Testing软件测试网3W:pE#Gh*S_Rb q1g
ie2 = Watir::IE.attach(:url, 'http://mytestsite')51Testing软件测试网_SmZ5BK P
ie3 = Watir::IE.attach(:title, 'Test New Window')51Testing软件测试网{N2o mpF D$f
也可以使用正则表达式
0AP/bFTR0ie4 = Watir::IE.attach(:title, /Test New/)51Testing软件测试网!Tka EZ7WA&K
注意:不要把新窗口分配到你的ie变量,最好给新窗口一个不同的名字
.qp GvN,\f0
)[0L Z)V#c7Q05.验证结果51Testing软件测试网!M:N:YC6]#^G
比较好的方法是在测试案例中假如验证点51Testing软件测试网/m b,lESxBhZ&`w
5.1对象存在51Testing软件测试网V'IrER ek
使用Watir方法contains_text
'L N#P5L;[;D)HN0ie.contains_text("Reached test verification point.")51Testing软件测试网Dq:jU _6qB

{/A4UuA;]0if ie.contains_text("Reached test verification point.")
tW`9B&v0   puts: "Test passed. Page contains the text: Reached test verification point."51Testing软件测试网K/mB0p$s"g#s
else51Testing软件测试网s;GL Nr1F0G
   puts: "Test failed! Page didn't contain text: Reached test verification point."
wVN-h-w0end51Testing软件测试网b&L(z ~c:J
51Testing软件测试网tGQ1H3x^\
5.2使用test::unit Assertions51Testing软件测试网0I-f1C9lt-EOp
5.2.1需要test::unit51Testing软件测试网,FYm#pjS
require 'test/unit'51Testing软件测试网7D]-k9y `#R6Z
51Testing软件测试网|+c)e9`#b:f%@
5.2.2创建测试实例
*Ewqm5G N!\ c0class TC_myTest < Test::Unit::TestCase51Testing软件测试网*e4j4[v9U)V Wx^
   ...fill in Test Case methods here...51Testing软件测试网L2Z ggaO"}'l
end51Testing软件测试网 N'x G0eS@]8N,k)D

M1c-]!Iwch4KN"Zc05.2.3创建测试用例方法
WXwL6d/d0在测试类中,需要声明象下面的方法:51Testing软件测试网3J N5N/L!O0c l'n
def test_myTestCase51Testing软件测试网;kBI(col
   fill in method body with Watir code and assertion here
{6@Kz2r7_0]0end
^F6f`;H"||j | W0
4E@5w"A6R3}n c0方法必须以test开头,ruby会随机运行测试案例,如果需要顺序执行,需要在test后加上字母或数字来强迫它顺序执行,比如“test_a_mytest”
E:W-Zb2w6L0
[lr'xL VG5k j0定义测试方法的类:51Testing软件测试网F;xcJ_q
class TC_myTest < Test::Unit::TestCase51Testing软件测试网1t jZXj#N
   def test_ myTestCase
Nwa(\2A8jd6t5nq0     Watir code and assertion here...51Testing软件测试网2k,LEKw.RW n
   end
.Bj t+i;qo:\0   def test_anotherTestCase51Testing软件测试网%nb?.e%qB"~K
     Watir code and assertion here...
_1y5i,u%d+GF`g0   end
T!dh:i2s#v-m0   def test_aTestCase51Testing软件测试网"r`~ tx!s M[
     Watir code and assertion here...51Testing软件测试网o)G;XL,M D#i
   end
u;m W5C g-Ar t0end
*D Q|3_#E:B(c S#n051Testing软件测试网 T2V5R-j2a[P0^#{
5.2.4使用Assertions51Testing软件测试网-k R6e4Dkp5A
Watir通过在一个asert覆写Watir方法支持assertions。
M-u] PP0assert(ie.contains_text("Reached test verification point.")51Testing软件测试网4@@;}8Q!P?4Qa|
51Testing软件测试网h/L`/t |f:I*o:X
5.2.5Setup and Teardown
;BA7PRk1Y*ME&{Ef2o0def setup
3KY$~3?6fcz0   fill in code that will run before every test case here...
$kj*SC/J&m0end51Testing软件测试网m Af ho }:y1}"Q8G
def teardown
*k ePm)` h4[&R0   fill in code that will run after every test case here...51Testing软件测试网t+b:baNbs
end
c#p I\ l P8w Z0
FR l ] d/Ru a(yLM06.Tips and Tricks
*t~7[.OU:i0_9DxG0Running Tests With the Browser Not Visible
+h*Lp.BRR*^0Run the tests with a "-b" option if you don't want the browser to be visible. ex. myTest.rb -b
 
URL编码:                                                                                       
5?8g5xG1_A Q0       require 'cgi'                                                                          
0M:c@,Fl6x1h'K+S0       string =  "URL编码"                                                                     
z0g#T5PR1M0       string = CGI::escape(string)                                                            51Testing软件测试网5]{-P+f!B/M
       puts string                 # 转换结果: URL%B1%E0%C2%EB                                 51Testing软件测试网%Lz#I [C}"v-C*C
                                                                                               51Testing软件测试网"n5HCVK
URL解码:                                                                                       51Testing软件测试网yADjT,{-[)]
      require 'cgi'                                                                            
+rC4jL z'gV0       string =  "URL%BD%E2%C2%EB"                                                             
Zv0Q.h5j0LZG0       string = CGI::unescape(string)                                                          51Testing软件测试网$Y;I#H mA
       puts string                  # 转换结果:URL解码
        
 

TAG:

 

评分:0

我来说两句

Open Toolbar