Wednesday, July 24, 2013

A Basic example of web site automation with TestStack.White

TestStack.White is an ancestor of old White UI Automation framework. The framework installs easily from NuGet.

To automate the web browser, we need to launch it and then automate the UI using White’s API. There are two ways to automate an application in white:

  • we can use the API to find controls in the control hierarchy and then use the API of found control to automate it (for example, the TextBox class that corresponds to input fields has the Text property with two accessors, get and set)
  • we can use the API to move the mouse and input data from the keyboard

The first method, although sounds interesting, is rather difficult when automating a web browser. The problem is that the White search API is rather limited and works better if the underlying application is a .NET application. For example, any control on the screen can be found by its .NET “name”.

In case of a native application (and browsers are not .NET applications), the only way to find a specific control is to get a complete list of controls and try to find a match programmatically. And this is why it is easier said than done. For example, trying to automate the web site and searching for all visible textboxes on FireFox yields a list of 42 controls with no way of picking this correct one that should contain the search phrase.

Let’s then see the code snippet and discuss it:

var psi = new ProcessStartInfo 
    FileName  = "iexplore.exe", 
    //FileName  = "firefox.exe",
    Arguments = "" 
Application application = Application.Launch( psi );
Window window = application.GetWindows().FirstOrDefault();
// find control and input text ...
//IUIItem[] textBoxes = window.GetMultiple( 
//   SearchCriteria.ByControlType( System.Windows.Automation.ControlType.Edit ) );
//TextBox searchTextBox = textBoxes[1] as TextBox;
//if ( searchTextBox != null )
//    searchTextBox.Text = "search phrase";
// .. or rather simulate user input
window.Keyboard.Enter( "search phrase" );
window.Keyboard.PressSpecialKey( White.Core.WindowsAPI.KeyboardInput.SpecialKeys.RETURN );
//window.Mouse.Location = new Point( 800, 870 );

As you can see it all starts with lauching the web browser’s process. Then I am selecting the very first active window of the process and this is where things start to be messy – this works only if there are no other active instances of the same browser.

Then the commented out snippet shows how to use the API to find a text box and set its value but then it turns out that providing the input is easier if made with the Keyboard and/or Mouse objects.


devika iangar said...

devika iangar said...

I think I have never watched such online diaries ever that has absolute things with all nuances which I need. So thoughtfully update this ever for us.
difference between analysis and analytics

tejaswini said...

Incredibly conventional blog and articles. I am realy very happy to visit your blog. Directly I am found which I truly need. Thankful to you and keeping it together for your new post.
data science training

360DigiTMGNoida said...

You finished certain solid focuses there. I did a pursuit regarding the matter and discovered almost all people will concur with your blog.
iot training in noida

PMP Certification said...

I feel extremely glad to have seen your site page and anticipate such a large number of additionally engaging occasions perusing here. Much obliged again for all the subtleties.

prathyusha said...

Truly overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. Much obliged for sharing.Best data analytics course in Hyderabad

tejaswini said...

Hello there to everyone, here everybody is sharing such information, so it's fussy to see this webpage, and I used to visit this blog day by day
internet of things malaysia