
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:
[c]
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
[/c]
How to Launch a Windows Application during Run Time
Below is the syntax that is used to launch a Windows application during run time:
[c]
ApplicationUnderTest obj = ApplicationUnderTest.Launch(ExePath, AlternateExePath);
[/c]
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:
[c]
public static readonly WinWindow WinWindow = new WinWindow();
public WinText TextboxObject()
{
var txtBoxProps = new WinText(WinWindow) { TechnologyName = "MSAA" };
txtBoxProps.SearchProperties.Add("Name", "txtBoxNameProperty");
return txtBoxProps;
}
[/c]
Generating Checkbox Control
The following method is used to perform actions on a Checkbox control, using its return value:
[c]
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;
}
[/c]
Generating Radio Button Controls
The following method is used to perform actions on a Radio button control, using its return value:
[c]
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;
}
[/c]
Generating Button Controls
The following method is used to perform actions on a Button control, using its return value:
[c]
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;
}
[/c]
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:
[c]
ApplicationUnderTest calcApp = ApplicationUnderTest.Launch("C:\\\\Windows\\\\System32\\\\calc.exe", "%windir%\\\\System32\\\\calc.exe");
[/c]
Available Actions
Following are some of the actions that software testers can perform on the ‘calcApp’ object:
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:
Generation of Calculator Objects using Code First Technique
Here’s the code to perform actions on calculator’s button and a textbox:
[c]
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;
}
[/c]
Test Script/Automated Test Scenario
[c]
///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");
}
[/c]
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:
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.
Author
![]() |
Naveen Varadaraju is Sr. Software Test Engineer at Evoke Technologies. His area of expertise is automation testing, he is highly proficient in using various testing tools, including Coded UI and Unified Functional Testing (UFT). Naveen closely follows emerging trends in the software testing domain. |



