qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

测试框架:利用WatiN自动化网站功能测试

 网站到结束阶段,发现每改一个bug,都要重新打开每个页面测试页面能否打开,并且打开页面后进行一些操作测试能否正常进行。每次部署后,发现新Bug修复后,都要做一遍功能测试
  先普及一下什么是功能测试吧。简单的说功能测试主要是参照用户手册,看看能不能完成所预先设计的功能状态,有点类似于从用户使用的角度来做测试。比如一个网站登陆的功能,做功能测试,我们需要验证能否打开登陆的页面,输入用户名,密码,看是否成功….
  WatiN是开源的C#库,利用它我们可以把需要手工做的功能测试自动化。
  watiN官方下载地址:
  http://sourceforge.net/project/showfiles.php?group_id=167632
  下载后解压,假设解压到: c:\Program Files\WatiN\,
  在c:\Program Files\WatiN\bin\目录下有个watiN.Core.dll文件,这个是最主要的动态链接库。
  咱们先用VS新建一个Test Project吧,不会的请看我博客上的前一篇文章
  利用VisualStudio白盒测试入门
  当然我们也可以不建Test Project,建其他的Console Application或者Windows Application都是可以的。
  建好工程后,添加对WatiN.Core.dll的引用(Add Reference).
  在代码中添加命名控件的引用:
  using WatiN.Core;
  然后新加一个Test Method,如下:
public void TestLoginFailed()
{
using (IBrowser ie = BrowserFactory.Create(BrowserType.InternetExplorer))
{
ie.GoTo("http://www.google.com");
ie.TextField(Find.ByName("q")).Value = "WatiN";
ie.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(ie.ContainsText("WatiN"));
}
}
  呵呵,运行一下,看看效果吧,是不是自动打开IE,然后打开google在搜索框上填入了关键词,点击搜索,看到搜索结果了呢?
  控制浏览器就是这么简单的几句话就完成了。



  估计有人会说了,你怎么知道google页面的输入框的名字是“q”,按钮名字是“btnG”呢? 看网页源代码去吧。
  呵呵,有个很好用的工具,对我们分析网页很方便。他就是 IE Developer Toolbar
  官方下载地址:
  http://www.microsoft.com/downloads/details.aspx?FamilyID=e59c3964-672d-4511-bb3e-2d5e1db91038&displaylang=en
  安装完之后,打开IE,可以看到如下图所示的东东:
  用这个我们就可以很方便的得到自己所需要的网页元素的各种属性了。
  我最近就爱上它了,看上哪个网站的网页做得好,自己想照搬,就用这个工具,很简单就搞定了。比起自己去研究他的网页源代码方便多了。
  废话少说,有了IE Developer ToolBar之后,我们剩下的用WatiN的测试工作就是打开网页,找到网页标签,对标签进行控制,然后验证结果。这个是最基本的操作了。下面大概看一下做这些操作用到的几个类和方法吧。
  1. 打开网页
  BrowserFactory.Create() 创建一个浏览器,可以是IE,可以是Firefox,很可惜不支持Opera,
  IBrowser Interface, 浏览器接口。
  IBrowser.GoTo() 转到某个URL去
  示例代码请看上面的代码

字体:        | 上一篇 下一篇 | 打印  | 我要投稿 

  2. 找到网页上的各种元素
  我们利用IE Developer ToolBar可以找到各种元素,那么HTML里的网页元素如何和WatiN里的类联系上来呢,请看下表:
Html elementWatiN ClassWatiN CollectionExampleVersion

<a />

Link

LinkCollection

Ie.Link(linkId)

0.7

<area />

Area

AreaCollection

ie.Area(Find.ByAlt(alttext))

1.2

<button />

Button

ButtonCollection

Ie.Button(buttonId)

0.9

<div />

Div

DivCollection

Ie.Div(divId)

0.7

<form />

Form

FormCollection

Ie.Form(formId)

0.7

<frame />

Frame

FrameCollection

Ie.Frame(frameId)

0.7

<frameset />

-

FrameCollection

Ie.Frames

0.7

<iframe />

Frame

FrameCollection

Ie.Frame(iframeId)

0.9

<img />

Image

ImageCollection

Ie.Image(imageId)

0.7

<input type=button/>

Button

ButtonCollection

Ie.Button(buttonId)

0.7

<input type=checkbox/>

CheckBox

CheckBoxCollection

Ie.CheckBox(checkboxId)

0.7

<input type=file/>

FileUpload

FileUploadCollection

Ie.FileUpload(fileuploadId)

0.9

<input type=hidden/>

TextField

TextFieldCollection

Ie.TextField(hiddenId)

0.7

<input type=image/>

Button

ButtonCollection

Ie.Button(imageId)

0.7

<input type=image/>

Image

ImageCollection

Ie.Image(imageId)

0.9.5

<input type=password/>

TextField

TextFieldCollection

Ie.TextField(passwordId)

0.7

<input type=radio/>

RadioButton

RadioButtonCollection

Ie.RadioButton(radioId)

0.7

<input type=reset/>

Button

ButtonCollection

Ie.Button(resetId)

0.7

<input type=submit/>

Button

ButtonCollection

Ie.Button(submitId)

0.7

<input type=text/>

TextField

TextFieldCollection

Ie.TextField(textId)

0.7

<label />

Label

LabelCollection

Ie.Label(elementId)

0.7

<option />

Option

OptionCollection

Ie.Select(selectId).Options

1.0

<p />

Para

ParaCollection

Ie.Para(pId)

0.7

<select />

Select

SelectCollection

Ie.Select(selectId)

0.7

<span />

Span

SpanCollection

Ie.Span(spanId)

0.7

<table />

Table

TableCollection

Ie.Table(tableId)

0.7

<tbody />

TableBody

TableBodyCollection

Ie.TableBody(tablebodyId)

Ie.Table(tableid).TableBodies

1.0

<td />

TableCell

TableCellCollection

Ie.TableCell(tablecellId) or

Ie.Table(TableId).TableRows[0].TableCells[0]

0.7

<textarea />

TextField

TextFieldCollection

Ie.TextField(textareaId)

0.7

<tr />

TableRow

TableRows

Ie.TableRow(tablerowId) or

Ie.Table(TableId).TableRows[0]

0.7

All elements, also the ones not mentioned in this list


Element and

ElementsContainer

ElementCollection

Ie.Element(elementId)

Ie.Element(tagname, elementId)

0.9


1.2

  有这个表之后就很容易得到网页里各种元素对应的类了
  得到类之后查WatiN的帮助,就能知道每个类里有些什么函数,方法,怎么控制就看具体的类及其方法了。
  3.验证结果
  有很多种方法验证结果,因人而异了。不过我用得最多的就是ie.ContainsText()方法了。
  有了上面的基础,基本上可以用WatiN完成一些常用的,基本的功能测试了。

posted on 2013-12-04 11:16 顺其自然EVO 阅读(244) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2013年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜