Boo is statically typed language for the .NET platform inspired by Python– with Boo, you can write functioning .NET applications quickly. If you have a handle on Python or even VB, then picking up Boo is anything but scary. Boo has a host of features including built in Regular Expressions, collections, and closures; in fact, even though it is Pythonic in nature, it reminds me a lot of Groovy in that the language is unique to itself and not a cousin of some other language like Jython is Python or JRuby is to Ruby.
Recently, I found myself needing to script out some functional tests via Internet Explorer. I considered using Watir; however, I wanted to see what I could knock out with Boo due to its close integration with the Windows platform. As it turns out, Boo’s closeness to Windows makes IE automation so easy it’s almost…well, frightening. The only, err….terrifying aspect of using IE automation in Boo is learning mshtml, which isn’t as documented as one would like. Nevertheless, once you master a few of the common properties of HTML elements, you’re golden.
Here is an example of how easy it is to script out a simple test that verifies there are four comments for a particular blog entry interactively using Boo’s booish. One of the coolest features many scripting languages have is a shell in which you can interactively code behavior– this is superb for prototyping, experimenting, and generally learning a language’s features.
The test does a few thing along the way. First, an instance of IE is created, followed by a navigation to a website (www.thediscoblog.com). All hrefs are obtained– if the desired one is found, it is clicked.
>>> ie as duck =
System.Type.GetTypeFromProgID("InternetExplorer.Application")()
System.__ComObject
>>> ie.Visible = true
true
>>> ie.Navigate2("http://www.thediscoblog.com")
>>> links = ie.Document.getElementsByTagName("a")
System.__ComObject
>>> site = "http://thediscoblog.com/2007/02/04/currying-maximum-..."
'http://thediscoblog.com/2007/02/04/currying-maximum-favor-with-groovy/'
>>> for link in links:
... if(link.href.Equals(site)):
... link.click()
...
mshtml.HTMLAnchorElementClass
Note how in the above code I must declare my ie variable as type duck. This is because of Boo’s statically typed nature– if I don’t force the duck-ness on my ie type then I wouldn’t be able to call the Visible property, as the lowest common denominator type, which would have been forced on the ie variable had I not said as duck, wouldn’t allow the Visible property call and would have yielded an exception.
Next, I obtain a list of all links in the resulting web page and proceed to iterate over the collection seeking my desired link. If it is found, I call the click method on the element.
With the next page loaded, I then grab a listing of all h3 HTML elements found on the page. If the id of the element (i.e. id=”comments”) is set to comments, I then assert that the text surrounded by the h3 tags contains the phrase “4 Responses” using a regular expression– which effectively tests the presence of 4 comments for this blog entry– if there are new comments, the assert will fail.
>>> h3s = ie.Document.getElementsByTagName("h3")
System.__ComObject
>>> for h3 in h3s:
... if(h3.id.Equals("comments")):
... assert h3.innerText =~ "4 Responses"
...
mshtml.HTMLHeaderElementClass
>>> ie.Quit()
Note how the assert keyword is built into the language as well– this is an example of Boo’s macros. Lastly, after asserting the presence of 4 comments, I proceed to kill the IE instance with a call to Quit.
As you can see, building out a simple acceptance test with Boo is a breeze– or should I say terrifyingly simple? Plus, using something like booish makes writing out simple tests a breeze. Watir is definitely a great choice for IE automation; however, with Boo, these tests (once written to a file) can be compiled into a native .NET dll, which can then be run via NAnt or even the command line with ease. How’s that for scary?

February 9th, 2007 at 4:14 pm
[…] Booing IE automation- Yours truly demonstrates how to script out a functional IE test via Boo, man. […]
February 11th, 2007 at 9:43 am
Another thing to consider is Selenium. Although you’ll need firefox to build your test, you can run the tests in both IE and FF. Thus automating your crossplatform tests.
Take a look at the IBM mini-tutorial
http://www-128.ibm.com/developerworks/library/wa-selenium-ajax/
February 19th, 2007 at 6:45 pm
[…] XML by itself is fairly limiting when it comes to expressing behavior; however, with the addition of Boo or even the extensibility provided by creating custom tasks, you can add a higher level of functionality. Plus, with Boo, the level of effort is frighteningly low! […]