Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / Visual Basic

WatiN Test Recorder

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
14 Jun 2007GPL34 min read 147.2K   5.2K   43   34
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:

C#
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:

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)


Written By
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 Pin
Gokul shingote26-May-14 2:17
Gokul shingote26-May-14 2:17 
QuestionBoth latest Alpha version and This version Not working on win7 Pin
deshmukhsc14-Aug-12 6:47
deshmukhsc14-Aug-12 6:47 
QuestionHow to download source files of Version 3 Alpha Pin
PapyRef21-May-12 19:26
professionalPapyRef21-May-12 19:26 
AnswerRe: How to download source files of Version 3 Alpha Pin
Daaron22-May-12 4:57
Daaron22-May-12 4:57 
AnswerRe: How to download source files of Version 3 Alpha Pin
PapyRef22-May-12 6:01
professionalPapyRef22-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 Pin
keshavjois15-May-09 7:14
keshavjois15-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 Pin
jeethendra123422-Sep-09 20:58
jeethendra123422-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. Pin
venki_nvts23-Dec-08 0:36
venki_nvts23-Dec-08 0:36 
Generalhelp me!!!!!!!!!1 Pin
siddhartha mukherjee5-Nov-08 18:39
siddhartha mukherjee5-Nov-08 18:39 
GeneralTrapping modal dialogs. Pin
pfer16-Jul-08 8:34
pfer16-Jul-08 8:34 
GeneralRe: Trapping modal dialogs. Pin
Daaron16-Jul-08 10:47
Daaron16-Jul-08 10:47 
GeneralUnable to Run test after successful compile Pin
chie784-Jul-08 23:01
chie784-Jul-08 23:01 
QuestionGreat Tool! But... I have an issue with iframe elements. Pin
csqepaul9-Apr-08 7:15
csqepaul9-Apr-08 7:15 
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pin
Daaron9-Apr-08 7:45
Daaron9-Apr-08 7:45 
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pin
csqepaul9-Apr-08 7:50
csqepaul9-Apr-08 7:50 
No I don't have an exposed page. Sorry.
Would the full page source help?
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pin
csqepaul10-Apr-08 11:16
csqepaul10-Apr-08 11:16 
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pin
csqepaul15-Apr-08 7:16
csqepaul15-Apr-08 7:16 
GeneralRe: Great Tool! But... I have an issue with iframe elements. Pin
Daaron15-Apr-08 8:17
Daaron15-Apr-08 8:17 
QuestionExtract table? Pin
otrack8-Nov-07 4:11
otrack8-Nov-07 4:11 
AnswerRe: Extract table? Pin
Daaron8-Nov-07 4:16
Daaron8-Nov-07 4:16 
GeneralFiles missing and Error in the modified WatiN Pin
Benjiiim30-Jul-07 0:22
Benjiiim30-Jul-07 0:22 
GeneralExcellent Pin
mehrcpp26-Jun-07 10:42
mehrcpp26-Jun-07 10:42 
QuestionFrame support Pin
DORNINEM18-Jun-07 20:15
DORNINEM18-Jun-07 20:15 
AnswerRe: Frame support [modified] Pin
Daaron19-Jun-07 3:44
Daaron19-Jun-07 3:44 
GeneralFrame support Pin
Ruchi Choksi19-Jan-12 3:35
Ruchi Choksi19-Jan-12 3:35 

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

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