本文來自http://blog.csdn.net/zxcred
前段時間寫網站,寫到結束階段,發現每改一個bug,都要重新開啟每個頁面測試頁面能否開啟,並且開啟頁面後進行一些操作測試能否正常進行。每次部署後,發現新Bug修複後,都要做一遍功能測試。
先普及一下什麼是功能測試吧。簡單的說功能測試主要是參照使用者手冊,看看能不能完成所預先設計的功能狀態,有點類似於從使用者使用的角度來做測試。比如一個網站登陸的功能,做功能測試,我們需要驗證能否開啟登陸的頁面,輸入使用者名稱,密碼,看是否成功….
WatiN是開源的C#庫,利用它我們可以把需要手工做的功能測試自動化。
下面的網址是watiN的首頁:
http://watin.sourceforge.net/
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,如下:
- [TestMethod]
- 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 element |
WatiN Class |
WatiN Collection |
Example |
Version |
|
|
|
|
|
<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完成一些常用的,基本的功能測試了。
本文來自
http://blog.csdn.net/zxcred