Using HttpWatch with WatiN

calendarOctober 30, 2008 in Automation , C# , HttpWatch , Internet Explorer

WatiN (pronounced as What-in) is a browser automation library for .NET that was inspired by the Ruby based Watir and FireWatir frameworks. It allows C# and VB.Net applications to programatically interact with a browser to perform tasks such as going to a web page, filling out fields and clicking on buttons. The current version only works with IE, but version 2.0 will include support for Firefox.

We have previously discussed the use of Ruby, Watir and HttpWatch and version 6.0 now contains Watir sample code that works with both IE and Firefox. HttpWatch can also be used alongside WatiN to record HTTP traffic and performance statistics while running an automated script. We adapted the Getting Started WatiN sample to use HttpWatch to record the Google results page. The modified code is shown below:

// This code works with WatiN version 1.3
using System;
using WatiN.Core;
 
namespace WatiNTest
{
  class WatiNTestWithHttpWatch
  {
    [STAThread]
    static void Main(string[] args)
    {
        // Open a new Internet Explorer window and
        // goto the google website.
        IE ie = new IE("http://www.google.com");
 
        // Attach HttpWatch to this new instance of IE
        HttpWatch.Controller ct = new HttpWatch.Controller();
        HttpWatch.Plugin plugin = ct.IE.Attach((SHDocVw.IWebBrowser2)ie.InternetExplorer);
 
        // Start recording a log file in HttpWatch
        plugin.Record();
 
        // Find the search text field and type Watin in it.
        ie.TextField(Find.ByName("q")).TypeText("WatiN");
 
        // Click the Google search button.
        ie.Button(Find.ByValue("Google Search")).Click();
        ie.WaitForComplete();
 
        // Stop recording and save an HttpWatch log file
        plugin.Stop();
        plugin.Log.Save(@"c:\mydir\googlesearch.hwl");
 
        HttpWatch.Summary logSummary = plugin.Log.Entries.Summary;
 
        Console.WriteLine("\r\nElapsed time (secs) = " + logSummary.Time.ToString() +
                          " Downloaded bytes = " + logSummary.BytesReceived.ToString());
 
        // Uncomment the following line if you want to close
        // Internet Explorer and the console window immediately.
        //ie.Close();
    }
  }
}

The only non-trivial step required to add HttpWatch support, was to supply an IWebBrowser2 interface to the Attach method. This was achieved using the cast shown below:

HttpWatch.Plugin plugin = ct.IE.Attach((SHDocVw.IWebBrowser2)ie.InternetExplorer);

One problem you may run into is that WatiN does not work correctly with IE 7 Protected Mode on Vista. However, you can work around this by creating the instance of IE with HttpWatch and then attaching WatiN as shown below:

// This code works with WatiN version 1.3
using System;
using WatiN.Core;
 
namespace WatiNTest
{
  class WatiNTestWithHttpWatch
  {
    [STAThread]
    static void Main(string[] args)
    {
        // Create a new instance of IE with HttpWatch to avoid Protected Mode
        // issues
        HttpWatch.Controller ct = new HttpWatch.Controller();
        HttpWatch.Plugin plugin = ct.IE.New();
 
        // Attach WatiN to this instance of IE
        IE ie = IE.AttachToIE(Find.By("hwnd", plugin.Container.HWND.ToString()));
        ie.GoTo("http://www.google.com");
        plugin.Record();
 
        // Find the search text field and type Watin in it.
        ie.TextField(Find.ByName("q")).TypeText("WatiN");
 
        // Click the Google search button.
        ie.Button(Find.ByValue("Google Search")).Click();
        ie.WaitForComplete();
 
        // Stop recording and save an HttpWatch log file
        plugin.Stop();
 
        // If you are saving from protected mode IE 7 on Vista
        // you will need to use a location that is accessible from protected mode
        //plugin.Log.Save(@"C:\Users\\AppData\Local\Temp\low\googlesearch.hwl");
 
        HttpWatch.Summary logSummary = plugin.Log.Entries.Summary;
 
        Console.WriteLine("\r\nElapsed time (secs) = " + logSummary.Time.ToString() +
                          " Downloaded bytes = " + logSummary.BytesReceived.ToString());
 
        // Uncomment the following line if you want to close
        // Internet Explorer and the console window immediately.
        //ie.Close();
    }
  }
}

If you would like to try this out for yourself you would need to:

  1. Download and install HttpWatch. These samples will work with the free Basic Edition
  2. Download and install WatiN 1.3 (not 2.0 Beta)
  3. Build a .Net project using the C# code shown above
  4. Set a reference to the WatiN assembly as shown here
  5. Set a reference to the HttpWatch Automation library as described in Automating HttpWatch with Visual Basic
  6. Compile and the run the sample

UPDATE: See ‘Using HttpWatch with WatiN 2.1‘ for information about using the updated version of WatiN

Automating HttpWatch with Visual Basic

calendarJuly 23, 2008 in Automation , HttpWatch , Internet Explorer

HttpWatch includes automation samples in C# and in a previous post we used C# to create simple empty cache and primed cache tests. However, a number of customers have recently asked how they can get started with HttpWatch and VB.Net. To use Visual Basic simply follow these steps:

1. Create a VB.Net Project

In Visual Studio create a new console application for Visual Basic:

Create VB.Net Console Application

2. Add a Reference for HttpWatch

To use the COM based HttpWatch automation API you need to create a  reference by right clicking on the project and going to ‘Add Reference…’ :

Add Reference

When the ‘Add Reference’ dialog opens click on the COM tab. If you have HttpWatch Basic or Professional Edition installed you should be able to find HttpWatch in the list of COM type libraries. A quick way to do this is to type ‘httpwatch’ into the References dialog. Once you have found the HttpWatch Automation library; select it and click on OK:

HttpWatch COM Reference

3. Start Writing Code!

The HttpWatch automation objects are now available in VB.Net. To make life easier you may want to add an Imports HttpWatch statement into your code so that you don’t have to add the HttpWatch namespace onto the name of every automation class and interface. There’s documentation for the API in the HttpWatch help file and you’ll see Intellisense prompts as you start writing code:

Writing VB.Net for HttpWatch

Here’s an example of a simple program to test how long a page takes to load:

Imports HttpWatch 
Imports System       
 
Module Module1       
 
    Sub Main() 
        Dim url As String = "http://www.httpwatch.com"       
 
        Dim controller As Controller = New Controller       
 
        ' Create a new instance of IE 
        Dim plugin As Plugin = controller.[New]       
 
        plugin.Record() 
        plugin.GotoURL(url)       
 
        ' Wait for page to load 
        controller.Wait(plugin, -1)       
 
        plugin.Stop()       
 
        Dim pageLoadTime As Double = _ 
            plugin.Log.Pages(0).Entries.Summary.Time       
 
        Console.WriteLine("The page loaded in " + _ 
            pageLoadTime.ToString() + " secs")       
 
        plugin.Container.Quit() ' Close IE 
    End Sub       
 
End Module

And here is the empty cache test converted to VB.Net:

 
Imports HttpWatch 
Imports System     
 
Module Module1     
 
    Sub Main() 
        Dim url As String = "http://www.httpwatch.com" 
        Dim controller As Controller = New Controller     
 
        ' Create a new instance of IE 
        Dim plugin As Plugin = controller.[New]     
 
        'Clear out all existing cache entries 
        plugin.ClearCache()     
 
        plugin.Record() 
        plugin.GotoURL(url)     
 
        ' Wait for page to load 
        controller.Wait(plugin, -1)     
 
        plugin.Stop()     
 
        Dim pageLoadTime As Double = _ 
            plugin.Log.Pages(0).Entries.Summary.Time     
 
        Console.WriteLine("The page loaded in " + _ 
            pageLoadTime.ToString() + " secs")     
 
        ' Uncomment the next line to save the results 
        ' plugin.Log.Save("c:\temp\emptycachetest.hwl")     
 
        plugin.Container.Quit() ' Close IE 
    End Sub     
 
End Module

Using HttpWatch to Measure Page Load Times for New and Existing Users

calendarMay 14, 2008 in Automation , C# , Caching , HttpWatch , Internet Explorer , Optimization

If you’re tuning a web page’s performance there are two types of visitors that you need to be concerned about:

  1. A new visitor to your site who won’t have any of your pages, scripts, CSS or images in their browser cache.
  2. An existing user of your site who will have your cacheable pages, scripts, CSS or images in their browser cache.

Visitor type 1) is said to be in the Empty Cache state and Vistor type 2) is said to be in the Primed Cache state. Optimizing for visitors with an empty cache is important as their initial impressions of your site will be affected by how quickly its pages are loaded.

The performance of any page is never going to worse for visitors with a primed cache, but minimizing the load on your site caused by existing users through the use of effective caching will reduce your bandwidth costs and server load.

Your can manually simulate these two scenarios with HttpWatch. For example, you could run an Empty Cache test on our home page (www.httpwatch.com) using these steps:

  • Open a HttpWatch in IE and click on Tools->Clear Cache or use the keyboard shortcut Alt+C. If you haven’t cleared your browser cache recently this could take a few minutes:
    Clear Cache
  • Click on Record and go to www.httpwatch.com
  • HttpWatch will then display a time chart with the page load time:
    Empty Cache Test

To run the Primed Cache test you would first need to ensure that the page has already been visited and then re-visit it in a new instance of IE. You shouldn’t re-use the same instance of IE because there may some images held in memory from the first visit to the page. To perform the Primed Cache test you would need to:

  • Visit www.httpwatch.com in IE to prime the cache
  • Close down IE and start a new instance
  • Open HttpWatch and click on Record
  • Go to www.httpwatch.com
  • HttpWatch will then display a time chart with the page load time:
    Primed Cache Test

Of course, with a test like this you really want to run it automatically. The HttpWatch Automation interface (document in the HttpWatch Help file) allows you to do this in a few lines of code. Here’s the code in C# for the Empty Cache test:

// Set a reference to the HttpWatch COM library
// to start using the HttpWatch namespace
//
// This code requires HttpWatch version 6.x
//
 
using HttpWatch;                
 
namespace EmptyCacheTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://www.httpwatch.com/download/";
            Controller controller = new Controller();
 
            // Create an instance of IE (For Firefox use
            // controller.Firefox.New("") )
            Plugin plugin = controller.IE.New();                
 
            // Clear out all existing cache entries
            plugin.ClearCache();                
 
            plugin.Record();
            plugin.GotoURL(url);                
 
            // Wait for the page to download
            controller.Wait(plugin, -1);                
 
            plugin.Stop();                
 
            // Find the load time for the first page recorded
            double pageLoadTimeSecs =
                plugin.Log.Pages[0].Entries.Summary.Time;                
 
            System.Console.WriteLine( "The empty cache load time for '" +
                url + "' was " + pageLoadTimeSecs.ToString() + " secs");                
 
            // Uncomment the next line to save the results
            // plugin.Log.Save(@"c:\temp\emptytestcache.hwl");                
 
            plugin.CloseBrowser();
        }
    }
}

and here’s the Primed Cache test. Notice how we use controller.IE.New() a second time to ensure that a new instance of IE is started:

// Set a reference to the HttpWatch COM library
// to start using the HttpWatch namespace
//
// This code requires HttpWatch version 6.x
//
using HttpWatch;               
 
namespace PrimedCacheTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://www.httpwatch.com/download/";
            Controller controller = new Controller();               
 
            // Create an instance of IE (For Firefox use
            // controller.Firefox.New("") )
            Plugin plugin = controller.IE.New();               
 
            // Visit the page to prime the cache
            plugin.GotoURL(url);
            plugin.Record();
            controller.Wait(plugin, -1);               
 
            // Shutdown IE and create a new instance
            plugin.CloseBrowser();
            plugin = controller.IE.New();               
 
            plugin.Record();               
 
            // Visit the page a second time with the cache primed
            plugin.GotoURL(url);
            controller.Wait(plugin, -1);               
 
            plugin.Stop();               
 
            // Find the load time for the first page recorded
            double pageLoadTimeSecs =
                plugin.Log.Pages[0].Entries.Summary.Time;               
 
            System.Console.WriteLine( "The primed cache load time for '" +
                url + "' was " + pageLoadTimeSecs.ToString() + " secs");               
 
            // Uncomment the next line to save the results
            // plugin.Log.Save(@"c:\temp\emptytestcache.hwl");               
 
            plugin.CloseBrowser();
        }
    }
}

BTW, everything we’ve mentioned in this blog post works with the free Basic Edition of HttpWatch as well as the Professional Edition.

Ready to get started? TRY FOR FREE Buy Now