Click here to Skip to main content
Click here to Skip to main content

WatiN Test Recorder

, 14 Jun 2007
Rate this:
Please Sign up or sign in to vote.
Automate web test recording into C#, VB.NET and PHP

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 (GPLv3)

About the Author

Daaron
Web Developer
United States United States
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...

Comments and Discussions

 
QuestionFrame PinmemberMember 422865726-May-14 2:17 
QuestionBoth latest Alpha version and This version Not working on win7 Pinmemberdeshmukhsc14-Aug-12 6:47 
QuestionHow to download source files of Version 3 Alpha PinmemberPapyRef21-May-12 19:26 
AnswerRe: How to download source files of Version 3 Alpha PinmemberDaaron22-May-12 4:57 
AnswerRe: How to download source files of Version 3 Alpha PinmemberPapyRef22-May-12 6:01 
GeneralHelp required in how to handle the click of a table column header link button which calls asynchronusly(uses AJAX) in WATIN Pinmemberkeshavjois15-May-09 7:14 
GeneralRe: Help required in how to handle the click of a table column header link button which calls asynchronusly(uses AJAX) in WATIN Pinmemberjeethendra123422-Sep-09 20:58 
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_nvts23-Dec-08 0:36 
Generalhelp me!!!!!!!!!1 Pinmembersiddhartha mukherjee5-Nov-08 18:39 
GeneralTrapping modal dialogs. Pinmemberpfer16-Jul-08 8:34 
GeneralRe: Trapping modal dialogs. PinmemberDaaron16-Jul-08 10:47 
GeneralUnable to Run test after successful compile Pinmemberchie784-Jul-08 23:01 
QuestionGreat Tool! But... I have an issue with iframe elements. Pinmembercsqepaul9-Apr-08 7:15 
GeneralRe: Great Tool! But... I have an issue with iframe elements. PinmemberDaaron9-Apr-08 7:45 
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pinmembercsqepaul9-Apr-08 7:50 
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pinmembercsqepaul10-Apr-08 11:16 
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pinmembercsqepaul15-Apr-08 7:16 
GeneralRe: Great Tool! But... I have an issue with iframe elements. PinmemberDaaron15-Apr-08 8:17 
QuestionExtract table? Pinmemberotrack8-Nov-07 4:11 
AnswerRe: Extract table? PinmemberDaaron8-Nov-07 4:16 
GeneralFiles missing and Error in the modified WatiN PinmemberBenjiiim30-Jul-07 0:22 
GeneralExcellent Pinmembermehrcpp26-Jun-07 10:42 
QuestionFrame support PinmemberDORNINEM18-Jun-07 20:15 
AnswerRe: Frame support [modified] PinmemberDaaron19-Jun-07 3:44 
GeneralFrame support PinmemberRuchi Choksi19-Jan-12 3:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 14 Jun 2007
Article Copyright 2007 by Daaron
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid