vrijdag 15 januari 2010

Browser.AttachTo<T> and IAttachTo

In WatiN RC1 the functionality to attach to a browser instance has had a complete redo. This had two reasons:

  1. Being able to attach to a browser in a browser agnostic way.
  2. Being able to return your custom browser type.

Download the example code here

From the old way to a browser agnostic way

If you don’t intend to run your tests with different browsers, you still need to make a small change in your code when upgrading to WatiN RC1.

image

needs to be changed into

image

which is the same as

image

and the same as

image

As you can see in the last example, you can pass in a type which should inherit WatiN.Core.Browser. The following example shows you how to utilize this method in situations where you get a browser instance injected into your test code. In the example the link that is clicked on will open up a new browser instance and we want to verify the text in this newly opened browser instance.

image

Thanks to this new AttachTo(type, findBy) method there is no knowledge at all of the used browser type inside my test code.

Returning a custom Browser type from AttachTo()

Many of us who use WatiN will probably use their own custom super duper version of WatiN.Core.IE. Providing new or different behavior then the default IE implementation does. See here for example  (and yes this part of the post was also a little bit sparked by their need to write their own AttachTo methods).

Here is my great MyIE class which overrides the ToString method and returns the Title and Url of the shown page (it’ll surf just as an example).

image

And now I want to be able write

image

When I run this piece of code, WatiN will throw this exception:

WatiN.Core.Exceptions.WatiNException: 
No AttachToHelper registered for type WatiN.Examples.Tests.MyIE.

That does sound like Ican register an AttachToHelper for a browser Type, doesn’t it. The Browser class provides a method RegisterAttachToHelper doing just that.

So we need to create our own AttachToMyIEHelper class. Since MyIE is inheriting IE, AttachToMyIEHelper will inherit AttachToIEHelper. And then it is just a matter of overriding the CreateBrowserInstance method, return a new instance of MyIE and we are done. Here is the implementation:

image

And this code shows you how to register the new AttachToMyIEHelper and use it in your tests.

image

Enjoy testing with WatiN.

Technorati Tags:

Combine WatiN and White to test Silverlight

Googling the web for stuff written about WatiN, I came across this very interesting article to combine forces between WatiN and White.

http://leobartnik.net/blog/2010/01/silverlight-uiautomation-testing-using.html

It also sparked this idea:

var  silverlightControl = browser.Control<SilverlightControl>(Find.ByFirst());

silverlightControl.Button(Find.ById(“someButton”)).Click;

Interesting.

Technorati Tags:

dinsdag 22 december 2009

Released WatiN 2.0 RC 1

A short shout out to announce the release of WatiN 2.0 Release Candidate 1.

Many fixes and new features since the last beta 1 release way back in March. I’m still compiling the list of changes so check back on this page later this week. But if you follow my blog you are well informed already. If you haven’t been using beta 1, start reading the release note of that release here.

And of course  you want to download the new release.

Enjoy testing with WatiN!

Technorati Tags:

vrijdag 11 december 2009

Wrapping complex logic in a Control

Update 18 December 2009: Added missing file to code download

WatiN is all about simplifying the way you can interact with a web browser and the elements on a page. Simple elements are just wrapped (like divs, links and alike) but for more complex elements WatiN offers you functionality which hides complex interactions with the browser. For instance TextField.TypeText will not only set the value of an input field but will also fire all the appropriate events, basically simulating a real user typing in text.

But what if you want to automate/model some action that involves complex logic and/or multiple elements. Yes indeed I am referring to all those ASP and third party controls out there. In this post I will show you how to create your own control which you can use as if it is an element on the page and natively supported by WatiN.

You can download the code for this post here (which also contains a build of WatiN 2.0 RC1)

The control to automate: JQuery-UI Datepicker

I’m going to show you how to create a basic control for the jQuery UI Datepicker. But that’s simple you might think, just use TypeText of the TextField class, the WatiN wrapper for input elements of type text. True, but then I need to think about formating the date correctly every time I automate a datepicker. And there might be other things I would like to check on the datepicker (see  jQuery-UI Datepicker for all the options that can be set and read on this widget). So I’m going to use the javascript API of the datepicker to automate the control.

imageimage

Wondering how to use this control in your test code?

image

Looks familiar (and simple) doesn’t it.

The implementation of DatePicker

To create the control I need a root WatiN.Core.Element subtype which will be the base/root Element for my DatePicker control. Since a html input of type text serves as the root element for the jQuery UI datepicker widget, it makes sense that DatePicker will use TextField as its root Element.

image

Since I decided I’m going to use the datepicker’s javascript API, the control needs to execute scripts inside the browser. Fortunately WatiN offers this functionality through the RunScript() and Eval() methods, both exposed by the DomContainer class. Control<T>, the base class for every control, exposes the property Element which returns the root Element of the control, in this case TextField. From there we can get to the DomContainer to execute javascript using Eval().

image

Next thing to do is to create the javascript to set the date using the API of the jQuery datepicker. This is what the script needs to look like to set the date to 10 December 2009 (indeed the month is 11, a javascript thingy):

window.jQuery(element).datepicker(‘setDate’, new Date(2009, 11, 10));

To make sure that the javascript references the correct html element in the DOM, I will make use of the new method  GetJavascriptElementReference(), available on every (subtype of) Element (in WatiN 2.0 RC1). And of course the Date should be created  correctly.

I’m also using window.jQuery() instead of window.$() to make sure this script will also work when $() is not a shortcut for the jQuery() function (but instead is used by some other javascript library like Prototype).

I will combine all this in the setter part of the Date property of the DatePicker control:

image

For the getter part there is one catch, transforming the returned javascript date in to a DateTime instance. Fortunately DateTime.Parse parses a UTC date format without any problem, so I let the javascript return the date in such a format.

image 

Now that we can set and get the date of the jQuery UI Datepicker widget we can start using it in our tests. And it offers a create place to add more functionality.

To conclude

I hope this post will help you start creating controls for your own or third party controls. There is much more that you can do so check out the download with this post to see:

  • How to restrict the use of this control to input elements which are a datepicker.
  • How to handle Date = null and reading a null date.
  • How to read options set on a datepicker widget instance.

There is also a WatiN-Contrib project started on Google code. So if you like to share your third party WatiN control library, let us know.

Enjoy testing with WatiN!

Technorati Tags:

zondag 6 december 2009

Using WatiN to parse and scrape HTML

Of course you can scrape web pages with WatiN so why this blog post you might ask. Well, with the out-of-the-box WatiN browser support you need to instantiate a browser to get the HTML for a page and scrape it. If you don’t need to interact with the page, like clicking on links, type in some text or rely on cookies for session state, you don’t need all the overhead (memory, start up time) of the browser.

This article will show you how you can use WatiN browserless by implementing a new browser: MsHtmlBrowser, using techniques described in this article.

For this code I use the current development code which will soon be released as WatiN 2.0 RC. You can download the code for this post here.

Load a url with HTMLDocumentClass

The trick to make this work is to make use of IPersistStreamInit and HTMLDocumentClass.

First lets get the definition of IPersistStreamInit in place:

image

And this is how we can combine it with the HTMLDocumentClass (don’t forget to add a reference to the assembly Microsoft.mshtml.dll distributed with WatiN):

image

Using multi browser support in WatiN 2.0

Knowing this we now need to give this code a place in the WatiN architecture. With the introduction of multi browser support in WatiN 2.0, the architecture of the WatiN API has been changed to allow adding new implementations for different browsers without having to change the WatiN.Core code. To create a new Browser implementation we need to create concrete implementations for the INative* interfaces specific for the browser we are adding. Since we are basing our new browser implementation on the same mshtml dll that Internet Explorer uses, we can reuse a lot of the IE specific native classes already available in the WatiN.Core.Native.InternetExplorer namespace.

But since we don’t want to use WatiN.Core.IE and need to use WatiN.Core.Browser to tap into the WatiN architecture, we need to create our own browser class. Lets call it MsHtmlBrowser (inheriting the abstract class Browser). This forces us to implement 2 abstract methods (WaitForComplete and Close) and 1 abstract property (NativeBrowser). Lets focus on implementing the NativeBrowser property.

Implementing MsHtmlNativeBrowser

NativeBrowser returns a type implementing INativeBrowser. So lets create a class MsHtmlNativeBrowser which implements INativeBrowser. This requires several methods and properties to be implemented. Many of these we can’t provide an implementation for since we aren’t wrapping a real browser. Two of these we need to focus on NavigateTo(url) and the property NativeDocument.

As you might guess, we can add our code (to load a page in an HtmlDocumentClass instance) to the NavigateTo method. After initialization of the object we wrap it into an IEDocument which will be returned by the NativeDocument property. Following the implementation. All the other methods do throw a NotImplementedException.

image

Back to implementing MshHtmlBrowser.

MsHtmlBrowser continued

Now that we have MsHtmlNativeBrowser, we can return an instance of this class in the NativeBrowser property of MsHtmlBrowser.

The implementation of the Close method is empty since we don’t have a real browser we need to close. You might consider disposing the instance of MsHtmlNativeBrowser here.

For the WaitForComplete method we can reuse functionality in the IEWaitForComplete class by passing in the IEDocument instance of the MsHtmlNativeBrowser.

This results in the following implementation:

image 

 

Using the new MsHtmlBrowser

image

Which concludes this example on creating a browserless Browser implementation.

Enjoy testing with WatiN!

Technorati Tags:

maandag 26 oktober 2009

WatiN and MbUnit, a nice combo

Just a shameless repost of a mail Jeff Brown did send to the WatiN mailinglists. Interesting stuff, go check it out!

ANN: New samples for using WatiN with MbUnit.

I just released Gallio / MbUnit v3.1 Update 1.

It's mainly a bug fix release but I also added some new samples
demonstrating one good way of using WatiN with MbUnit.

The sample introduces a new custom attribute called [Browser] with some
fancy behavior like:

* Capturing a screenshot if the test fails (or whenever you like).
* Embedding a video capture of the test run if the test fails (or whenever
you like).
* Running the same test repeatedly with different browsers.

The code is pretty easy to use so if you're already using MbUnit v3 and
WatiN then you should find it pretty easy to incorporate some of these
features from the samples into your own tests.

After installing Gallio v3.1 Update 1, look in %PROGRAMFILES%\Gallio\samples
for the WatiN samples and unzip them to a location of your choice.  You'll
need Visual Studio 2008 SP1 to run them out of the box.

Here's one of the sample tests to give you an idea of how this looks:


       /// <summary>
       /// Runs the same test repeatedly with different browsers.
       /// </summary>
       [Test]
       [Browser(BrowserType.IE)]
       [Browser(BrowserType.FireFox)]
       [Browser(BrowserType.Chrome)]
       public void MultipleBrowsers()
       {
           GoogleSearchPage.GoTo(Browser);
           Browser.Page<GoogleSearchPage>().Search("Fiddlesticks");
       }


Full announcement and download links:
http://blog.bits-in-motion.com/2009/10/announcing-gallio-and-mbunit-v31-upda
te.html


You can also see a sample video capture from a previous release here:
http://www.youtube.com/watch?v=rN0CmutflFs

Enjoy,
Jeff.

Technorati Tags:

woensdag 26 augustus 2009

FREE Agile Software Development seminar by David Hussman

This is a shameless plug for a FREE presentation by David Hussman about Agile Software Development. So if you are in the Netherlands on September the 16th sign up!

Following the invitation for this FREE event (or visit the LinkiT site for the dutch invitation)

Introducing Agile Software Development, by David Hussman

On September 16 LinkiT projects provides a presentation on Agile Software Development. The presentation will be given by David Hussman. David Hussman coaches organizations all over the world to adopt and apply Agile principles, techniques and tools. From the recent past there is a close link between David and LinkiT projects, created by an intense collaboration.

For more information about David Hussman, see www.devjam.com.

The presentation consists of two parts. In the first part David talks about why Agile Software Development came to existence, how to make the transition from waterfall to Agile, what pitfalls there may arise, and how to stay agile. In the second part of the presentation David will zoom in on a number of techniques used in Agile Software Development.

Agile software development is currently 'hot'. The most popular methods are currently Scrum and XP, with Scrum being project management focused and XP focuses on the development techniques.

Date / Time: Wednesday, September 16 18:00-21:00

Location: The office of LinkiT, Rijnzathe 9, de Meern, Netherlands (here)

Attending the presentation is free of charge, but the number of seats is limited!

Audience: Developers, Project Managers, IT Managers, CIOs, Architects & User Managers

Sign up now!