Click here to Skip to main content
6,295,667 members and growing! (9,549 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Utilities     Intermediate License: The GNU General Public License (GPL)

WatiN Test Recorder

By Daaron

Automate web test recording into C#, VB.NET and PHP
C#, VB, Windows, .NET, Visual Studio, Dev
Posted:14 Jun 2007
Views:26,296
Bookmarked:24 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
13 votes for this article.
Popularity: 4.73 Rating: 4.24 out of 5
4 votes, 30.8%
1

2

3

4
9 votes, 69.2%
5

Screenshot - SS-MainScreen.png

Introduction

First there was WatiN, Web Application Testing in .NET, enabling a developer to automate Internet Explorer. Jesus Jimenez has written an article about it, also. The code for it looks something like this:

using (IE ie = new IE("http://www.google.com"))
{
    ie.TextField(Find.ByName("q")).TypeText("WatiN");
    ie.Button(Find.ByName("btnG")).Click();
}

This code opens the Google home page, enters "WatiN" into the search text box and then clicks the search button. The test recorder was born from the need to automatically record clicks and typing within a web page, enabling the faster production of tests for WatiN. It can produce code in C#, VB.NET and PHP. Languages can be changed in the settings dialog.

The recorder is an application with an embedded web browser, thanks to mehrcpp and his great embedded browser article. The browser hooks several happenings -- such as navigation, right-clicking and popup windows -- and Windows hooks are used to pick up keystrokes. The fundamental thing I wanted to accomplish with the recorder was the proper handling of all HTML input elements, like checkboxes and select lists. The application reaches beyond these modest goals.

NOTE: The application uses a modified version of the WatiN library to support the embedded browser, but your code does not need to.

Interface

The browser interface was designed by mehrcpp and has not strayed far. The top of the window looks like a typical browser, but the bottom of the window is where our magic happens. The list of tests are on the left; the code is on the right. To begin a test, enter the name of the test, which will be the name of the method. Click Record and begin using the web page normally. Notice that navigation, clicks, and keystrokes generate code. When you are done with your test, click Stop. From here, you have the option of simply copying and pasting to your favorite IDE, running the code or just compiling it. Of course, if you chose PHP as your code language, the compilation button will be disabled.

If you click on Run or Compile, you'll see a list of code templates available for the language. These templates are simple XML files in the templates directory and can be modified to fit your needs. For instance, if you need your tests to send data to a database, you can reference assemblies and add the code you need.

Functions

Some methods implemented in WatiN don't correspond to simple mouse clicks or keypresses, such as the Blur method. To that end, when you right-click an element, the Function Explorer window opens. This is a specially formatted HTML page that returns code in a post method to the window and to the code box.

Screenshot - SS-FunctionMain.png

By extension, a developer can make a function page for their own snippets, inserting significant amounts of code when desired. Functions can be language- and element-dependent.

The code

Besides the main interfaces, the next important base class is the WatinScript object. This implements an interface pattern through virtual methods and handles methods for keyboard interpretation, select list handling, and compilation. Methods to add specific lines of code are handled in descendant WatiNVBNET, WatiNCSharp and WatiNPHP objects. To implement another language, you would need to inherit from WatinScript, create a code template and then create (or modify) functions to generate appropriate code.

PHP support

PHP is able to run WatiN through the use of a COM interface (WatiNCOM.dll) in the installation package. This interface is also freeware open source and available on the recorder website. The interface attempts to mirror much of WatiN, but because static methods and overloading are currently difficult in PHP, some changes had to be made. Your code will therefore differ slightly from what you may be used to. For instance, the code from above looks like this in PHP:

$Interface=new COM('WatiN.COMInterface') or die('Cannot create IE object');
    $ie = $Interface->CreateIE("http://www.google.com/");
    $ie->TextField($Interface->FindByName("q"))->TypeText("watin");
    $ie->Button($Interface->FindByName("btnG"))->Click();

Notice that CreateIE is a method of the base COM interface, not a separate constructor. Additionally, the static Find methods in the .NET assembly are methods of the base COM interface also. Jeroen is considering adding the interface to his core, but that may be further in the future.

Known issue

An HTML Dialog -- i.e. a web page opened with JavaScript using showModalDialog or showModelessDialog -- cannot be tracked by this application. It does not fire the NewWindow event, so a handle to the new browser cannot be captured for use by the recorder. If you are someone or know someone at Microsoft who can change this, we'd love to support it. Read the Microsoft KnowledgeBase article.

Project information can be found here.

History

Full release anticipated for early July, 2007

Beta 3 - 6/1/07

  • Add: Support for VB.NET code generation
  • Add: Support for PHP code generation
  • Add: User-definable templates for code pages (see the "Templates" directory)
  • Add: Replaced Property Explorer (right-click) with user-definable code generation (see the "Functions" directory)

Beta 2 - 4/24/07

  • Fix: DOM/WatiN selection caused exception or not highlighting element
  • Fix: Non-alpha keys were not printing properly
  • Fix: Popup did not support window.opener and other JavaScript
  • Add: Exceptions show a window to email report
  • Add: WatiN Element support for frames
  • Add: Natural support for traditional input tags (select, checkbox, radio, and file)
  • Add: Setting for search types and ordering

Beta 1 - 4/9/07

  • First release

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPL)

About the Author

Daaron


Member
Raised by wolves in the mean streets of Macintosh C/C++ for IBM and Motorola, moved on to Delphi and now C#. Ah, the mother's milk of Microsoft...
Occupation: Web Developer
Location: United States United States

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
GeneralHelp required in how to handle the click of a table column header link button which calls asynchronusly(uses AJAX) in WATIN Pinmemberkeshavjois8:14 15 May '09  
QuestionHow do we record a dropdown box and select one of the options available in the dropdown box using WatiN TestRecorder.Can we modify the code generated by Test Recorder. Pinmembervenki_nvts1:36 23 Dec '08  
Generalhelp me!!!!!!!!!1 Pinmembersiddhartha mukherjee19:39 5 Nov '08  
GeneralTrapping modal dialogs. Pinmemberpfer9:34 16 Jul '08  
GeneralRe: Trapping modal dialogs. PinmemberDaaron11:47 16 Jul '08  
GeneralUnable to Run test after successful compile Pinmemberchie780:01 5 Jul '08  
QuestionGreat Tool! But... I have an issue with iframe elements. Pinmembercsqepaul8:15 9 Apr '08  
GeneralRe: Great Tool! But... I have an issue with iframe elements. PinmemberDaaron8:45 9 Apr '08  
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pinmembercsqepaul8:50 9 Apr '08  
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pinmembercsqepaul12:16 10 Apr '08  
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pinmembercsqepaul8:16 15 Apr '08  
GeneralRe: Great Tool! But... I have an issue with iframe elements. PinmemberDaaron9:17 15 Apr '08  
GeneralExtract table? Pinmemberotrack5:11 8 Nov '07  
GeneralRe: Extract table? PinmemberDaaron5:16 8 Nov '07  
GeneralFiles missing and Error in the modified WatiN PinmemberBenjiiim1:22 30 Jul '07  
GeneralExcellent Pinmembermehrcpp11:42 26 Jun '07  
QuestionFrame support PinmemberDORNINEM21:15 18 Jun '07  
AnswerRe: Frame support [modified] PinmemberDaaron4:44 19 Jun '07  
QuestionSupport for Python PinmemberSrein7:17 15 Jun '07  
AnswerRe: Support for Python PinmemberDaaron13:56 16 Jun '07  
GeneralMIssing files and project Pinmembernji789:53 14 Jun '07  
GeneralRe: MIssing files and project PinmemberDaaron14:20 16 Jun '07  
GeneralSource is missing some files Pinmembergrundt9:20 14 Jun '07  
GeneralRe: Source is missing some files PinmemberDaaron14:20 16 Jun '07  
GeneralRe: Source is missing some files PinmemberRobertC19762:54 20 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Jun 2007
Editor: Genevieve Sovereign
Copyright 2007 by Daaron
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project