Windows Application Automation TestingIn my previous blog, I put forth a strong case for adopting Coded UI Test (CUIT) for automation testing. As a result, I have been receiving a lot of comments asking how to automate ‘Windows Applications’ using Coded UI Test. With this in mind and to also provide readers an insight on Windows Application Automation Testing, I have written this blog. Using Coded UI Test, software testers can perform actions on controls (UI Elements) using UIMAP (an in-built feature of Coded UI Engine) or Code First approach (Hand Coding).

What is Code First Approach?

The Code First approach is a relatively new workflow introduced by Microsoft. It is utilized to generate UI Map elements by using Search (or) Filter properties of a control, instead of using UI Map objects generated by the Coded UI Test Builder.

Windows Application Automation Testing

In order to gain a better understanding of windows application automation testing, let’s first discuss about handling some of the windows applications controls using Coded UI Test.

Generating Controls using Code First Approach

Here are some of the windows controls, where we can perform actions WinButton, WinCalender, WinCell, WinColumnHeader, WinComboBox, WinControl, WinCustom, WinEdit, WinGroup, WinHyperLink, WinList, WinMenuItem, WinListItem, WinMenuBar, WinPane, WinRadioButton, WinScrollBar etc.

I am sure most of the software testers are aware that an object is built depending on its search (or) filter properties.

Handling Controls of Windows Applications

The following namespace has to be added, while handling the controls of Windows applications:

using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
How to Launch a Windows Application during Run Time

Below is the syntax that is used to launch a Windows application during run time:

ApplicationUnderTest obj = ApplicationUnderTest.Launch(ExePath, AlternateExePath);

If you observe the above syntax, the ExePath is mandatory, whereas the AlternateExePath is optional. The AlternateExePath is used to launch the .exe (just in case the playback engine fails to identify the given ExePath).

Generating Textbox Control

The following method is used to perform actions on a textbox control using its return value:

public static readonly WinWindow WinWindow = new WinWindow();
public WinText TextboxObject()
{
var txtBoxProps = new WinText(WinWindow) { TechnologyName = "MSAA" };
txtBoxProps.SearchProperties.Add("Name", "txtBoxNameProperty");
return txtBoxProps;
}
Generating Checkbox Control

The following method is used to perform actions on a Checkbox control, using its return value:

public static readonly WinWindow WinWindow = new WinWindow();
public WinCheckBox CheckBoxObject()
{
var chkProps = new WinCheckBox(WinWindow) { TechnologyName = "MSAA" };
chkProps.SearchProperties.Add("Name", "CheckBoxNameProperty","Id","checkBoxIdproperty");
return chkProps;
}
Generating Radio Button Controls

The following method is used to perform actions on a Radio button control, using its return value:

public static readonly WinWindow WinWindow = new WinWindow();
public WinRadioButton RadioButtonObject()
{
var radioProps = new WinRadioButton(WinWindow) { TechnologyName = "MSAA" };
radioProps.SearchProperties.Add("Name", "RadioName", "Id", "RadioIdProperty");
return radioProps;
}
Generating Button Controls

The following method is used to perform actions on a Button control, using its return value:

public static readonly WinWindow WinWindow = new WinWindow();
public WinButton ButtonObject(string btnNameProperty)
{
var btnProps = new WinButton(WinWindow) { TechnologyName = "MSAA" };
btnProps.SearchProperties.Add("Name", btnNameProperty);
return btnProps;
}

Now that we have gained a good understanding of creating controls using the Code First approach, let us now consider a use case to gain an overview on windows automation testing, the test object used here is a calculator application, which is a widely used application in the windows environment.

Launching a Calculator Application During Runtime

The below code snippet would help testers to launch the calculator application:

ApplicationUnderTest calcApp = ApplicationUnderTest.Launch("C:\\Windows\\System32\\calc.exe", "%windir%\\System32\\calc.exe");
Available Actions

Following are some of the actions that software testers can perform on the ‘calcApp’ object:

Windows Application Testing - Actions

Addition Operation and Output Verification

Performing addition operation and output verification on the calculator application. The following illustration depicts a calculator’s button object properties:

Windows Application Automation - Calculator Button Properties

Generation of Calculator Objects using Code First Technique

Here’s the code to perform actions on calculator’s button and a textbox: 

public static readonly WinWindow WinWindow = new WinWindow();
///summary
///Constructor - Passing Calc Windows Tittles and Calc ControlId
///summary
 public CalculatorScenarios()
 {
 WinWindow.WindowTitles.Add("Calculator");
 WinWindow.SearchProperties[WinWindow.PropertyNames.ControlId] = "131";
 }
 public WinButton ButtonObject(string btnProp)
 {
 var txtProps = new WinButton(WinWindow) { TechnologyName = "MSAA" };
 txtProps.SearchProperties.Add("Name", btnProp);
 return txtProps;
 }
 public WinText TextboxObject()
 {
 var resultProps = new WinText(WinWindow) { TechnologyName = "MSAA" };
 resultProps.SearchProperties.Add("Name", "Result");
 return resultProps;
 }
Test Script/Automated Test Scenario
///summary
///Performing Addition Operation and Output Verification
///Clicking on Button 1 and 2 and verifying output is 3 or not
///summary
[TestMethod]
public void CalcAdditionAndOutputVerification()
{
Mouse.Click(ButtonObject("1"));
Mouse.Click(ButtonObject("Add"));
Mouse.Click(ButtonObject("2"));
Mouse.Click(ButtonObject("Equals"));
var outVal = TextboxObject().DisplayText;
Console.WriteLine(Equals(outVal, "3") ? "PASS : Output is correct" : "FAIL : Output is not correct");
}

Below is the output screen that confirms that the test validation is passed. As can be seen, the program was written in Coded UI to add the numbers 1 and 2 and verify, if the output is 3 or not:

Windows Application Automation Testing

Conclusion

On the whole, it has been observed that generation of controls is a lot easier in Coded UI, as compared to other automation testing tools. Furthermore, it is also easy to include Assertions in Coded UI. Hope this post helps readers to gain better insights on windows application automation testing using Coded UI.