0% found this document useful (0 votes)
11 views9 pages

Coded UI Testing with WinAppDriver

This tutorial provides a comprehensive guide on using the UI Automation Framework for automated acceptance testing in Visual Studio 2010, specifically for testing the Windows XP calculator. It outlines the steps to create a test project, add coded UI tests, and implement methods for launching the calculator, clicking buttons, and asserting results. Additionally, it includes appendices detailing system requirements, accessible names, deployment for GUI testing, and modifications needed for Windows 7 compatibility.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Coded UI Testing with WinAppDriver

This tutorial provides a comprehensive guide on using the UI Automation Framework for automated acceptance testing in Visual Studio 2010, specifically for testing the Windows XP calculator. It outlines the steps to create a test project, add coded UI tests, and implement methods for launching the calculator, clicking buttons, and asserting results. Additionally, it includes appendices detailing system requirements, accessible names, deployment for GUI testing, and modifications needed for Windows 7 compatibility.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tutorial – UI Automation Framework

Tu, Pin‐Ying
st
Ver. 1.0, 21 September, 2010

Contents
Contents .........................................................................................................................1
About This Tutorial.........................................................................................................1
Tutorial – Coded UI Testing ............................................................................................2
Appendix A – System Requirement................................................................................7
Appendix B – Accessible Names ....................................................................................7
Appendix C – Deployment for GUI Testing.....................................................................8
Appendix D – Modification for Windows 7....................................................................9

About This Tutorial


Unit test framework is very useful for testing programs. In this tutorial, we will
introduce automatic acceptance testing. To do acceptance testing, we need to use
programs to “simulate” user’s actions and validate system responses. A manual
approach requires a number of testers to operate the system and look at the results
shown on the screen. It works, but is also very expensive. Announced with Visual
Studio 2010, “UI Automation Framework” provides a method to do automatic GUI
(acceptance) testing (see Appendix A – System Requirement). This tutorial
introduces how to use the UI Automation Framework to test the calculator of
Windows XP (for Windows 7, please see “Appendix D – Modifications for Windows
7”).

Window Programming Tutorial – UI Automation Framework 1


Tutorial – Coded UI Testing
Step 1 Create a Test Project
Open Visual Studio 2010 and then choose File  New  Project. In the dialog, choose Visual
C#  Test  Test Project. Key in the project name and click OK button to create a test project.

Step 2 Add Coded UI Test


By default, the wizard loads unit testing framework references and creates a simple test class.
Remove the class. Click right mouse button on the project in the Solution Explorer (Figure 1). In the
context menu, choose Add  Coded UI Test. The wizard loads UI Automation Framework references,
creates another test class, and then pops up a dialog (Figure 2). Click Cancel button, the usage of
this dialog will be introduced in another tutorial.

Figure 1 Add a coded UI test

Figure 2 Generate Code for Coded UI Test Dialog

Window Programming Tutorial – UI Automation Framework 2


Step 3 Modify the Class
Step 2 loads necessary private (reserved for wizards) references, but the codes generated by
the wizard are not needed. Replace all with the following codes.
using [Link];

using [Link];

namespace GUITestTutorial {

[CodedUITest]

public class WinXPCalculatorTest {

private const string FILE_PATH = ""C:\\Windows\\system32\\[Link]"";

private const string MSAA_TECHNOLOGY = "MSAA";

private const string WINDOW_TYPE = "Window";

private const string BUTTON_TYPE = "Button";

private const string TEXT_TYPE = "Edit";

private const string CALCULATOR_TITLE = "小算盤";

private const string TEXT_PROPERTY = "Text";

private const string EXPECTED_VALUE = "444. ";

private ApplicationUnderTest _aut;

private UITestControl _window;

Step 4 Launch the Calculator


Add two member methods Initialize() and FindWindow()with following codes to
launch the calculator. You have to modify the value of FILE_PATH constant if the calculator does
not exist at the specific location.
/// <summary>

/// Launches the Calculator

/// </summary>

[TestInitialize()]

public void Initialize() {

_aut = [Link](FILE_PATH);

_window = FindWindow(CALCULATOR_TITLE);

Window Programming Tutorial – UI Automation Framework 3


UI Automation Framework uses SearchProperties to filter GUI controls on the screen.
Three properties are required: TechnologyName, ControlType, and Name to find a specific
control. The Find()will throw UITestControlNotFoundException that cause the test
failed if the window doesn’t exist.
/// <summary>

/// Finds the specific window on the screen by its title

/// </summary>

/// <param name="title">The title of the window</param>

/// <returns>The found window</returns>

private UITestControl FindWindow(string title) {

UITestControl window = new UITestControl(_aut);

PropertyExpressionCollection properties = new PropertyExpressionCollection();

[Link]([Link], MSAA_TECHNOLOGY);

[Link]([Link], WINDOW_TYPE);

[Link]([Link], title);

[Link](properties);

[Link]();

return window;

Step 5 Close the Calculator


In the unit testing tutorial, there is no Cleanup() method, because garbage collection is
performed automatically after finishing tests. But in this tutorial, this method is required to close
the launched calculator. Add a member method Cleanup() method with the following codes.
/// <summary>

/// Closes the launched program

/// </summary>

[TestCleanup()]

public void Cleanup() {

_aut.Close();

Step 6 Click Buttons


UI Automation Framework offers Mouse and Keyboard classes that can simulate the user’s
actions. Add a member method ClickButton() with the following codes which moves the
cursor to the specific button and clicks on it. In this case, the name is the same as the text displayed
on the button. However, the name is not always the same as the text. Please see Appendix B –
Accessible Names for more details.

Window Programming Tutorial – UI Automation Framework 4


/// <summary>

/// Finds and clicks on the specific button by its name

/// </summary>

/// <param name="name">The name of the button</param>

private void ClickButton(string name) {

UITestControl button = new UITestControl(_aut);

PropertyExpressionCollection properties = new PropertyExpressionCollection();

[Link]([Link], MSAA_TECHNOLOGY);

[Link]([Link], BUTTON_TYPE);

[Link]([Link], name);

[Link](properties);

[Link]();

[Link](button);

Step 7 Write a Test Script


Now, use the ClickButton() method to write a test script. Add a new member method
RunScriptAdd() with the following codes. This method automatically clicks buttons just like
that a user is using the calculator.
/// <summary>

/// Runs the script: 123 + 321 =

/// </summary>

private void RunScriptAdd() {

ClickButton("C");

ClickButton("1");

ClickButton("2");

ClickButton("3");

ClickButton("+");

ClickButton("3");

ClickButton("2");

ClickButton("1");

ClickButton("=");

Step 8 Assert the Result


In order to make sure that the result is correct, add a member method GetResult() with
the following codes to get the calculated result. Note that there is no Name search property because
the calculator didn’t assign the text box an accessible name. This is not a good design for testability.

Window Programming Tutorial – UI Automation Framework 5


However, in this case, the calculator only has one text box so the method works. If there are more
than one text box, this method may fail. Therefore, please make sure that every control you want to
test has an accessible name in homework.
/// <summary>

/// Gets the result in the calculator

/// </summary>

/// <returns>The actual result</returns>

private string GetResult() {

UITestControl text = new UITestControl(_aut);

PropertyExpressionCollection properties = new PropertyExpressionCollection();

[Link]([Link], MSAA_TECHNOLOGY);

[Link]([Link], TEXT_TYPE);

[Link](properties);

[Link]();

return [Link](TEXT_PROPERTY).ToString();

Now, add a member method with [TestMethod] annotation as follows:


/// <summary>

/// Tests that the result of 123 + 321 should be 444

/// </summary>

[TestMethod]

public void TestAdd() {

RunScriptAdd();

[Link](EXPECTED_VALUE, GetResult());

Step 9 Run!!
Click “Run Tests in Current Context” button in the toolbar. While the test is running, please do
not move your mouse or press any key. Any event generated manually will cause the test failed.
Design your actions sequence for practice if you have time after finishing the tutorial, or leave the
practice in your homework.

‐‐ The End ‐‐

Window Programming Tutorial – UI Automation Framework 6


Appendix A – System Requirement
Not every version of Visual Studio provides UI Automation Framework. The
computers in the CSIE Lab have been installed with Visual Studio 2010 Ultimate
version. If you need to do your homework at home, you need to install either the
Ultimate or Premium version.
Professional Premium Ultimate
Unit Testing   
UI Automation Testing  
Figure 3 Comparison of different Visual Studio versions

Appendix B – Accessible Names


How were the “names” used in the tutorial obtained? Programmers! For better
testability, the programmer has to assign [Link] property
value during programming. Note that the name of each testable control must be
unique! The name can be assigned by using the Windows Form Designer (Figure 2) or
by coding. However, there are some exceptions. For example, the accessible name of
tabs (not tab pages) in TabControl is assigned at runtime and its name is the same
as the displayed text. Since a name may be different when a program is running on
an OS with a different language, your design of test codes needs the ability to access
the displayed text for different languages.

Figure 4 AccessibleName property

Window Programming Tutorial – UI Automation Framework 7


Appendix C – Deployment for GUI Testing
Since different computers use different paths, testing a program (project)
developed under the same solution requires a special treatment when the program is
launched. First, please check the project dependency. As shown in Figure 5, the test
project “UniversityGUITest” depends on the production project “University.” That
means University will always be built before UniversityGUITest. Second, double click
on the “[Link]” file. In the Deployment page (Figure 6), enable the
deployment option and click “Add File” button and choose the executable file. Every
time, before running tests, the last executable file will be copied to the test working
directory. Thus, the tester can always use a relative path to launch the program.

Figure 5 Project Dependency

Figure 6 Deployment

Window Programming Tutorial – UI Automation Framework 8


Appendix D – Modifications for Windows 7
If you do this tutorial in Windows 7 (Traditional Chinese version), the test case
will fail, because Win7’s calculator is totally different from Windows XP’s calculator.
You need to modify the following constants:
private const string TEXT_TYPE = "Text";

private const string RESULT_CONTROL_NAME = "結果"; // Add for Windows 7

private const string TEXT_PROPERTY = "DisplayText";

private const string EXPECTED_VALUE = "444";

In addition, modify GetResult() and RunScriptAdd() methods.


private string GetResult() {

UITestControl text = new UITestControl(_aut);

PropertyExpressionCollection properties = new PropertyExpressionCollection();

[Link]([Link], MSAA_TECHNOLOGY);

[Link]([Link], TEXT_TYPE);

[Link]([Link], RESULT_CONTROL_NAME);

[Link](properties);

[Link]();

return [Link](TEXT_PROPERTY).ToString();

private void RunScriptAdd() {

ClickButton("清除");

ClickButton("1");

ClickButton("2");

ClickButton("3");

ClickButton("加");

ClickButton("3");

ClickButton("2");

ClickButton("1");

ClickButton("等於");

Window Programming Tutorial – UI Automation Framework 9

Common questions

Powered by AI

Good testability significantly enhances automated GUI testing effectiveness by ensuring that UI controls are easily identifiable and interactable. Design elements such as assigning meaningful, unique ‘AccessibleName’ properties to UI elements allow tests to locate components reliably. Testable designs often separate UI logic from business logic, reducing maintenance when UI changes. Open and consistent APIs further support integration with testing tools. However, poorly designed interfaces might lack these elements, leading to unstable tests that break with minor UI changes and are hard to maintain or debug, undermining test reliability and increasing maintenance costs .

Setting up a coded UI test involves several steps: (1) Create a Test Project by choosing 'Visual C# -> Test -> Test Project' in Visual Studio 2010. (2) Add a Coded UI Test using the context menu in 'Solution Explorer' and select 'Add -> Coded UI Test'. (3) Modify the class by replacing wizard-generated code with custom test code, including necessary using directives like 'Microsoft.VisualStudio.TestTools.UITesting'. (4) Launch the application to test by adding an Initialize() method to start the calculator. (5) Create a method to find the calculator window using 'UITestControl'. (6) Write test scripts using methods like 'ClickButton()' for UI interactions. (7) Implement assertions to verify test outcomes with methods like 'GetResult()'. (8) Finally, clean up with a method to close the application post-test .

The UI Automation Framework provides significant benefits for automated GUI testing, such as reducing the need for manual testing, which can be time-consuming and expensive. It allows for frequent and consistent regression testing without human error. The framework can simulate complex user interactions and validate UI elements easily. However, drawbacks include the complexity of setting up tests, the need for maintenance when the UI changes, and potential issues with controls lacking 'AccessibleName' properties. Additionally, automated tests can be brittle, failing if the visual appearance of applications changes, and they require significant initial investment in tool setup and learning .

To ensure UI tests are reliable and maintainable across different languages and operating systems, testers should utilize 'AccessibleName' properties for controls, assigning unique names that remain consistent regardless of language or OS changes. Additionally, leveraging localization files to map control identifiers in different languages can decouple test logic from UI text. Tests should abstract interactions into reusable methods to minimize changes necessary when UI updates occur. Furthermore, regular reviews and updates of test scripts aligned with product updates are crucial. Using virtual machines or containers can simulate different OS environments for thorough testing .

The UI Automation Framework can be integrated with existing unit testing workflows to enhance software testing efficiency by combining unit and UI tests for more comprehensive coverage. Using the same testing tools and frameworks, like Visual Studio, can streamline the setup process and reduce learning curves. Coded UI tests can be scheduled alongside unit tests to detect not only logical errors but also interface errors early in the development process. This integration encourages collaboration between developers and testers, allowing for iterative improvements in UI design and functionality. However, integration needs careful planning to prevent test overlap and redundancy, requiring distinct strategies for unit logic and UI behavior testing .

Testers should ensure the testing environment is consistent and isolated from user interactions by avoiding manual interventions during test execution, as stated by the advice not to move the mouse or press keys while tests run. They should also confirm that test environments mirror production settings closely, minimizing discrepancies. Backup configurations and environments prevent variations. Using test-specific setups like virtual machines can reduce interference from other processes. Additionally, proper cleanup methods ensure test environments return to their baseline state before and after tests .

The UI Automation Framework identifies GUI controls using 'SearchProperties', which require 'TechnologyName', 'ControlType', and 'Name' attributes to locate a specific control. Interactions are then performed using methods like 'Mouse.Click()' or 'Keyboard.SendKeys()'. However, issues arise if controls lack 'AccessibleName' properties, which are crucial for reliable test automation. This is problematic if a control doesn't have a unique name or is named differently across operating system languages. Additionally, if controls with dynamic names or multiple text boxes exist without accessible names, tests could fail due to incorrect identification .

Migration from Windows XP to Windows 7 requires adjustments due to differences in the calculators between these operating systems. Specifically, constants like 'TEXT_TYPE', 'RESULT_CONTROL_NAME', and 'TEXT_PROPERTY' need modification. 'RESULT_CONTROL_NAME' and 'TEXT_PROPERTY' are adjusted to '結果' and 'DisplayText' respectively, reflecting control name changes in the Windows 7 UI languages. Additionally, methods 'GetResult()' and 'RunScriptAdd()' require updates to accommodate changes in button labels, e.g., '加' for 'Add' in the Traditional Chinese version of Windows 7. These changes ensure the test aligns with the actual UI elements of the Windows 7 calculator .

The UI Automation Framework manages dependencies and deployment through Visual Studio’s project settings. Dependency management is ensured by defining the test project’s dependencies on production projects, making sure changes are compiled and included in tests. Deployment is managed via 'local.testsettings', where enabling the deployment option allows testers to specify and add necessary files. This ensures that the latest executables are copied to the test directory, facilitating consistent testing across different environments and reducing path issues, allowing relative paths for program launches .

The UI Automation Framework supports testing in non-English environments primarily through using control properties like 'AccessibleName', which can be set appropriately for different languages. It also supports searching GUI controls by 'Name', which testers can map to different languages using localization techniques. However, limitations include the need to modify test scripts manually if controls don't have localized names or when the UI design changes significantly between language versions. There is also a requirement for programmers to ensure controls are adequately named and accessible across languages .

You might also like