WatiN系列之三 Watin如何处理Web页面弹出窗口,对话框,提示框

转帖地址: http://www.51testing.com/?uid-61753-action-viewspace-itemid-219961

 

Watin Web 页面弹出窗口 , 对话框 , 提示框的处理 51Testing软件测试网 W(~@ox G2W8]

  51Testing软件测试网+Do�rK/'H1BUw@Z }

处理弹出窗口 51Testing软件测试网o m+AjPu+Y o

  首先创建一个 web 页,用于演示弹出窗口。 51Testing软件测试网j8YER1b"Z8_w

 

3X8I3I%H9m2w0

'[_be/X4^SUf0

< input id ="Button1" type ="button" value ="button" onclick ="openwindow()" />

#|.I'D:kh.{0

    < script type ="text/javascript">

r8zy�R2a�rk0

    function openwindow()

p-_1[#q.T,I'C9Mpa0

      {

2oH8h*YE"sD0

          window.open("http://localhost/Test/test2.htm" ); 51Testing软件测试网/XRr)c fhe?9r)z

      }

5U6GB^UV&i0

    </ script >

8{5k&yo9]K)] K$jZ0

51Testing软件测试网:H7L:wz#Mh!Zr

上述代码 , 点击“ Button1 ”后 , 弹出窗口 test2.htm. 我们要做的是如何处理 test2.htm 页面 51Testing软件测试网eiwDcb;J

Watin 处理代码如下 : 51Testing软件测试网h$d1B;W3v7^ j

51Testing软件测试网3}'@j$pvq/e&I

IE ie =new IE ("http://localhost/Test/" ); 51Testing软件测试网cO$}.v5NVSR

//点击按钮,打开新窗口test2

;C*q2as:R8emJli0

ie.Button(Find .ById("Button1" )).Click();

t0]*rmXo,z0

//查找新窗口test2并赋给新的IE对象 51Testing软件测试网S}&i0~ e

IE newie =IE .AttachTo<IE >(Find .ByTitle("test2" ));

Lq,Qw4o0

//使用新的IE对象就可以继续对新窗口进行操作了

AJ F/3d@0

newie.TextField(Find .ById("Text1" )).TypeText("this is new ie" ); 51Testing软件测试网T(N9d;NIh9o

51Testing软件测试网hIb'Y!bk`Z2f

  51Testing软件测试网3xS2d.@:Q,L)G6v s9w

 

Q0qg%z:ff#Y%v0

    处理 confirm 弹出框 51Testing软件测试网eOu9cH _Z

  首先创建一个 web 页,用于演示 confirm 对话框。 51Testing软件测试网x6s{UQ _

51Testing软件测试网2oHT*?�j%p9E0[

< input id ="myButton1" type ="button" value ="this is a button"

*_"Qt&?+o |0

      onclick ="confirmMe(); return false;">< br >

Q7ptiEw(K0

    < script > 51Testing软件测试网.J-_ANV7g

      function confirmMe() { 51Testing软件测试网VUtS*EL9N'bB d

        var answer = confirm ("Are you having fun?" )

9/]|5Y5x`{*h1k0

        if (answer) 51Testing软件测试网 SyZEjv/ Py9~5u"Z

          document.getElementById("myButton1" ).value="Clicked OK" ;

1_o C'ao}*LF0

        else 51Testing软件测试网 b N/tr�@wf{

          document.getElementById("myButton1" ).value="Clicked Cancel" ; 51Testing软件测试网k@/kM$z)]:H|

      }

0b^ r|7h c0

    </ script > 51Testing软件测试网]4fk&E(U#CL

j"g0r0Z#a0

 

zhW4XX9ij.^:R0

相应的 Watin 测试 代码如下 :

F(qjDD k M`0

}%Qa(x,V?"p3S5kE0

 

YT xNNs&s?V4M0

IE ie =new IE ("http://localhost/Test/" ); 51Testing软件测试网&o*//.HG+}c

//创建一个ConfirmDialogHandler对象

G+{&Ihh*v#Y0

ConfirmDialogHandler cdh =new ConfirmDialogHandler (); 51Testing软件测试网M8o"?gEjk q%E

//ConfirmDialogHandler对象与ie建立关联

nv&mV2G�D0

ie.AddDialogHandler(cdh);

3}M2VQ]2o,X0w"R6jpD0

//点击按钮,这里需要注意的是:使用的是ClickNoWait()方法 51Testing软件测试网4yQg SC yl~

ie.Button(Find .ById("myButton1" )).ClickNoWait();

G/tg|s|FS*J*y0

//等待Confirm对话框出现 51Testing软件测试网[9?Z!]%L^0L)?

cdh.WaitUntilExists(); 51Testing软件测试网3C9GK)b{?.T2{

//点击Confirm对话框的OK按钮 51Testing软件测试网,g W)k"H%r$b*W

cdh.OKButton.Click(); 51Testing软件测试网 KNx:t E~G

ie.WaitForComplete(); 51Testing软件测试网 lS1h [&O ]

//ConfirmDialogHandler对象与ie取消关联

fl5j5Mm{d)P FVy0

ie.RemoveDialogHandler(cdh); 51Testing软件测试网(];B }r f-/` B�y,ay

Console .WriteLine(ie.Button(Find .ById("myButton1" )).Value);

&`!j(g-J'y a'f0

Console .ReadLine(); 51Testing软件测试网H(E&y0XOb8x;E+k�N

BGp5kM`(U^]0

 

$TkyUM0

处理 Alert 弹出框

aq8I%P,/tGZE0

首先创建一个 web 页,用于演示 Alert 对话框。

4|%gu^ru8P!E0

:X(BK{-Q+O%LjyJ0

< input id ="myButton2" type ="button" value ="this is a button2" 51Testing软件测试网JKyI(] f.n.B�n

      onclick ="alertMe(); return false;">< br /> 51Testing软件测试网/d"}hSa)|(B)h

    < script >

!W:T5i1gSP,w;r0

      function alertMe() {

//:] BR Acl{H c!cV0

        var answer = alert("this is a alert dialog" ); 51Testing软件测试网�q&~F2h6^'x m

        if (answer) 51Testing软件测试网wN6QS7F!X

          document.getElementById("myButton2" ).value="Clicked OK" ;

xf6g.gwb8i,W&b2C0

      } 51Testing软件测试网7V$V7z0j,X

    </ script >

:OA[*J*] z1ma*uW0

51Testing软件测试网|+B�Q-NU4o

  51Testing软件测试网-X'j-t4{(ZAJ*k3t

相应的 Watin 测试代码如下 :

EG7/YE0

CbL8s'sTO@l0

IE ie =new IE ("http://localhost/Test/" ); 51Testing软件测试网:z({4E9nh!x H}sjW

//创建一个AlertDialogHandler对象

(gy"e"R3Y` JC'n_0

AlertDialogHandler adh =new AlertDialogHandler (); 51Testing软件测试网Q p/D!k y-t

//ConfirmDialogHandler对象与ie建立关联

*O5y"?Q-c.A0

ie.AddDialogHandler(adh); 51Testing软件测试网[UN$B{^L

//点击按钮,这里需要注意的是:使用的是ClickNoWait()方法

/FWA1J#HK&U0

ie.Button(Find .ById("myButton2" )).ClickNoWait();

7gch!MF0

//等待Alert对话框出现 51Testing软件测试网qof-k2KLK

adh.WaitUntilExists();

Sw�Jt*]F2zv0

//点击Alert对话框的OK按钮 51Testing软件测试网'dmwh d?R4T

adh.OKButton.Click();

f:z_DNh`N0

ie.WaitForComplete(); 51Testing软件测试网)ZX!y R FKo

//AlertDialogHandler对象与ie取消关联 51Testing软件测试网3ol�y |6o;J1X0X

ie.RemoveDialogHandler(adh);

,X*g8w.R|[/.K0

Console .WriteLine(ie.Button(Find .ById("myButton2" )).Value); 51Testing软件测试网-`*stV+m v

Console .ReadLine();

XNX#Ev.S%v0

c.],G,K(t0

 

9Ptx8E4@ z/p0

处理 Fileupload 弹出框

3j;])X|t8{8KR0

首先创建一个 web 页,用于演示 Fileupload 对话框。

dk-D[&t0 51Testing软件测试网 s:Me9W],sq

< input id ="File1" type ="file" /></ p > 51Testing软件测试网-LM;z I9K+v

相应的 Watin 测试代码如下 : 51Testing软件测试网 T(lmH8G8M y 51Testing软件测试网o/X h9e eFoY

            IE ie =new IE ("http://localhost/Test/test2.htm" ); 51Testing软件测试网pS5G tx

            //打开文件对话框并加载指定文件 51Testing软件测试网:A.H+TqBJ&W"R$F

            ie.FileUpload(Find .ById("File1" )).Set(@"D:/Data.txt" );

{{;J A&U T~"W0

J0h3} yT l)^0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值