Archive

Archive for January, 2013

HttpWatch Automation using C# and NUnit

January 17, 2013 Leave a comment

In web application development, web pages require the use of multiple HTTP requests to download HTML, graphics and JavaScript. I came across a tool named HttpWatch that shows what HTTP traffic is triggered when a webpage is accessed. This tool integrates with Internet Explorer and Firefox browsers.  I used to run my tests using this tool manually for every iterations of my testing.

In Agile, the number of test increases with the story points and this indeed increased my test efforts for upcoming iterations. So, I felt that there is a need for automation. This tool supports test automation using C# and Ruby. As I was working on a .Net project, I had to use C# as a programming language for setting up the automation framework.

SOFTWARE REQUIREMENTS:

  • Microsoft Visual Studio 2010 as IDE
  • NUnit as Unit Testing Tool
  • HttpWatch Installed

SET-UP INSTRUCTIONS:

Referencing the HttpWatch Automation Library:
  • Create a new Project with C# Class Library and save it (‘HttpWatchPerformance’ my example).
  • Rename the class as DriverScript.cs.
  • In Visual Studio, Go to the Project -> Add Reference menu item.
  • When the Add Reference dialog appears, click on the COM tab. Find the ‘HttpWatch Automation Library’ in the list and select it as shown in the figure:
 
Referencing the NUnit Library:
  • Download the latest NUnit framework from the site: http://www.nunit.org
  • Install the NUnit software on your machine.
  • In Visual Studio, Go to the Project -> Add Reference menu item.
  • When the Add Reference dialog appears, click on ‘Browse’ and navigate to C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\framework and select nunit.framework.dll.
  • Finally, the solution Explorer should look like this:
 

Now, it’s the time to write tests. Let me explain with a sample test scenario:

As an end user,
I would like to visit the google home page 
And then I would like to click on the 'Sign in' button
so that I can login to Google account

Steps:

  • Define the browser on which the tests needs to be executed in [TestFixtureSetUp] attribute.
  • In the [Test] attribute, clear the log for deleting the old test results and then start recording.
  • In the [Test] attribute, pass the URLs of ‘Google home page’ and ‘ Google Sign In page’ using the 2 variables myURL1 and myURL2 respectively.
  • Specify the location for saving the HttpWatch log file.
  • Refer the APIs available for HttpWatch by visiting the URL link: http://apihelp.httpwatch.com/#Automation Overview.html

TEST EXECUTION INSTRUCTIONS:

  • Copy and paste the code snippet below into your Visual Studio 2010.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit;
using NUnit.Framework;
using HttpWatch;
using SHDocVw;

namespace HttpWatchPerformance
{

 public class PerformanceTests
 {
  private HttpWatch.Controller controller;
  private HttpWatch.Plugin plugin;

 [TestFixtureSetUp]
  public void Setup()
  {
  controller = new HttpWatch.Controller();
  //Creating the HttpWatch plug-in for IE
  plugin = controller.IE.New();
  //Creating the HttpWatch plug-in for Firefox
  //plugin = controller.Firefox.New("");
  }

  public void init()
  {
  }

 [Test]
  public void Test1()
  {
  //Turn Off filtering
  plugin.Log.EnableFilter(false);

  //Clear the HttpWatch Log
  plugin.Clear();

  //Start Recording
  plugin.Record();

  //Loading a Page using HttpWatch
  string myURL1 = "http://www.google.com.au/";
  plugin.GotoURL(myURL1);
  //Validate that Step 1 is executed
  Console.WriteLine("Step 1 executed");
  //Don't return until the page loads
  controller.Wait(plugin,-1);
  string myURL2 = "https://accounts.google.com/ServiceLogin?hl=en&continue=http://www.google.com.au/";
  plugin.GotoURL(myURL2);
  //Validate that Step 2 is executed
  Console.WriteLine("Step 2 executed");
  //Don't return until the page loads
  controller.Wait(plugin, -1);
  //Save an HttpWatch Log (.hwl) file
  plugin.Log.Save(@"C:\Users\Administrator\Downloads\HttpWatchPerformance\Logs\Test1.hwl");
  }
 }
}
Running the tests using NUnit:
  • In Visual Studio, Go to Projects -> Project Properties (‘HttpWatchPerformance Properties’ in my example).
  • Click on the ‘Debug’ tab.
  • Set the ‘Start external program’ to the location of NUnit exe file (C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\nunit-x86.exe) as shown in the snapshot below.
  • Build the solution, Go to ‘Build -> Build Solution’ (hit the F6 key) in Visual Studio.
  • Execute the test, Go to ‘Debug -> Start Debugging’ (hit the F5 key) in Visual Studio. Visual Studio invokes the NUnit application.
  • In NUnit, click on ‘File -> Open Project’ and choose the location of the HttpWatchPerformance.dll file.
  • In NUnit, click on the ‘Run’ button to run the tests. Test will be executed and the results will be saved in the log file, Test1.hwl in the specified location. Refer the log file for the test results as shown in the snapshot below:
 
%d bloggers like this: