Friday, May 24, 2013

Automating Integration Test with WatiN and Microsoft Test

A physicist is an atom's way of knowing about atoms. – George Wald



We also have that in software development field. The web developers are the way of the browsers to test itself.



Having said that, we should not be subjected to test the application correctness manually. We can also automate this kind of stuff



To cut to the chase, I used WatiN to test the application automatically. Automating integration test is also good for regression testing



It's very easy to use WatiN for test automation. Here's a sample test for doing integration test:


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;
  
namespace TestTheWatiN.Tests
{
    [TestClass]
    public class TheUnitTest
    {
        public void Logon(Browser browser)
        {
            // Arrange
            var uxUsername = browser.TextField(Find.ByName("LoginFields.Username"));
            var uxPassword = browser.TextField(Find.ByName("LoginFields.Password"));
            var uxLogin = browser.Link(Find.ById("loginButton"));
                 
            // Act
            uxUsername.TypeTextQuickly("mb");
            uxPassword.TypeTextQuickly("opensesame");
            uxLogin.Click();
  
            // Assert
            Assert.IsTrue(browser.ContainsText("Welcome"));
        }
  
  
        [TestMethod]
        [Timeout(10 * 60 * 1000)]
        public void TestSocial()
        {
            string devSite = "http://blah/DEV/Account/Login";
            // string qaSite = "http://blah/QA/Account/Login";
  
            string testSite = devSite;
  
            using (var b = new IE(testSite))
            {
                Logon(b);
                Social_test_if_navigation_is_working_properly(b);
                Social_post_then_check_if_message_is_added(b);
            }
        }
  
        public void Social_test_if_navigation_is_working_properly(Browser browser)
        {
            // Arrange
            var link = browser.Link(Find.BySelector(".sectionContainer a"));
  
            // Act
            link.Click();
  
            // Assert
            Assert.IsTrue(browser.ContainsText("Share"));
        }
  
         
        public void Social_post_then_check_if_message_is_added(Browser browser)
        {
            // Arrange
            const string placeholder = "Write your status...";           
            const string waitBeforeAssert = "Just Now";
  
            string expectedAdded = Guid.NewGuid().ToString() + " are like snowflakes";
  
            var uxTextToPost = browser.TextField(Find.ByName("PostedText"));
            var uxSave = browser.Link(Find.ById("SaveButton"));
  
             
            // Act
            uxTextToPost.Focus();
            uxTextToPost.TypeTextQuickly(expectedAdded);
            uxSave.Click();           
            browser.WaitUntilContainsText(waitBeforeAssert);
             
  
            // Assert           
            Assert.AreEqual(placeholder, uxTextToPost.Text);
            Assert.IsTrue(browser.ContainsText(expectedAdded));
        }
  
    }
  
    public static class WatiNExtensions
    {
        public static void TypeTextQuickly(this TextField textField, string text)
        {
            // Slow:
            // textField.TypeText(text);
  
            // Fast:
            textField.SetAttributeValue("value", text);
        }
    }
}


Happy Computing! ツ

No comments:

Post a Comment