Use watin for TDD

Source: Internet
Author: User
I have heard of a very good web automation testing framework watin Based on the. NET platform over the past two days. I have downloaded and tried it for a while and it is really easy to use. Its basic functions are a bit like selenium, but not as powerful as Selenium. It does not have script recording and only supports IE6/7. Its basic functions include automatic operations on most HTML elements, multiple search methods, Ajax, frame/iframe, and pop-up boxes. Now we will use a simple example to see how to use watin for TDD.

In this example, a function is implemented based on the northwind database to search for a customer by ID, list the basic information, and then find all the associated orders. Now let's implement it step by step. (The development tool is Visual Studio 2008 beta2. The testing framework includes nunit and watin)

(Recall several steps of test-driven development: Write test --> test failed --> write implementation --> test passed --> rebuild if repeated code exists --> repeat)

Here, I divide the above functions into two steps: 1. Search for the customer, and list the information if it is found. 2. Search for the associated order. The first part is implemented as follows:

First, consider which controls are required on the page. An input box is used to enter the customer ID. A button is used to search for actions. After clicking the button, if the customer is found, to list his ID and 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();
   }

 }

 

For a brief explanation, first create an IE, enter the page, search for the text box with ID "tb_customerid", enter the text "alfki", then find the button with ID "btn_find_customer", and click. The following two assertions indicate that "alfki" is the customer ID and "Alfreds futterkiste" is the company name of the customer. Disable IE.

 

The purpose is to make the test pass. Now create this page and add the corresponding code:

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

Run the test again and see the green bar. The test passes. However, here is only a false implementation that does not actually implement the search function. We need to modify the test to make it more complete.

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

If you run the test, a red bar will appear again, and the test fails. Now we need to consider how to implement a real search function in the database? Of course, the test is still started. With the above foundation, the cross-database write test can be slightly larger:

 

 

 

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

This Code cannot be compiled. Because there is no customerdao class, you need to add this class and the findcustomerbyid method. In the above test, two test scenarios are included. Now you can directly write the implementation:

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

 }

 

Run the test and pass it! Then modify the code in Aspx. CS to pass the webpage test.

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

Yes, now the first part of the function has been completed and all tests have passed. Now we can open a browser and try the customer search function.

Looking back at the test code you just wrote, there are a lot of duplicates. This is not good and requires refactoring. The refactoring code is not listed here.

By the time we implement the second part, we will list all the orders related to this user. The test code is released without further detailed steps. It is easy to implement the Code. Of course, the test is not complete and needs to be improved.

Web page test code:

 [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 test code:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.