尝试使用Web自动化测试框架WatiN进行TDD

发表于:2008-5-26 16:21

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

        现在的目的就是要使测试通过,现在便创建这个页面,并且添加相应的代码:

aspx:

<asp:TextBox runat="server" ID="tb_customerID"></asp:TextBox>
    &nbsp; &nbsp;
    <asp:Button runat="server" ID="btn_find_customer" Text="Find Customer" />
    <br />
    <br />
    <asp:Panel ID="pnl_customerInfo" runat="server" GroupingText="Customer Information"
        Visible="false">
        CustomerID:
        <asp:Label ID="lbl_customerID" runat="server"></asp:Label>
        <br />
        Company Name:
        <asp:Label ID="lbl_companyName" runat="server"></asp:Label>
    </asp:Panel>
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;
 }

        再次运行测试,看见绿条,测试通过。不过这里只是一个假实现并没有真正实现查找功能,我们需要对测试进行修改使之更加完善。

 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();

        运行测试,又会出现红条了,测试失败。现在要考虑实现一个真正的在数据库中的查找功能,怎么开始做呢?当然还是由测试开始,有了上面的基础,现在写的测试跨库可以稍微大点:

 [TestFixture]
 public class CustomerDAOTests
 {
     [Test]
     public void ShouldFoundCustomerByID()
     {
         string id = "ALFKI";
         string comName = "Alfreds Futterkiste";

         CustomerDAO customerDAO = new CustomerDAO();
         Customer found = customerDAO.FindCustomerByID(id);
        
         Assert.That(found, Is.Not.Null);
         Assert.That(found.CustomerID, Is.EqualTo(id));
         Assert.That(found.CompanyName, Is.EqualTo(comName));

         id = "AROUT";
         comName = "Around the Horn";
         found = customerDAO.FindCustomerByID(id);

         Assert.That(found, Is.Not.Null);
         Assert.That(found.CustomerID, Is.EqualTo(id));
         Assert.That(found.CompanyName, Is.EqualTo(comName));
     }
 }

        这段代码不能编译,因为并没有CustomerDAO这个类,所以得新增该类以及FindCustomerByID方法,而且上面的测试中已经包括了两个测试场景,现在可以直接写实现:

 public class CustomerDAO
 {
     public Customer FindCustomerByID(string id)
     {
         using (NorthwindDataContext ctx = new NorthwindDataContext())
         {
             IQueryable<Customer> customers = ctx.Customers.Where(c => c.CustomerID == id);
             if (customers.Count() > 0)
                 return customers.Single();
             else
                 return null;
         }
     }

 }

        运行一下该测试,通过!然后再将aspx.cs里面的代码进行改动使Web页面的测试通过

 void btn_find_customer_Click(object sender, EventArgs e)
 {
     string id = tb_customerID.Text;

     Customer c = customerDAO.FindCustomerByID(id);
     if (c == null)
         return;

     lbl_customerID.Text = c.CustomerID;
     lbl_companyName.Text = c.CompanyName;

     pnl_customerInfo.Visible = true;
 }

        不错,现在第一部分功能已经完成了,所有测试已经通过了,这时候我们可以打开浏览器,试试查找Customer的功能。

        回头看看刚才写的测试代码,有很多重复的地方,这是不好的,需要进行重构。这里也就不列出重构代码了。

        到我们实现第二部分的时候了,列出该用户相关的所有Order。在这里也不再详细些步骤了,就放出测试代码,实现的话还是很容易的 :) 当然测试并不完全,需要更加完善。

web页面测试代码:

 [Test]
 public void ShouldFindOrders()
 {
     string id = "ALFKI";

     ie.TextField(Find.ById("tb_customerID")).TypeText(id);
     ie.Button(Find.ById("btn_find_customer")).Click();
     ie.Button(Find.ById("btn_find_orders")).Click();

     Table ordersTable = ie.Table(Find.ById("grdv_orders"));

     Assert.That(ordersTable, Is.Not.Null);
     Assert.That(ordersTable.TableRows.Length, Is.EqualTo(6 + 1));
 }
DAO测试代码:

 [TestFixture]
 public class OrderDAOTests
 {
     [Test]
     public void ShouldFindOrdersByCustomerID()
     {
         string id = "ALFKI";

         OrderDAO orderDAO = new OrderDAO();
         List<Order> orders = orderDAO.FindOrdersByCustomerID(id);

         Assert.That(orders.Count, Is.EqualTo(6));
     }
 }

22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号