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 google.com 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 = "http://www.google.pl"
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 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.