-----box----- #程式1 using NUnit.Framework; using NUnit.Extensions.Asp; using NUnit.Extensions.Asp.AspTester; -----end----- -----box----- #程式2 [TestFixture] public class Class1 : WebFormTestCase { } -----end----- -----box----- #程式3 [Test] public void TestLayout() { //瀏覽localhost電腦上GuestBook虛擬目錄下的 //GuestBook.aspx網頁 Browser.GetPage( "http://localhost/GuestBook/GuestBook.aspx"); } -----end----- -----box----- #程式4 [Test] public void TestLayout() { //建立TextBoxTester類別的物件負責測試前網頁 //上名稱為name的TextBox控制項 TextBoxTester name = new TextBoxTester("name", CurrentWebForm); //建立TextBoxTester類別的物件負責測試前網頁 //上名稱為comments的TextBox控制項 TextBoxTester comments = new TextBoxTester( "comments", CurrentWebForm); //建立ButtonTester類別的物件負責測試前網頁 //上名稱為save的Button控制項 ButtonTester save = new ButtonTester("save", CurrentWebForm); //建立DataGridTester類別的物件負責測試前網頁 //上名稱為book的DataGrid控制項 DataGridTester book = new DataGridTester("book", CurrentWebForm); Browser.GetPage( "http://localhost/GuestBook/GuestBook.aspx"); } -----end----- -----box----- #程式5 [Test] public void TestLayout() { //建立TextBoxTester類別的物件負責測試前網頁上 //名稱為name的TextBox控制項 TextBoxTester name = new TextBoxTester("name", CurrentWebForm); //建立TextBoxTester類別的物件負責測試前網頁上 //名稱為comments的TextBox控制項 TextBoxTester comments = new TextBoxTester( "comments", CurrentWebForm); //建立ButtonTester類別的物件負責測試前網頁上 //名稱為save的Button控制項 ButtonTester save = new ButtonTester("save", CurrentWebForm); //建立DataGridTester類別的物件負責測試前網頁 //上名稱為book的DataGrid控制項 DataGridTester book = new DataGridTester("book", CurrentWebForm); Browser.GetPage( "http://localhost/GuestBook/GuestBook.aspx"); //測試目前網頁上名稱為name的TextBox控制項 //是否存在 AssertVisibility(name, true); //測試目前網頁上名稱為comments的TextBox //控制項是否存在 AssertVisibility(comments, true); //測試目前網頁上名稱為save的Button控制項 //是否存在 AssertVisibility(save, true); //測試目前網頁上名稱為book的DataGrid控制項 //是否存在 AssertVisibility(book, true); } -----end----- -----box----- #程式6 TextBoxTester name=null; TextBoxTester comments=null; ButtonTester save=null; DataGridTester book=null; [Test] public void TestLayout() { //建立TextBoxTester類別的物件負責測試前網頁上 //名稱為name的TextBox控制項 name = new TextBoxTester("name", CurrentWebForm); //建立TextBoxTester類別的物件負責測試前網頁上 //名稱為comments的TextBox控制項 comments = new TextBoxTester("comments", CurrentWebForm); //建立ButtonTester類別的物件負責測試前網頁上 //名稱為save的Button控制項 save = new ButtonTester("save", CurrentWebForm); //建立DataGridTester類別的物件負責測試前網頁 //上名稱為book的DataGrid控制項 book = new DataGridTester("book", CurrentWebForm); Browser.GetPage( "http://localhost/GuestBook/GuestBook.aspx"); //測試目前網頁上名稱為name的TextBox控制項 //是否存在 AssertVisibility(name, true); //測試目前網頁上名稱為comments的TextBox控制項 //是否存在 AssertVisibility(comments, true); //測試目前網頁上名稱為save的Button控制項 //是否存在 AssertVisibility(save, true); //測試目前網頁上名稱為book的DataGrid控制項 //是否存在 AssertVisibility(book, true); } -----end----- -----box----- #程式8 [Test] public void TestSave() { //將名稱為name的TextBoxTester類別的物件的 //Text屬性設定成"John" name.Text = "John"; //將名稱為comments的TextBoxTester類別的 //物件的Text屬性設定成"ABC" comments.Text = "ABC"; //呼叫名稱為save的ButtonTester類別的物件 //的Click方法 save.Click(); //判斷網頁上名稱為name的TextBox控制項的 //Text屬性的內容值是否為空白 AssertEquals("name錯誤", "", name.Text); //判斷網頁上名稱為comments的TextBox控制項 //的Text屬性的內容值是否為空白 AssertEquals("comments錯誤", "", comments.Text); } -----end----- -----box----- #程式9 private void Page_Load(object sender, System.EventArgs e) { //清除名稱為name的TextBox控制項上 //使用者輸入的內容 name.Text = ""; //清除名稱為comments的TextBox控制項 //上使用者輸入的內容 comments.Text = ""; } -----end----- -----box----- #程式10 [Test] public void TestSave() { //將名稱為name的TextBoxTester類別的物件 //的Text屬性設定成"John" name.Text = "John"; //將名稱為comments的TextBoxTester類別的 //物件的Text屬性設定成"ABC" comments.Text = "ABC"; //呼叫名稱為save的ButtonTester類別的物件 //的Click方法 save.Click(); //判斷網頁上名稱為name的TextBox控制項 //的Text屬性的內容值是否為空白 AssertEquals("name錯誤", "", name.Text); //判斷網頁上名稱為comments的TextBox控制項 //的Text屬性的內容值是否為空白 AssertEquals("comments錯誤", "", comments.Text); //測試網頁上名稱為book的DataGrid控制項是否有顯示 AssertVisibility(book, true); //準備測試的資料 string[][] expected = new string[][] { new string[] {"John", "ABC"} }; //判斷網頁上名稱為book的DataGrid控制項的內容 //和expected二維陣列的內容是否相等 AssertEquals("book錯誤", expected, book.TrimmedCells); } -----end----- -----box----- #程式11 private void save_Click(object sender, System.EventArgs e) { //從Session1取出使用者之前留下的訪客留言 DataTable table = (DataTable)Session["GuestBookData"]; //如果沒有任何留言,表示為第一次留言 if (table == null) { //建立儲存留言的DataTable類別的物件 table = new DataTable(); //為table加入一個名稱為Name,型態為string的欄位 table.Columns.Add(new DataColumn("Name", typeof(string))); //為table加入一個名稱為Comments,型態為string的欄位 table.Columns.Add(new DataColumn( "Comments", typeof(string))); } //建立DataRow類別的物件 DataRow row = table.NewRow(); //把新建的DataRow類別的物件的Name欄位 //設定成網頁上名稱為name的TextBox的控制 //項上使用者輸入的文字 row["Name"] = name.Text; //把新建的DataRow類別的物件的Comments //欄位設定成網頁上名稱為comments的TextBox //的控制項上使用者輸入的文字 row["Comments"] = comments.Text; //將做好的DataRow類別的物件加到table中 table.Rows.Add(row); //將table拿給網頁上名稱為book的DataGrid/控制項顯示 book.DataSource = table; //命令名稱為book的DataGrid控制項顯示資料 book.DataBind(); //把table放入Session Session["GuestBookData"] = table; //清除網頁上名稱為name的TextBox控制項上 //使用者輸入的文字 name.Text = ""; //清除網頁上名稱為comments的TextBox //控制項上使用者輸入的文字 comments.Text = ""; } -----end----- -----box----- #程式12 name.Text = ""; comments.Text = ""; -----end----- -----box----- #程式13 DataTable table = (DataTable)Cache["GuestBookData"]; 並把使用者輸入的留言資料放到Cache物件中: Cache["GuestBookData"] = table; -----end-----