使用WatiN對ASP.NET頁面進行單元測試

jopen 10年前發布 | 12K 次閱讀 WatiN 測試工具

本文翻譯自:Unit Testing ASP.NET Pages Using WatiN

引言

單元測試是應用程序設計的一個重要部分,它可應用于程序的多個層次。本文將主要關注用戶界面層的單元測試。我們將使用WatiN 測試ASP.NET應用程序。

什么是WatiN?

WatiN 是一個源自Watir的工具,用于測試Web頁面。WatiN表示Web Application Testing in .NET。

我們要測試什么?

在本文中我們將測試一個簡單的ASP.NET頁面,用這個頁面來演示認同、接受(agreement acceptance)的場景。用戶在文本框中輸入名字,點擊“I agree”復選框,然后按下submit按鈕。這顯然是一個非常簡單的頁面,在你熟悉了WatiN框架的工作機制后,就可以將這里的理念用于大型頁面的測試了。

這里是待測頁面的截圖:

WatiNImage1

測試認同(Agreement)頁面:

向解決方案添加一個類庫項目,并為其添加對測試工具(我這里用的是MbUnit,但你完全可以使用NUnit或VSTS的測試項目)和WatiN庫的引用。你可以在這里下載WatiN。

下面的測試代碼用來確保用戶已經認同。

 [TestFixture(ApartmentState = ApartmentState.STA)]

public class TestAgreementPage

{

[Test]

public void TestCanAcceptUserAgreement()

{

    IE ie = new IE(ConfigurationManager.AppSettings["DefaultPageUrl"]);

    ie.TextField("txtName").TypeText("Mohammad Azam");

    ie.CheckBox("chkAgree").Checked = true;

    ie.Button("btnAgree").Click();



    Assert.AreEqual("Valid", ie.Span("lblMessage").Text);

}

}</pre>
</div>

這個類有TestFixture特性(Attribute),STA值確保該測試運行于STA(Single Threaded Apartment)狀態下。這是因為測試代碼要加載IE。

WatiN中的IE類完成了主要工作。IE類打開IE,通過name或id來引用html控件。這一行ie.TextField("txtName").TypeText("Mohammad Azam"),引用了id為“txtName”的文本框。瀏覽器加載后,WatiN會將值“Mohammad Azam”寫入id為“txtName”的文本框。這個過程在測試時你會看到的。然后id為“chkAgree”的復選框會被選中。最后,WatiN會按下提交按鈕,窗體被提交。

運行測試,失敗。因為名為“lblMessage”的Label從未被賦值為“Valid”。加上這段代碼:

 protected void btnAgree_Click(object sender, EventArgs e)

{

lblMessage.Text = "Valid";

}</pre>
</div>

現在,如果你運行測試它會通過。但是,好像不太正確。如果我們把這一行測試代碼刪掉:

ie.CheckBox("chkAgree").Checked = true;

再次運行測試,依然能通過。這可不對!應當只有在CheckBox選中時才可通過。將頁面的Code behind代碼改一下。

 protected void btnAgree_Click(object sender, EventArgs e)

{

if (chkAgree.Checked)

{

    lblMessage.Text = "Valid";

}

}</pre>
</div>

現在,測試只有在CheckBox選中時才可通過了。

以編程方式運行Web服務器:

在上例中我們需要運行WebServer,要么是通過命令行工具,要么是通過運行Web項目。但有時我們需要單元測試項目能夠動態打開一個WebServer。一起來看看。

首先,如果你需要打開ASP.NET內部服務器(WebDev.WebServer),可以使用命令行。語法如下:

WebDev.WebServer.exe /port:1950 /path:"C:\Projects\MyWebApplication"

需要定位到WebDev.WebServer所在的目錄,默認情況下它在:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe

好了,現在來看看如何在單元測試中打開服務器。首先,添加必要的配置(App.config中)。

 <configuration>

<appSettings>

    <add key="WebServerExePath" value="C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe"/>

    <add key="Port" value="4463"/>

    <add key="WebApplicationPath" value="c:\projects\demowatiN\demowatiN" />

    <add key="DefaultPageUrl" value="http://localhost:4463/Default.aspx" />

</appSettings>

</configuration></pre>
</div>

BaseTestPage類可以通過這些信息運行服務器,所有繼承了它的測試類都可以使用這個功能了。

下面是BaseTestPage類的完整代碼:

 public class BaseTestPage

{

static Process server = null;



static BaseTestPage()

{

    if (Process.GetProcessesByName("WebDev.WebServer").Length == 0)

    {

        string webServerExePath = (string)ConfigurationManager.AppSettings["WebServerExePath"];

        server = new Process();

        Process.Start(webServerExePath, GetWebServerArguments());

    }

}



public static string GetWebServerArguments()

{

    string args = String.Format("/port:{0} /path:\"{1}\"", GetPort(), GetWebApplicationPath());

    if (String.IsNullOrEmpty(args)) throw new ArgumentNullException("Arguments is not defined");

    return args;

}



public static string GetPort()

{

    string port = ConfigurationManager.AppSettings["Port"] as String;

    if (String.IsNullOrEmpty(port)) throw new ArgumentNullException("Port is null or empty");



    return port;

}



public static string GetWebApplicationPath()

{

    string webApplicationPath = ConfigurationManager.AppSettings["WebApplicationPath"] as String;

    if (String.IsNullOrEmpty(webApplicationPath)) throw new ArgumentNullException("WebApplicationPath is null or empty");



    return webApplicationPath;

}

}</pre>
</div>

如果服務器沒有運行,我們會新建一個進程運行它,否則就使用已有的進程。GetWebServerArguments()、GetPort()和GetWebApplicationPath()僅僅是輔助方法,可以提高可讀性。

最后,你可以讓單元測試類繼承該類:

public class TestAgreementPage : BaseTestPage

現在,運行單元測試項目時,它會運行WebServer,然后再執行所有測試。

結論:

在本文中,我們學習了如何對用戶界面層進行單元測試,這些測試可幫助我們理解UI的需求,并快速地看到基于用戶輸入所得到的結果。而如果手動進行測試,就要花費很多時間了。

源碼:點擊這里

</div> 作者:Anders Cui
出處:http://anderslly.cnblogs.com

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!