Automate software using WPF UI Automation

No.of Views2194
Bookmarked0 times
Downloads 
Votes0
By  ashraf   On  08 May 2010 06:05:52
Tag : WPF , Applications
I automated instalallation wizard to show the power of WPF UI Automation
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In that article I will show you a funny magic tool which can install/uninstall software automatically. Actually my intension is the show the power of WPF UI Automation. I found UI automation as a replacement of basic application testing and automate anything which we need to make repeatedly.
That article is just a sample work on how can we catch a window and follow entering and changing data in it. As example I am using installation wizard.

 

Image Loading

Background

My recent development task is to automate server deployment for some of the products which are actually deploying regularly and wasting lots of time of the deployment engineers as well sometime people are making mistake which messed up the product.
Deployment automation mainly includes making build, file copy/paste, start/stop services, setup virtual directory, and backup database, modify & run dbscripts, and so on. At the time of deployment automation I found that I need to install and uninstall MSI file and there is no silent mode available for these MSI files but the MSI file installation wizard take input in textbox, need to select checkbox and so on.

Many tools are available to record the installation steps and run it according to schedule but there are many problems using these tools such as error handling, predict on a tool to run it in live server, license issue and so on. So my Chief Architect suggest me to use WPF UI Automation to make a tool which will automatically invoke button, search text box, set value in text box, change radio, measure progress and so on.. I found that WPF UI Automation library is a nice step to do that and I have really done it successfully by a day.

Steps to do

By automating the installation wizard I will show some of the automation techniques. These are as follows.

Step 1: Search the wizard window and click next button to be forwarded (That step will show the codes of how to find a window from the desktop and proceed invoking a button)

Step 2: Search textbox, change text, search radio button, select radio and proceed (That step will show the codes of how to search if any textbox is available in the present widows if any then insert new text in it and how to select/deselect a radio button )

Step 3: Check installation status and proceed (Every installer should have stage to wait for installation complete which normally shows progress bar, automation script need to wait until the installation is completed, here I will show the code to wait for a new status and proceed)

Step 4: Close the wizard (The simplest end of the wizard will be shown in that step)

Step 1

Search the wizard window and click next button to be forwarded

 

Image Loading

At first we need to search for a specific window where the automation script will execute the next steps. I called the title of the window to get the window element. The code is 

AutomationElement rootElement = AutomationElement.RootElement;
if (rootElement != null)
{
	Automation.Condition condition = 
		new PropertyCondition(AutomationElement.NameProperty, "My Application Installer");
	AutomationElement appElement = 
		rootElement.FindFirst(TreeScope.Children, condition);
}

After getting of the main window need to click on next button to proceed. I have written a method named Private AutomationElement GetElementByNameProperty(AutomationElement parentElement, string nameValue)

Here parameter 1 takes the parent element such as parent window, parameter 2 takes the text of the element you are searching for, not only for button will that method be used to find any element containing the name in the second parameter. The code is

private AutomationElement GetElementByNameProperty(AutomationElement parentElement, string nameValue)
{
	Automation.Condition condition = 
		new PropertyCondition(AutomationElement.NameProperty, nameValue);
	AutomationElement element = 
		parentElement.FindFirst(TreeScope.Descendants, condition);
	return element;
} 

Need to invoke the button to go next.

private void ClickElement(AutomationElement element) that method can click on a element if the element is button then click on it, if the element is radio then select. In future I will modify the method for other controls too. The code is 

private void ClickElement(AutomationElement element)
{
	if (element != null)
	{
		if (element.Current.ControlType.Equals(ControlType.Button))
		{
			InvokePattern pattern = 
				element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
			pattern.Invoke();
			Wait(2);
		}
		else if (element.Current.ControlType.Equals(ControlType.RadioButton))
		{
			SelectionItemPattern pattern = 
				element.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
			pattern.Select();
			Wait(2);
		}
	}
} 

Step 2

Search textbox, change text, search radio button, select radio and proceed

 

Image Loading

In that stage I will show a method which can search if any textbox is available in the present widow and it insert data as well is any text box found.

private bool SetValueInTextBox(AutomationElement rootElement, string value). The code is 

private bool SetValueInTextBox(AutomationElement rootElement, string value)
{
	Automation.Condition textPatternAvailable = 
		new PropertyCondition(AutomationElement.IsTextPatternAvailableProperty, true);
	AutomationElement txtElement = 
		rootElement.FindFirst(TreeScope.Descendants, textPatternAvailable);
	if (txtElement != null)
	{
		try{
			Console.WriteLine("Setting value in textbox");
			ValuePattern valuePattern = 
				txtElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
			valuePattern.SetValue(value);
			Wait(2);
			return true;
		}catch{
			Console.WriteLine("Error");
			return false;
		}
	}
	else
	{
		return false;
	}
}

After inserting data in the textbox we have 2 radio button to select/deselect, radio button can be select/deselect same as the button which I have described in step one next button click.

Step 3

Check installation status and proceed

 

Image Loading

In my automation I need to wait for a window before completing installation, that means I need to wait for something, I found that the progress bar is the right control to pick and wait.

private void WaitUntilInstallationComplete(AutomationElement appElement) method mainly wait until the installation is completed means the progress bar ended. The code for the method is as follows.

private void WaitUntilInstallationComplete(AutomationElement appElement)
{
	AutomationElement status = null;
	Automation.Condition statusPatternAvailable = new PropertyCondition(AutomationElement.IsRangeValuePatternAvailableProperty, true);
	int numWaits = 0;
	do{
		Console.WriteLine(numWaits + "Waiting for close button");
		status = appElement.FindFirst(TreeScope.Descendants, statusPatternAvailable);
		++numWaits;
		Wait(5);
		} while (status != null && numWaits < 50);
}

Step 4

Close the wizard

 

Image Loading

At the end we have a close button to invoke and complete installation, same as next button, I described in step1, call the close button and invoke to be close the wizard.

Points of Interest

This is really fun to automate something like that. Software tester can use the example to automate Windows application testing.

References 

Some of the blog post and articles helped and inspired me to develop such a tools using WPF. Here are some links, I apologize if I forget some of the links

Article 1    
Article 2
MSDN

Sample Project Source

Download source files -5 kb

 
Sign Up to vote for this article
 
About Author
 
ashraf
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-01 Aug 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top