watir学习笔记(zz)

上一篇 / 下一篇  2009-08-04 18:41:45 / 个人分类:ruby&watir

watir学习笔记/ruby
:U[(Kn,v _| r0
开发测试案例(Developing Test Cases)
kJ k(hY8{/?5h01.打开编辑器51Testing软件测试网 ?0Kg&tzr
2.以.rb为你的文件扩展名51Testing软件测试网QrE q _*b
3.在测试文件的第一句写上“require 'watir'”,确保可以访问Watir工具。
*HF:H8|5pk5XXz'P04.打开浏览器并转到要测试的应用51Testing软件测试网4Z#j1] xr
5.与之交互并设计你的testcase
pWuF2K G7[06.在测试脚本中使用Watir方法
AIk5G&Lp)~#@.m07.验证结果51Testing软件测试网\,j4}[]%tY

kp6|YY4e0与网页交互(Interacting With a Web Page)51Testing软件测试网s"o_({7Q9Vz
当使用Watir开发测试脚本的时候,通过给网页上的对象发送消息来与之交互。
3mX$|Oi(h051Testing软件测试网F0q-EHz\d
ie.text_field(:name , "q").set("bluescorpio")
|,W-@2[Mm c,M0ie.button(:value , "Click Me").click51Testing软件测试网7S9H9Mi-HM
51Testing软件测试网o7S,yt-A

i a#b z"@+NC7@0Watir语法(Watir Syntax)51Testing软件测试网cW,f VU},n6N {w
1.使用Watir工具,需要在脚本中加上
3d]0Tv Jy0require 'watir'
h*d/ov D:AI0
A6r3O z2G"Lo2Nj n02.创建一个IE的测试实例
8p.lplY e;U0ie = Watir::IE.new51Testing软件测试网+j-n*DO0S ^4S
或者在创建的同时直接转到页面
&j)}tiu Z5O0ie = Watir::IE.start("http://mytestsite";)51Testing软件测试网 ?;b+p-d"{E7k
Watir使用start方法同时创建一个浏览器实例并转到一个页面。51Testing软件测试网JL,A2w)B$Ee
51Testing软件测试网,MW0CR,yFI%G
3.页面导航
v$Os!f'P0ie.goto("http://mytestsite";)
scD]'q\8~;Zb051Testing软件测试网6DSt@$EpeP bT;d
4.操纵Web页面对象
Q!l i7T5c04.1超链接51Testing软件测试网k1fP h6z
4.1.1使用Text属性点击超链接51Testing软件测试网,lST4B%O"Z wf3W
ie.link(:text , "Pickaxe").click
Jr Hqo0对应的HTML代码为:51Testing软件测试网$W6y2f @ Y}
<a href="http://pragmaticprogrammer.com/titles/ruby/";>Pickaxe</a>51Testing软件测试网/IA1l&DGTgo4{
4.1.2使用URL属性点击超链接
p X+Li?S0ie.link(:url , "http://pragmaticprogrammer.com/titles/ruby/";).click51Testing软件测试网"ke%kBmfWU {R:P u
对应的HTML代码为:51Testing软件测试网tK0}J'U z
<a href="http://pragmaticprogrammer.com/titles/ruby/";>Test Site</a>51Testing软件测试网,w.Z j#A ~Ho

aqFv$f nW&y5v04.2复选框
m,tBwt&bG04.2.1使用name属性设置复选框51Testing软件测试网c2qMQ/S1y^
ie.checkbox(:name, "checkme").set
1kQ7uq/y04.2.2使用name属性清除复选框
-b*w2H Q/` cF0ie.checkbox(:name, "checkme").clear
;\tRj-@'g9K7\04.2.3使用name和value属性设置复选框
1r+F t J)v6y9J*|(y0ie.checkbox(:name, "checkme", "1").set51Testing软件测试网"B a;eb'H
4.2.4使用name和value属性清除复选框
*~APlkt~-E0ie.checkbox(:name, "checkme", "1").clear
qU/C"Dp0对应的HTML代码为:
? c7fOZ4^0R0<input type = "checkbox" name = "checkme" value = "1">
&m K'x.\1PU Rw }H(@V0
's$I"iF`4hn04.3单选框
~yR.` Z3[9s/X04.3.1使用name属性设置单选框
;nG4l^:SNlo^ g0ie.radio(:name, "clickme").set
/bb0Q8XI G1vI04.3.2使用name属性清除单选框51Testing软件测试网$?+U/Y'sxT#s0jk,T
ie.radio(:name, "clickme").clear51Testing软件测试网4R3M/t6j` x
4.3.3使用name和id属性设置单选框
&vKie"C+jh0ie.radio(:name, "clickme", "1").set
aGF J9XTnRp7]04.3.4使用name和id属性清除单选框51Testing软件测试网(uP!W(Jx
ie.radio(:name, "clickme", "1").clear
g3{%v3un a5DZX;_0对应的HTML代码为:
4q1Md%h u p-U bt&EB0<input type = "radio" name = "clickme" id = "1">
QmA[N%n051Testing软件测试网Pq s ?'BT
4.4下拉框
O-Dv9}tn Q3k6E2b04.4.1使用name属性和值来设置下拉框51Testing软件测试网 _0[*} dlL
ie.select_list( :name , "selectme").select("is fun")
9[]!G;j4ahf04.4.2使用name属性和值来清除下拉框51Testing软件测试网mV*Y4^}Ob(@
ie.select_list( :name , "selectme").clearSelection
1c;AlK:fc0对应的HTML代码为:
V r7u"I)c0V@0<select name = "selectme" > <option name=1> <option name=2>Web Testing <option name=3>in Ruby <option name=4>is fun </select>51Testing软件测试网H fl;a-@

3pTjn.^w,n$A04.5在Web页面中输入数据
"{Z COu051Testing软件测试网'{[!i:iH F7gYj's2p:S
4.5.1使用文本输入框的那么属性设置输入内容51Testing软件测试网 L0Ta'^C^3n-A
ie.text_field(:name, "typeinme").set("Watir World")
t1x PNst-T Z04.5.2清空文本输入框
h0Y;\{8V _n8\x|0ie.text_field(:name, "typeinme").clear
;@8|+[)gM$` j0对应的HTML代码为:
4J,j5hMag0<input type = "text" name = "typeinme" >
1JY8B/H(We051Testing软件测试网/{ ] L2y A wO2o6\
4.6从Web页面上提交数据
Xw$Co!O*}04.6.1按钮
h:L4@&^-DS04.6.1.1通过值或标题属性点击按钮51Testing软件测试网+[/}Q.e6v!Ci
ie.button(:value, "Click Me").click51Testing软件测试网ZJi:X,r"Bd/pS
4.6.1.2通过name属性点击按钮
1Pz N4kd&{%Nf;Z0ie.button(:name, "clickme").click
TE&jwY*v9h.O+y0
]?LPzErQ/r0对应的HTML代码为:51Testing软件测试网.]#V$uA-gC4k&@
<input type = "button" name = "clickme" value = "Click Me">
0BO$OP[rW'_|B0
]*S0F)L5B\._0
g%X#dV-@*`I04.6.2表单
:U j0^+a\:KCI04.6.2.1表单中的按钮51Testing软件测试网z_^{~;`4J(\
使用value或标题属性
!`1]+W,O"y6G1f6M0[ ~P0ie.button(:value, "Submit").click51Testing软件测试网+`^i[&`T]!or7v:E
对应的HTML代码为:
e8v0j*C;rP0<form. action = "submit" name = "submitform" method="post"><input type = "submit" value = "Submit"></input></form>
&zdydO8R$d051Testing软件测试网Wc(p c)h
4.6.2.2表单中的图片按钮
j+\Y R:W,fe`o0使用那么属性
6JF)q sS_`'j9r0ie.button(:name, "doit").click
mkYA} k.e0对应的HTML代码为:
9?~9WJ4Y0<form. action = "submit" name = "doitform" method="post"><input type="image" src = "images/doit.gif" name = "doit"></form>51Testing软件测试网W2M t5M_r i
51Testing软件测试网*[%Y#b/z+UQ(AF
4.6.2.3没有按钮的表单51Testing软件测试网5s[9x3` Q I a2BY"Q
Watir can submit a form. by identifying it by its name, action and method attributes.
5\ pDw@7@0可以通过name、action以及method属性来提交表单
-o9~nL'~[0ie.form(:name, "loginform").submit51Testing软件测试网 aR*mM~
ie.form(:action, "login").submit
6s1bR1F.?Q)bL \0对应的HTML代码为:51Testing软件测试网0l8U7u5a2p-w8Pi
<form. action = "login" name = "loginform" method="get"><input name="username" type="text"></input></form>51Testing软件测试网q6V Z [ d
51Testing软件测试网`2BE?;XTR-n
4.6.3框架51Testing软件测试网iU"@ S$}
ie.show_frames可以打印出当前页面框架的数量和名称51Testing软件测试网c~e,|[a
Watir允许通过名称属性来访问框架,如ie.frame("menu")51Testing软件测试网@|rCt_ J vS4z
如果要访问menu框架中的一个超链接,可以ie.frame("menu").link(:text, "Click Menu Item").click51Testing软件测试网&Y8h/a5}-R+Pn

M,MAt8o04.6.4嵌套框架51Testing软件测试网5f:VV} Q"e
ie.frame("frame1").frame(:name, "nested_frame")
,l*vkH1N8[~0
|)a N.zPJ6aTa04.6.5新窗口51Testing软件测试网6ja7^)a,b0A
一些Web应用会弹出新窗口或打开一个新窗口,可以使用attach方法来访问并控制新窗口。通过标示新窗口的URL或者title来访问。
Z ?EPay%R H0ie2 = Watir::IE.attach(:url, 'http://mytestsite')
L `0f JG1n.S@6p0ie3 = Watir::IE.attach(:title, 'Test New Window')
BC2e"w9P t0也可以使用正则表达式51Testing软件测试网B,k/a&R:M
ie4 = Watir::IE.attach(:title, /Test New/)51Testing软件测试网TH"{v1\ W(^*Pvm
注意:不要把新窗口分配到你的ie变量,最好给新窗口一个不同的名字
#I9y$by)a2M~3z051Testing软件测试网4j k0J"xmuRg`~
5.验证结果
^2e [:Ze2s|3E0比较好的方法是在测试案例中假如验证点
[3Z B1o-r _*l&Y)yP05.1对象存在51Testing软件测试网u/ps#Qz3Y
使用Watir方法contains_text51Testing软件测试网2_8V6@@[ ?1^s4i$P
ie.contains_text("Reached test verification point.")51Testing软件测试网"Y[\2zb)o'g)l j-v
51Testing软件测试网z)x;KX ig(P
if ie.contains_text("Reached test verification point.")51Testing软件测试网(?` n q@,d
   puts: "Test passed. Page contains the text: Reached test verification point."51Testing软件测试网t[%o8f#k
else51Testing软件测试网j:BAz0T
   puts: "Test failed! Page didn't contain text: Reached test verification point."
n(NH(j*G*}0end
'u*X%]m7^MYT7g*C3mj:u051Testing软件测试网,m/[tYx|`7I0`
5.2使用test::unit Assertions51Testing软件测试网Bq M4ot*V lZ
5.2.1需要test::unit
/Q0VA'?!?\0require 'test/unit'
,O;UuLq$u051Testing软件测试网*]V7k`(zGh:Mc E
5.2.2创建测试实例
v%n0P;`F.l-P].C_"c0class TC_myTest < Test::Unit::TestCase51Testing软件测试网{.b;IXEk Z
   ...fill in Test Case methods here...51Testing软件测试网9op;{(tL V{(d
end51Testing软件测试网 Fh8O.[*R?*`-g
51Testing软件测试网?D.d G-^.l-@!H
5.2.3创建测试用例方法
3kvBa-fV8F0在测试类中,需要声明象下面的方法:51Testing软件测试网!s.aV~Yo Kn @
def test_myTestCase51Testing软件测试网rE_!KZz.I` j+S'?
   fill in method body with Watir code and assertion here51Testing软件测试网BUy}P \*Xi
end51Testing软件测试网?&jN`2{0Y
51Testing软件测试网V"hs pL7rV
方法必须以test开头,ruby会随机运行测试案例,如果需要顺序执行,需要在test后加上字母或数字来强迫它顺序执行,比如“test_a_mytest”51Testing软件测试网S+}Z)Vaz;ZT
51Testing软件测试网 `"l]T9s?
定义测试方法的类:51Testing软件测试网Y)hY&t5kW\{ H
class TC_myTest < Test::Unit::TestCase51Testing软件测试网2|6tAE lrZ
   def test_ myTestCase51Testing软件测试网9kx?L ^&@^[ o
     Watir code and assertion here...51Testing软件测试网.Q Y0v?E7B4GB4R vh/`,D
   end51Testing软件测试网(a|wR9C*h$Z
   def test_anotherTestCase
8]+lB \G;J0     Watir code and assertion here...51Testing软件测试网'v VbVL
   end51Testing软件测试网J{J(bC
   def test_aTestCase51Testing软件测试网*z;J&^6zBr
     Watir code and assertion here...51Testing软件测试网 IB7u7lR NH^ h0|
   end51Testing软件测试网1{zKQsk y Oq
end51Testing软件测试网,ZUy#tP
51Testing软件测试网)g"y[7wd2z
5.2.4使用Assertions
3I`*C_ep4IP6q0Watir通过在一个asert覆写Watir方法支持assertions。
'FENqM i)O0assert(ie.contains_text("Reached test verification point.")51Testing软件测试网 L0z~BM{-@"Q7L N7E
51Testing软件测试网t ]7Y _8OH bX(j
5.2.5Setup and Teardown51Testing软件测试网6k-dO,x^,rV!B n
def setup
;SP1o2V9Yb0   fill in code that will run before every test case here...51Testing软件测试网C5H};F2dd&z
end
1g5s)c-{S'Q4W v5B0def teardown51Testing软件测试网.P9`toC.cB B
   fill in code that will run after every test case here...
@3Jr$[P0end51Testing软件测试网4]8G tH$m`d3NuYp#C

t0^*z4D'`%|06.Tips and Tricks
(dl*{go$Red+E0Running Tests With the Browser Not Visible51Testing软件测试网PI!y*mH:Qa~
Run the tests with a "-b" option if you don't want the browser to be visible. ex. myTest.rb -b51Testing软件测试网&O%L ?(ZO'ro@

TAG:

 

评分:0

我来说两句

Open Toolbar