您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源功能測(cè)試工具 > Watir
用Web自動(dòng)化測(cè)試框架WatiN進(jìn)行TDD
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/12 16:10:52 ] 推薦標(biāo)簽:

這兩天聽說(shuō)了一個(gè)很不錯(cuò)的基于.NET平臺(tái)的Web自動(dòng)化測(cè)試框架WatiN,下載試用了一下,的確很好用。它的基本功能和Selenium有點(diǎn)像,但是不如Selenium強(qiáng)大,沒有腳本錄制,只支持IE6/7等。它基本功能包括自動(dòng)操作大部分的HTML元素,多種查找方式,支持AJAX,支持frame/iframe,支持彈出框等等。現(xiàn)在用一個(gè)簡(jiǎn)單的例子來(lái)看看怎樣使用WatiN來(lái)進(jìn)行TDD。

在這個(gè)例子中,基于Northwind數(shù)據(jù)庫(kù)實(shí)現(xiàn)這樣一個(gè)功能,通過(guò)ID查找某個(gè)Customer,列出相關(guān)基本信息,然后能夠查找他關(guān)聯(lián)的所有Order。現(xiàn)在來(lái)一步一步實(shí)現(xiàn)吧。(開發(fā)工具是Visual Studio 2008 beta2, 測(cè)試框架包括NUnit和WatiN)

(回憶一下測(cè)試驅(qū)動(dòng)開發(fā)的幾個(gè)步驟:寫測(cè)試 --> 測(cè)試失敗 --> 編寫實(shí)現(xiàn) --> 測(cè)試通過(guò) --> 如有重復(fù)代碼進(jìn)行重構(gòu) --> 重復(fù))

在這里我將上面的功能分為兩步來(lái)實(shí)現(xiàn):1. 查找Customer,如果找到列出信息。 2. 查找關(guān)聯(lián)的Order。下面先實(shí)現(xiàn)第一個(gè)部分:

先想想該頁(yè)面需要哪些控件,一個(gè)輸入框,用來(lái)輸入Customer的ID;一個(gè)按鈕,用來(lái)查找動(dòng)作,點(diǎn)擊按鈕以后,如果找到該Customer,列出他的ID和他的Company Name

public class FindCustomerAndOrders

{

[Test]

public void ShouldFindCustomer()

{

IE ie = new IE("http://localhost:1781/Default.aspx");

ie.TextField(Find.ById("tb_customerID")).TypeText("ALFKI");

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

Assert.That(ie.ContainsText("ALFKI"), Is.True);

Assert.That(ie.ContainsText("Alfreds Futterkiste"), Is.True);

ie.Close();

}

}

簡(jiǎn)單解釋一下,首先創(chuàng)建一個(gè)IE,進(jìn)入頁(yè)面,然后查找ID為"tb_customerID"的文本框,輸入文字"ALFKI",然后查找ID為"btn_find_customer"的按鈕并點(diǎn)擊。接下來(lái)的兩個(gè)斷言表示頁(yè)面應(yīng)該出現(xiàn)"ALFKI"即Customer的ID和"Alfreds Futterkiste"即該Customer的Company Name。后關(guān)閉IE。

運(yùn)行測(cè)試,由于現(xiàn)在還沒有創(chuàng)建該頁(yè)面,測(cè)試很明顯不能通過(guò)。錯(cuò)誤信息是沒有找到這個(gè)文本框

現(xiàn)在的目的是要使測(cè)試通過(guò),現(xiàn)在便創(chuàng)建這個(gè)頁(yè)面,并且添加相應(yīng)的代碼:

 

Visible="false">

CustomerID:


Company Name:

aspx.cs:

protected void Page_Load(object sender, EventArgs e)

{

btn_find_customer.Click += new EventHandler(btn_find_customer_Click);

}

void btn_find_customer_Click(object sender, EventArgs e)

{

lbl_customerID.Text = "ALFKI";

lbl_companyName.Text = "Alfreds Futterkiste";

pnl_customerInfo.Visible = true;

}

再次運(yùn)行測(cè)試,看見綠條,測(cè)試通過(guò)。不過(guò)這里只是一個(gè)假實(shí)現(xiàn)并沒有真正實(shí)現(xiàn)查找功能,我們需要對(duì)測(cè)試進(jìn)行修改使之更加完善。

IE ie = new IE("http://localhost:1781/Default.aspx");

ie.TextField(Find.ById("tb_customerID")).TypeText("ALFKI");

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

Assert.That(ie.ContainsText("ALFKI"), Is.True);

Assert.That(ie.ContainsText("Alfreds Futterkiste"), Is.True);

ie.TextField(Find.ById("tb_customerID")).TypeText("AROUT");

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

Assert.That(ie.ContainsText("AROUT"), Is.True);

Assert.That(ie.ContainsText("Around the Horn"), Is.True);

ie.Close();

上一頁(yè)123下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd