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

发表于:2008-06-25来源:作者:点击数: 标签:tddTDD自动化框架Web
尝试使用Web 自动化测试 框架WatiN进行 TD D 作者: 未知 来源: 网络 转载 这两天听说了一个很不错的基于.NET平台的Web自动化 测试框架 WatiN,下载试用了一下,的确很好用。它的基本功能和 Selenium 有点像,但是不如Selenium强大,没有脚本录制,只支持IE6/


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

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


        这两天听说了一个很不错的基于.NET平台的Web自动化测试框架WatiN,下载试用了一下,的确很好用。它的基本功能和Selenium有点像,但是不如Selenium强大,没有脚本录制,只支持IE6/7等。它基本功能包括自动操作大部分的HTML元素,多种查找方式,支持AJAX,支持frame/iframe,支持弹出框等等。现在用一个简单的例子来看看怎样使用WatiN来进行TDD

        在这个例子中,基于Northwind数据库实现这样一个功能,通过ID查找某个Customer,列出相关基本信息,然后能够查找他关联的所有Order。现在就来一步一步实现吧。(开发工具是Visual Studio 2008 beta2, 测试框架包括NUnit和WatiN)

(回忆一下测试驱动开发的几个步骤:写测试 --> 测试失败 --> 编写实现 --> 测试通过 --> 如有重复代码进行重构 --> 重复)

        在这里我将上面的功能分为两步来实现:1. 查找Customer,如果找到就列出信息。 2. 查找关联的Order。下面先实现第一个部分:

        先想想该页面需要哪些控件,一个输入框,用来输入Customer的ID;一个按钮,用来查找动作,点击按钮以后,如果找到该Customer,就列出他的ID和他的Company Name。

  [TestFixture]
  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();
      }

  }

        简单解释一下,首先创建一个IE,进入页面,然后查找ID为"tb_customerID"的文本框,输入文字"ALFKI",然后查找ID为"btn_find_customer"的按钮并点击。接下来的两个断言表示页面应该出现"ALFKI"即Customer的ID和"Alfreds Futterkiste"即该Customer的Company Name。最后关闭IE。

        运行测试,由于现在还没有创建该页面,测试很明显不能通过。错误信息是没有找到这个文本框

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

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


 

原文转自:http://www.ltesting.net