#副標=網站測試工具 #大標=好用的網站整合測試Framework #作者=王寧疆 ========box 範例 1========== public ActionResult Index() { ViewData["Message"] = "Welcome to ASP.NET MVC!"; return View(); } ==========end============== ========box 範例 2========== using MvcIntegrationTestFramework.Browsing; using MvcIntegrationTestFramework.Hosting; using NUnit.Framework; ==========end============== ========box 範例 3========== [Test] public void Root_Url_Renders_Index_View() { appHost.SimulateBrowsingSession(browsingSession => { RequestResult result = browsingSession.ProcessRequest("/"); //瀏覽網站根目錄 var viewResult = (ViewResult)result.ActionExecutedContext.Result; //取得瀏覽結果 Assert.AreEqual("Index", viewResult.ViewName); //判斷View的名稱是否為Index Assert.AreEqual("Welcome to ASP.NET MVC!", viewResult.ViewData["Message"]); //判斷View顯示的資料中,項目名稱為Message的內 //容是否為:Welcome to ASP.NET MVC! Assert.IsTrue(result.ResponseText.Contains(" { RequestResult initialRequestResult =    browsingSession.ProcessRequest(securedActionUrl);    //瀏覽受管制的網址 string loginRedirectUrl =   initialRequestResult.Response.RedirectLocation;   //取得網站重導後的網址 Assert.IsTrue(loginRedirectUrl.StartsWith("/Account/LogOn"), "Didn't redirect to logon page"); //判斷重導後的網址是否為:/Account/LogOn string loginFormResponseText = browsingSession.ProcessRequest( loginRedirectUrl).ResponseText;     //取得重導後的網址的內容 string suppliedAntiForgeryToken = MvcUtils.ExtractAntiForgeryToken( loginFormResponseText);      //取得網頁中__RequestVerificationToken的內容值 RequestResult loginResult =     browsingSession.ProcessRequest(loginRedirectUrl, HttpVerbs.Post, new NameValueCollection { { "username", "steve" }, { "password", "secret" }, { "__RequestVerificationToken", suppliedAntiForgeryToken } }); //模擬使用者於登入網頁輸入帳號與密碼進行登入 string afterLoginRedirectUrl = loginResult.Response.RedirectLocation;      //取得執行登入動作後的網址 Assert.AreEqual(securedActionUrl, afterLoginRedirectUrl, "Didn't redirect back to SecretAction"); //判斷是否為原先欲瀏覽的受管制的網址 ? RequestResult afterLoginResult = browsingSession.ProcessRequest( securedActionUrl);      //取得登入成功後的網頁的內容 Assert.AreEqual("Hello, you're logged in as steve", afterLoginResult.ResponseText);      //判斷內容是否出現登入成功的訊息 }); } ==========end============== ========box 範例5 ========== [TestFixture] public class MyIntegrationTests { private static readonly string mvcAppPath = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "\\..\\..\\..\\ASP.NET MVC網站專案的名稱"); //取得ASP.NET MVC網站的路徑 private readonly AppHost appHost=new AppHost(mvcAppPath);   //建立裝載ASP.NET MVC網站的物件 } ==========end============== ========box 範例 6========== copy "$(TargetDir)*.dll" "$(TargetDir)..\..\..\ASP.NET MVC網站專案名稱\bin\" ==========end============== ========box 範例7 ========== [WebTestClass] public class LoginTests //宣告LoginTests為測試類別 { [WebTestMethod] public void SignInAndSignOut() //測試登入和登出功能 { var page = new HtmlPage("Login.aspx"); //指定被測試的網頁為Login.aspx page.Elements.Find("UserName").SetText("User1"); //在網頁的UserName欄位填入:User1 page.Elements.Find("Password").SetText( "P@ssw0rd"); //在網頁的Password欄位填入:P@ssw0rd page.Elements.Find("LoginButton").Click(     WaitFor.Postback);     //模擬點選網頁上的LoginButton鍵,並等待Postback動作完成 var logoutLink = page.Elements.Find("LogoutHyperlink");    //取得網頁中執行登出的超連結 Assert.AreEqual("[登出]",     logoutLink.GetInnerText());     //判斷所取得的超連結的文字是否為:[登出] logoutLink.Click(WaitFor.Postback);    //模擬點選執行登出的超連結並等待PostBack動作完成 Assert.IsNotNull(page.Elements.Find(     "LoginButton")); //判斷網頁中存在名稱為LoginButton的按鍵 } [WebTestMethod] public void SignInFailed()  //使用錯誤的帳號和密碼進行測試 { var page = new HtmlPage("Login.aspx"); //指定被測試的網頁為Login.aspx page.Elements.Find("UserName").SetText( "someuser"); //在網頁的UserName欄位填入:someuser page.Elements.Find("Password").SetText(     "yourPW"); //在網頁的Password欄位填入:yourPW page.Elements.Find("LoginButton").Click( WaitFor.Postback); //模擬點選網頁上的LoginButton鍵,並等待Postback動作完成 var logoutLink = page.Elements.Find("LogoutHyperlink");    //取得網頁中執行登出的超連結 Assert.AreEqual("[登出]",     logoutLink.GetInnerText());     //判斷所取得的超連結的文字是否為:[登出] logoutLink.Click(WaitFor.Postback);    //模擬點選執行登出的超連結並等待PostBack動作完成 Assert.IsNotNull(page.Elements.Find(     "LoginButton"));     //判斷網頁中存在名稱為LoginButton的按鍵 } } ==========end============== ========box 範例8 ========== protected void LogoutHyperlink_Click (object sender, EventArgs e) { FormsAuthentication.SignOut(); //呼叫FormsAuthentication類別的SignOut方法登出網站 Session.Abandon(); //清除Session的內容 FormsAuthentication.RedirectToLoginPage(); //重導至網站中讓使用者執行登入網站的網頁 } ==========end============== ========box 範例9 ========== public void LoginError() { #region Variable Declarations HtmlEdit uI使用者名稱Edit = this.UIHttplocalhostLTAFTesWindow.UIHttplocalhostLTAFTesDocument.UI使用者名稱Edit; HtmlEdit uI密碼Edit = this.UIHttplocalhostLTAFTesWindow.UIHttplocalhostLTAFTesDocument.UI密碼Edit; HtmlInputButton uI登入Button = this.UIHttplocalhostLTAFTesWindow.UIHttplocalhostLTAFTesDocument.UI登入Button; #endregion // 在'使用者名稱:'文字方塊中輸入 'User1' uI使用者名稱Edit.Text = this.LoginErrorParams.UI使用者名稱EditText; // 在'密碼:' 文字方塊中輸 '********' uI密碼Edit.Password = this.LoginErrorParams.UI密碼EditPassword; // 按一下 '登入' 按鈕 Mouse.Click(uI登入Button, new Point(27, 10)); } ==========end==============