sdb委托开发测试项目 watin
TRANSCRIPT
产品名称 密级
开发使用手册 机密
产品版本 共33页
3.0
SDB委托开发测试项目
WatiN
华为技术有限公司版权所有 侵权必究
(DVP05T01 V2.8 / 仅供内部使用)
拟制 谢宝利/10096 日期 2012-11-15
审核 日期
批准 日期
SDB委托开发测试项目 开发使用手册 机密
修订记录
Date
日期Revision Version
修订版本
CR ID
CR号
Sec No.
修改章节Change Description
修改描述Author
作者
2012-11-15 1.00 初稿 谢宝利
华为机密,未经许可不得扩散 第2页,共33页
SDB委托开发测试项目 开发使用手册 机密
目 录
1简介..............................................................................................................................................51.1 目的....................................................................................................................................51.2 介绍....................................................................................................................................51.3 测试开发环境.....................................................................................................................5
2 Hello,The World!......................................................................................................................53 元素分析..................................................................................................................................73.1 Qaliber Developer Standalone元素分析..........................................................................73.2 开发人员工具元素分析.......................................................................................................9
4 WatiN自动化开发...................................................................................................................104.1 WatiN主要命名空间.........................................................................................................114.2 WatiN常用的类和方法......................................................................................................114.3 WatiN 常用页面元素的操作.............................................................................................114.4 WatiN页面弹出窗口 , 对话框 , 提示框的处理...................................................................14
5 实战实例................................................................................................................................17附录..............................................................................................................................................19
华为机密,未经许可不得扩散 第3页,共33页
SDB委托开发测试项目 开发使用手册 机密
SDB委托开发测试项目 使用手册
关键词:WaitN;explorer
摘 要:
缩略语清单:
缩略语 英文全名 中文解释
华为机密,未经许可不得扩散 第4页,共33页
SDB委托开发测试项目 开发使用手册 机密
1 简介
1.1 目的
SDB委托开发项目WatiN是指导新加入项目的人员能快速学习如何使用WatiN技术性文档。
本文档预期阅读对象:此项目参与人员、项目管理人员、客户。
1.2 介绍
WatiN 是一个开源的用于 Web 测试 自动化的类库,从 watir 中获得的灵感,使用 C# 开发。
WatiN 通过与浏览器的交互来实现自动化,使用起来具有轻便,简单的特点。目前最新版本为 2.0
,加入了对 Firefox 的支持,使其功能更趋于完善和多样。下载地址:http://watin.org/。支持浏览
器 Internet Explorer 6, 7, 8, 9 和 FireFox 2 、3。
1.3 测试开发环境
系统环境:Windows Xp,Windows 7
开发环境:Microsoft Visual Studio 2010 , Qaliber测试框架(包括了WatiN)。
2 Hello,The World!
我们先以一个简单的示例为例来介绍说明 WatiN 是如何与浏览器交互, WatiN 有哪些主要的
部分组成的。
下面是一段 Watin 代码 , 效果是打开百度 网页,然后在搜索 “Watin” ,最后返回搜索结果。
新建工程WatiNTest,使用TestCasesPackage测试项目模板(2-1);默认是引用Watin.Core.dll(2-
2);使用命名空间WatiN.Core(2-3)。
华为机密,未经许可不得扩散 第5页,共33页
SDB委托开发测试项目 开发使用手册 机密
2-1
2-2
2-3
增加新的方法:SearchForWatiNOnBaidu(),代码如下:
华为机密,未经许可不得扩散 第6页,共33页
SDB委托开发测试项目 开发使用手册 机密
public bool SearchForWatiNOnBaidu()
{
bool Ret = false;
//启动一个IE浏览器,打开www.baidu.com网址
IE ieBaidu = new IE("http://www.baidu.com", true);
//找到搜索输入框,并输入WatiN
//*在浏览器中查找Id属性为"kw"的输入控件,然后调用它的TypeText方法
//*模拟用户用键盘输入"WatiN"
ieBaidu.TextField(Find.ById("kw")).TypeText("WatiN");
//找到搜索按钮,并点击
//*在浏览器中查找Id属性为"su"的输入控件,然后调用它的Click方法
//*模拟用户用鼠标点击
ieBaidu.Button(Find.ById("su")).Click();
//判断浏览器中是否包含了WatiN
Ret =ieBaidu.ContainsText("WatiN");
return Ret;
}
运行一下,看看效果吧,是不是自动打开IE,然后打开百度在搜索框上填入了关键词,点击
搜索,看到搜索结果了呢?
控制浏览器就是这么简单的几句话就完成了。
3 元素分析
3.1 Qaliber Developer Standalone元素分析
有人会说了,你怎么知道百度页面的输入框的Id是“kw”,按钮Id是“su”呢?现在可以给大家
带来答案。
我们QAliber Developer Standalone, 可以抓取IE浏览器上的元素。启动IE,打开百度(3-1-
1);启动QAliber Developer Standalone(3-1-2),可以查看到http://www.baidu.com网页下面的
所有元素控件的集合,这些控件的集合是依照控件类型来区分。
华为机密,未经许可不得扩散 第7页,共33页
SDB委托开发测试项目 开发使用手册 机密
3-1-1
3-1-2
我们双击展开TextFields,依次右击在弹出菜单选择【highlight control】,可以找出对应的控
件“输入框”查看右边的属性栏,可以看到ID是“kw”,还有name、class等这些属性(3-1-3);
3-1-3
双击展开Buttons,右击在弹出菜单选择【highlight control】,找出百度一下的“按钮”,查
看右边属性栏,可以看到ID是“su”(3-1-4);
华为机密,未经许可不得扩散 第8页,共33页
SDB委托开发测试项目 开发使用手册 机密
3-1-4
3.2 开发人员工具元素分析
Qaliber能够抓取网页元素,还有其他工具可以辅助增强抓取网页元素:IE自带的开发人员工
具。启动IE,【工具】---开发人员工具(3-1-5),可以打开工具(3-1-6)
3-1-5
3-1-6
华为机密,未经许可不得扩散 第9页,共33页
SDB委托开发测试项目 开发使用手册 机密
我们点击【单击选择元素】,然后点击“输入框”,输入框会高亮,并且在下面显示<HTML>代
码:
<input name="wd" class="s_ipt" id="kw" type="text" maxLength="100" autocomplete="off"/>
可以看到id是“kw”,还有一些其他属性name是“wd”,class是“s_ipt”。
3-1-7
点击【单击选择元素】,然后点击“百度一下按钮”,输入框会高亮,并且在下面显示<HTML>
代码:
<input class="s_btn" id="su" onmouseout="this.className='s_btn'"
onmousedown="this.className='s_btn s_btn_h'" type="submit"
value="百度一下"/>
可以看到id是“su”。
3-1-8
华为机密,未经许可不得扩散 第10页,共33页
SDB委托开发测试项目 开发使用手册 机密
4 WatiN 自动化开发
接下来我们要介绍一些查找窗口控件、操作控件。
4.1 WatiN主要命名空间
WatiN.Core: Watin 的核心部分,与浏览器的交互都包含在此模块中。
WatiN.Core.DialogHandlers: 该命名空间提供用于操 作浏览器对话框的对象。其中包括
AlertDialogHandler, ConfirmDialogHandler, FileUploadDialogHandler, PrintDialogHandler, and
LogonDialogHandler 。
WatiN.Core.Exceptions: 该 命 名 空 间 里 的 对 象 用 于 处 理 各 种 异 常 , 比 如 :
ElementNotFoundException, IENotFoundException, TimeoutException, and a generic WatiNException
4.2 WatiN 常用的类和方法
IE 类 : 最基础,最重要的一个类,用于访问 Web 页面中的元素以及操纵 IE 浏览器。
Goto(string): 导航到某个地址的网页。
Close() : 关闭浏览器
ContainsText(string): 是否包含指定的文本文字
CaptureWebPageToFile(string): 对当前 web 页截图并保存到指定位置
ClearCache(): 清除 Cache
ClearCookie(): 清除 Cookie
………
Find 类 : 也是必须用到的一个类,用于查找并定位 web 页面中的元素。
ById() : 通过元素的 id 属性进行查找
ByName(): 通过元素的 name 属性进行查找
………
4.3 WatiN 常用页面元素的操作
<A> 元素
<a> 标签对应 DOM 中的 Anchor 对象。表示 Html 中的超链接。
华为机密,未经许可不得扩散 第11页,共33页
SDB委托开发测试项目 开发使用手册 机密
< a id ="baidu" href = "http://wwww.baidu.com"> 百度 </ a >
Watin 操作 <a> 元素的代码如下
ie.Link(Find .Byid("baidu" )).Text;//返回超链接文字
ie.Link(Find .Byid("baidu" )).Url;//返回超链接的Url
ie.Link(Find .Byid("baidu" )).Click();//点击超链接
备注:有些开发不规范的 web 页,有可能只有 href 属性存在的现象,这时只有通过 Find.ByUrl ( url )进行查找。
<input type="text"> 元素
<input type="text"> 元素对应 HTML DOM 中的 Text 对象。表示一个单行的标准输入框。
华为机密,未经许可不得扩散 第12页,共33页
SDB委托开发测试项目 开发使用手册 机密
华为机密,未经许可不得扩散 第13页,共33页
SDB委托开发测试项目 开发使用手册 机密
< input id ="Text1" type ="text" />欢迎!</ p >
Watin 操作 <a> 元素的代码如下
ie.TextField (Find .Byid(" Text1 " )).Value;//返回输入框中的文字
ie.TextField (Find .Byid(" Text1 " )).TypeText("Watin" );//输入框中输入 ” Watin ”
<input type="button"> 元素
<input type="button"> 元素对应 HTML DOM 中的 Button 对象。表示一个按钮
华为机密,未经许可不得扩散 第14页,共33页
SDB委托开发测试项目 开发使用手册 机密
华为机密,未经许可不得扩散 第15页,共33页
SDB委托开发测试项目 开发使用手册 机密
< input id ="btn" type ="button" />登录</ p >
Watin 操作 <a> 元素的代码如下
Ie.Button (Find .Byid(" btn " )).Text;//返回按钮中的文字
ie.Button (Find .Byid(" btn " )).Click();//点击按钮
<input type="checkbox"> 元素
<input type="Checkbox"> 元素对应 HTML DOM 中的 Checkbox 对象。表示一个选择框
< input id ="chk" type ="checkbox" />勾选</ p >
Watin 操作 <a> 元素的代码如下
ie.CheckBox (Find .Byid(" chk " )).Text;//返回选择框的文字
ie.CheckBox (Find .Byid(" chk " )).Click();//点击选择框
ie.CheckBox (Find .ById(" chk " )).Checked =true ;//设置Checkbox的勾选状态
<area> 元素
<area> 标签对应 HTML DOM 中的 area 对象。表示图像映射的一个区域(图像映射指的是带
有可点击区域的图像) < img src ="map/map.jpg" alt =" 校 区地图 " width ="1004" height ="601"
border ="0" usemap ="#Map" />
< map name ="Map" id ="Map">
< area shape ="rect" alt ="a1" coords ="134,59,255,92" href
="edushiMap.html" />
< area shape ="rect" alt ="a2" coords ="345,122,464,160" href
="edushiMap.html" />
</ map >
Watin 操作 <area> 元素的代码如下 ie.Area(Find .ByAlt("a1" )).Url;//返回area区域的url
ie.Area(Find .ById("area1" )).Click();//点击area区域
ie.Area(Find .ByAlt("a1" )).Click();//点击area区域
<Select> 元素
<Select> 元素对应 HTML DOM 中的 Select 对象。表示一个下拉列表
华为机密,未经许可不得扩散 第16页,共33页
SDB委托开发测试项目 开发使用手册 机密
< select id ="Select1">
< option > 1</ option >
< option > 2</ option >
</ select >
Watin 操作 <Select> 元素的代码如下
ie.SelectList(Find .ById("Select1" )).Select(string);//返回选择框的某项
华为机密,未经许可不得扩散 第17页,共33页
SDB委托开发测试项目 开发使用手册 机密
<table> 元素
<table> 元素对应 HTML DOM 中的 table 对象。表示一个表格 < table style =" width :100% ;" id ="table1" class = "class1">
< tr >
< td class ="style1">
第1行第1列</ td >
< td class ="style1">
第1行第2列</ td >
</ tr >
< tr >
< td >
第2行第1列</ td >
< td >
第2行第2列</ td >
</ tr >
</ table >
华为机密,未经许可不得扩散 第18页,共33页
SDB委托开发测试项目 开发使用手册 机密
Watin 操作 <table> 元素的代码如下
华为机密,未经许可不得扩散 第19页,共33页
SDB委托开发测试项目 开发使用手册 机密
ie.Table(Find .ByClass("class1" )).TableRows[0].TableCells[1].Text;//返回表格中项的数据
华为机密,未经许可不得扩散 第20页,共33页
SDB委托开发测试项目 开发使用手册 机密
4.4 WatiN页面弹出窗口 , 对话框 , 提示框的处理
处理弹出窗口
首先创建一个 web 页,用于演示弹出窗口。
华为机密,未经许可不得扩散 第21页,共33页
SDB委托开发测试项目 开发使用手册 机密
< input id ="Button1" type ="button" value ="button" onclick
="openwindow()" />
< script type ="text/javascript">
function openwindow()
{
window.open("http://localhost/Test/test2.htm" );
}
</ script >
上述代码 , 点击“ Button1 ”后 , 弹出窗口 test2.htm. 我们要做的是如何处理 test2.htm 页面,
Watin 处理代码如下 : IE ie =new IE ("http://localhost/Test/" );
//点击按钮,打开新窗口test2
ie.Button(Find .ById("Button1" )).Click();
//查找新窗口test2并赋给新的IE对象
IE newie =IE .AttachTo<IE >(Find .ByTitle("test2" ));
//使用新的IE对象就可以继续对新窗口进行操作了
newie.TextField(Find .ById("Text1" )).TypeText("this is new ie" );
处理 confirm 弹出框
首先创建一个 web 页,用于演示 confirm 对话框。 < input id ="myButton1" type ="button" value ="this is a button"
onclick ="confirmMe(); return false;">< br >
< script >
function confirmMe() {
var answer = confirm ("Are you having fun?" )
if (answer)
document.getElementById("myButton1" ).value="Clicked OK" ;
else
document.getElementById("myButton1" ).value="Clicked Cancel" ;
}
</ script >
华为机密,未经许可不得扩散 第22页,共33页
SDB委托开发测试项目 开发使用手册 机密
相应的 Watin 测试 代码如下 :
华为机密,未经许可不得扩散 第23页,共33页
SDB委托开发测试项目 开发使用手册 机密
华为机密,未经许可不得扩散 第24页,共33页
SDB委托开发测试项目 开发使用手册 机密
IE ie =new IE ("http://localhost/Test/" );
//创建一个ConfirmDialogHandler对象
ConfirmDialogHandler cdh =new ConfirmDialogHandler ();
//ConfirmDialogHandler对象与ie建立关联
ie.AddDialogHandler(cdh);
//点击按钮,这里需要注意的是:使用的是ClickNoWait()方法
ie.Button(Find .ById("myButton1" )).ClickNoWait();
//等待Confirm对话框出现
cdh.WaitUntilExists();
//点击Confirm对话框的OK按钮
cdh.OKButton.Click();
ie.WaitForComplete();
//ConfirmDialogHandler对象与ie取消关联
ie.RemoveDialogHandler(cdh);
Console .WriteLine(ie.Button(Find .ById("myButton1" )).Value);
Console .ReadLine();
处理 Alert 弹出框
首先创建一个 web 页,用于演示 Alert 对话框。
华为机密,未经许可不得扩散 第25页,共33页
SDB委托开发测试项目 开发使用手册 机密
< input id ="myButton2" type ="button" value ="this is a button2"
onclick ="alertMe(); return false;">< br />
< script >
function alertMe() {
var answer = alert("this is a alert dialog" );
if (answer)
document.getElementById("myButton2" ).value="Clicked OK" ;
}
</ script >
华为机密,未经许可不得扩散 第26页,共33页
SDB委托开发测试项目 开发使用手册 机密
相应的 Watin 测试代码如下 :
IE ie =new IE ("http://localhost/Test/" );
//创建一个AlertDialogHandler对象
AlertDialogHandler adh =new AlertDialogHandler ();
//ConfirmDialogHandler对象与ie建立关联
ie.AddDialogHandler(adh);
//点击按钮,这里需要注意的是:使用的是ClickNoWait()方法
ie.Button(Find .ById("myButton2" )).ClickNoWait();
//等待Alert对话框出现
adh.WaitUntilExists();
//点击Alert对话框的OK按钮
adh.OKButton.Click();
ie.WaitForComplete();
//AlertDialogHandler对象与ie取消关联
ie.RemoveDialogHandler(adh);
Console .WriteLine(ie.Button(Find .ById("myButton2" )).Value);
Console .ReadLine();
处理 Fileupload 弹出框
首先创建一个 web 页,用于演示 Fileupload 对话框。 < input id ="File1" type ="file" /></ p >
相应的 Watin 测试代码如下 :
华为机密,未经许可不得扩散 第27页,共33页
SDB委托开发测试项目 开发使用手册 机密
华为机密,未经许可不得扩散 第28页,共33页
SDB委托开发测试项目 开发使用手册 机密
IE ie =new IE ("http://localhost/Test/test2.htm" );
//打开文件对话框并加载指定文件
ie.FileUpload(Find .ById("File1" )).Set(@"D:/Data.txt" );
华为机密,未经许可不得扩散 第29页,共33页
SDB委托开发测试项目 开发使用手册 机密
5 实战实例
实例:linkedin登陆
1、分析Linkedin:
信息1:输入网址是:http://www.linkedin.com/(5-1)
5-1
信息2:登陆框,name:session_key,id:session_key-login,class:。(5-2)
5-2
信息2:密码框,name:session_password,id:session_password-login,class:.。(5-3)
华为机密,未经许可不得扩散 第30页,共33页
SDB委托开发测试项目 开发使用手册 机密
5-3
信息3:登陆按钮,name:,id:btn-login,class:btn-secondary。(5-4)
5-4
2、模拟操作:
代码如下:
//打开linkedin网站
IE ie = new IE("http://www.linkedin.com/", true);
//找到登陆框,并且键入用户名
ie.TextField(Find.ByName("session_key")).TypeText("[email protected]");
//找到密码框,并且键入密码
ie.TextField(Find.ById("session_password-login")).TypeText("dpidev");
//找到登陆按钮,点击登陆
ie.Button(Find.ByClass("btn-secondary")).Click();
附录
华为机密,未经许可不得扩散 第31页,共33页
SDB委托开发测试项目 开发使用手册 机密
附录一,网页元素对照表
Html element WatiN Class WatiN Collection Example
<a /> Link LinkCollection Ie.Link(linkId)
<area /> Area AreaCollection ie.Area(Find.ByAlt(alttext))
<button /> Button ButtonCollection Ie.Button(buttonId)
<div /> Div DivCollection Ie.Div(divId)
<form /> Form FormCollection Ie.Form(formId)
<frame /> Frame FrameCollection Ie.Frame(frameId)
<frameset /> - FrameCollection Ie.Frames
<iframe /> Frame FrameCollection Ie.Frame(iframeId)
<img /> Image ImageCollection Ie.Image(imageId)
<input type=button/> Button ButtonCollection Ie.Button(buttonId)
<input type=checkbox/> CheckBox CheckBoxCollection Ie.CheckBox(checkboxId)
<input type=file/> FileUpload FileUploadCollection Ie.FileUpload(fileuploadId)
<input type=hidden/> TextField TextFieldCollection Ie.TextField(hiddenId)
<input type=image/> Button ButtonCollection Ie.Button(imageId)
<input type=image/> Image ImageCollection Ie.Image(imageId)
<input type=password/> TextField TextFieldCollection Ie.TextField(passwordId)
<input type=radio/> RadioButton RadioButtonCollection Ie.RadioButton(radioId)
<input type=reset/> Button ButtonCollection Ie.Button(resetId)
<input type=submit/> Button ButtonCollection Ie.Button(submitId)
<input type=text/> TextField TextFieldCollection Ie.TextField(textId)
<label /> Label LabelCollection Ie.Label(elementId)
<option /> Option OptionCollection Ie.Select(selectId).Options
华为机密,未经许可不得扩散 第32页,共33页
SDB委托开发测试项目 开发使用手册 机密
<p /> Para ParaCollection Ie.Para(pId)
<select /> Select SelectCollection Ie.Select(selectId)
<span /> Span SpanCollection Ie.Span(spanId)
<table /> Table TableCollection Ie.Table(tableId)
<tbody /> TableBody TableBodyCollection Ie.TableBody(tablebodyId)
Ie.Table(tableid).TableBodies
<td /> TableCell TableCellCollection Ie.TableCell(tablecellId) or
Ie.Table(TableId).TableRows[0].TableCells[0
]
<textarea /> TextField TextFieldCollection Ie.TextField(textareaId)
<tr /> TableRow TableRows Ie.TableRow(tablerowId) or
Ie.Table(TableId).TableRows[0]
All elements, also the
ones not mentioned in
this list
Element and
ElementsContainer
ElementCollection Ie.Element(elementId)
Ie.Element(tagname, elementId)
华为机密,未经许可不得扩散 第33页,共33页