Angel \”Java\” Lopez on Blog

March 15, 2010

Scripting WatiN using AjSharp

Filed under: .NET, AjSharp, Open Source Projects — ajlopez @ 10:47 am

AjSharp is an interpreter I wrote using C#. It’s a dynamic language, with dynamic classes and objects, functional values, channels and goroutines, agents-alike entities, access to native .NET object. It was create as a derivative of my previous work on AjBasic, the dynamic language used in AjGenesis, code generation engine. My plan is to unify both language using AjLanguage core, and add them to AjGenesis, allowing other template language to be used as plugins. But all that is still only a plan.

The code of this post is in the trunk of my AjCodeKatas Google Project:

http://code.google.com/p/ajcodekatas/source/browse/

under trunk/AjLanguage/Src/AjSharp.Examples

One of the objetives in the design of AjSharp, is to serve as a light shell to invoke .NET libraries. As a proof of concept, I wrote a simple class to access WatiN, the web automation test library. This is the simple class I’m using:

class Wat
{
  var Browser;
  
  function Wat()
  {
    WatiN.Core.Settings.WaitForCompleteTimeOut = 480;
    this.Browser = new WatiN.Core.IE();
  }
  
  sub Browse(url)
  {
    this.Browser.GoToNoWait(url);
    this.Browser.WaitForComplete(480);
  }
  
  function Button(name)
  {
    button = this.Browser.Button(name);
    
    if (button.Exists)
      return button;
      
    button = this.Browser.Button(WatiN.Core.Find.ByName(name));
    
    if (button.Exists)
      return button;
      
    button = this.Browser.Button(WatiN.Core.Find.ByValue(name));
    if (button.Exists)
      return button;
      
    return null;
  }
  
  function TextField(name)
  {
    textfield = this.Browser.TextField(name);
    
    if (textfield.Exists)
      return textfield;
      
    textfield = this.Browser.TextField(WatiN.Core.Find.ByName(name));
    
    if (textfield.Exists)
      return textfield;
      
    return null;
  }
  
  function Link(name)
  {
    link = this.Browser.Link(name);
    
    if (link.Exists)
      return link;
      
    link = this.Browser.Link(WatiN.Core.Find.ByUrl(name));
    
    return link;
    
    if (link.Exists)
      return link;
      
    return null;
  }
  
  function Contains(text)
  {
    return this.Browser.ContainsText(text);
  }
  
  sub Close()
  {
    this.Browser.Close();
  }
}

You can extend it to support other features of WantiN browser. An example of use:

Include("Tests.ajs");
System.Reflection.Assembly.Load("WatiN.Core");
Include("Wat.ajs");
wat = new Wat();
wat.Browse("http://google.com");
wat.TextField("q").TypeText("Angel Java Lopez");
wat.Button("Google Search").Click();
Assert(wat.Contains("ajlopez.com"), "No content");
wat.Browse("http://www.ajlopez.net/");
Assert(wat.Contains("Java"), "No content");
Assert(wat.Link("http://ajlopez.zoomblog.com/").Exists, "No link 4");
wat.Link("http://ajlopez.zoomblog.com/").Click();
Assert(wat.Contains("Ciencia"), "No content 2");
wat.Close();
exit;

The only problem I had to invoke WatiN, it was its requirement of run inside an STAThread. So, I changed the main method of AjSharp.Console, adding STAThread attribute to it. I’m using this kind of script to test a real web application that is being developed in an agile team. I could add support for exceptions, and logging of messages.

You have more robust utilities to use over WatiN, like Wax, that enables to write tests using Excel spreedsheets. But now, the above scripting core is good enough to the simple initial tests we are using, and it was a good exercise to test AjSharp abilities in a real requirement.

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: