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

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

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

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

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

先想想該頁面需要哪些控件,一個輸入框,用來輸入Customer的ID;一個按鈕,用來查找動作,點擊按鈕以后,如果找到該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();

}

}

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

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

現(xiàn)在的目的是要使測試通過,現(xiàn)在便創(chuàng)建這個頁面,并且添加相應(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;

}

再次運行測試,看見綠條,測試通過。不過這里只是一個假實現(xiàn)并沒有真正實現(xià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();

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