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

HttpWatch Version 6.0

calendarSeptember 15, 2008 in Firefox , HttpWatch , Internet Explorer

HttpWatch 6.0

HttpWatch version 6.0 has been released and is now available for download.

Any customers eligible for a free upgrade to HttpWatch Professional can install the latest version using their existing license key. If you’re not sure whether your license will work with version 6.0 go to Help->Check For Updates in HttpWatch and it will show you any available updates or upgrades.

What’s New?

The major new feature in this release is support for Firefox 2.0 and 3.0 on Windows. If you install version 6.0 you’ll get an HttpWatch status bar icon in Firefox:

Clicking on the icon will open the same HttpWatch user interface that’s previously only been available in Internet Explorer:

HttpWatch Plug-in For Firefox

A few other areas have also been improved to accommodate Firefox. First of all there’s a new Properties window that displays information about the browser that recorded a log file:

The comment field allows simple notes and annotations to be saved with each log file.

The automation interface has been updated to include the data from the Properties window and to allow automation of HttpWatch within Firefox:

We’ve also done some work on the way that Start times are displayed. You can now choose between time offsets, local time and GMT/UTC. The latter can be particularly useful when trying to compare log files from other monitoring tools and other locations world-wide:

And finally, HttpWatch Studio now has tabs making it easier to switch between log files:

Compatibility with Version 5.x

We’ve kept the file format unchanged from version 5.x, so you can readily exchange log files with version 6.0. Although you cannot view some of the information such as the comment and browser version in version 5.x, this data is maintained if you re-save the log file and then re-open it in version 6.0.

The automation interface maintains backwards source compatibility with interpreted script clients and binary compatibility with existing compiled clients (e.g. C#, C++, VB.Net). However, if you attempt to recompile an automation program with version 6.0, you may need to make some source code changes. For example, the New method has moved from the Plugin object onto the Firefox and IE objects.

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

Ready to get started? TRY FOR FREE Buy Now