Introduction to UI Automation in WPF
Automation helps developers to test the User Interface (UI) of the application, which is a more common, convenient and useful approach in modern days. It reduced manual work of clicking buttons and typing text as it is all done by automation tools. With the help of automation scripts type, click and confirm the messages. This is more significant and faster than manual testing. Moreover, it simplifies the testing process.
Why Automate WPF Testing?
WPF testing includes the testing of the application created based on WPF. Compared to manual testing, automated testing for WPF (Windows Presentation Foundation) based desktop applications is faster and more effective. It is particularly useful when testing intricate logic or performing repetitive operations. Automated test cases remain consistent, making it easier to generate reports and fix errors. It also supports assertions that help verify the program’s functionality and ensure that software modifications do not affect its behavior. It is also known as regression testing. In short, Automated test cases speed up development and enhance software quality.
Overview of the White Framework
The white framework is one of the frameworks that help create automated test cases. It is lightweight, free (does not require a license), and easy to maintain. The white framework is compatible with Win32, WinForm, and WPF, and is built on .NET framework. Therefore, it does not need to learn different scripting languages. The white framework provides a user-friendly way to test an application’s user interface without relying on Microsoft’s UI Automation library.
Setting Up the White Framework in Project
It is very easy to connect testing applications with white framework automation. The following steps guide you on how to connect your applications with the automated testcase project.
- Set Up White Framework Project: Include the white framework project in the solution. You can find the package on Nuget or can easily download the package directly into the project.
- Find Application Path: Find the directory that contains the applications executable file (.exe) on the computer.
- Start the Application: To run the application, use the Application.Launch() function.
- Get Main Window of the Application: Locate the application’s main window using the Application.GetWindow(), by passing the window name or title.
- Manipulate UI Elements: To find and interact with buttons, text boxes, and other app interface elements, use utilities like SearchCriteria and UIItems.
- Add Test Validations: When writing tests, it’s important to assert the UI state and check that your application is performing as expected.
Launching and Attaching to a WPF Application
Some of the DLLs must be included in the automation project to utilize the white framework. The project should have those references for the predefined functions of the white framework.
There are two ways to attach an application to an automation project—attach an already running application instance or launch a fresh instance of the application.
Locating UI Elements in White Framework
White framework automation is a key component of automating the WPF application. It can control and interact with the application’s UI elements. The components’ positions must be correct for automation scripts that can trigger necessary actions such as button click, write text in textbox, and monitor the control visibility. It can be possible in white framework with the help of the following efficient tools.
- Automation ID
- The developer assigns a programmatic identification name to the control, called AutomationID. With the help of the code below, we can access the control.
- Text:
- In the application, set the element name using the code below so we can access the button in the UI for which test needs to be performed.
- Control Type
- The application has a lot of controls like button, textbox, and combobox that can be identified by control types. The code below helps to get all components of that class.
- Index
- Index can be used to get any controls at specific index from the list of controls. Example below is to get first button from the list of button controls.
Interacting with Controls
- Button: Click the button with the help of button.Click().
- Text Box: Write the text with the help of textBox.Text = “YourText”.
- Combo Box:
- Select an item from combo box with the help of comboBox.Select(“<<Selected item text>>”).
- Additionally, we can select an item on combo box with the help of comboBox.SelectedItemText.
- Checkbox: State of the checkbox control can be verified using the Checked property and by setting any value to this property, one can update the state of the checkbox control.
- Radio Button: Activate a radio button with radioButton.Click().
- List Box: Select an item using listBox.Items from the list box.
- Handling Dialog: To handle the dialog box in the application, first get the dialog box control then perform appropriate action on it.
Assertions and Validations
The automated test cases must add assertions to ensure the application behaves as expected in various scenarios. The assertions compare what the program does with what it should do. It verifies that the application functionality works as expected. The assert class is supported by various frameworks such as NUnit, xUnit, and MSTest. Moreover, this class has a number of methods that help validate the results like matching the value, condition is satisfied, and object is null. In short, assertions and validations increase the accuracy of the application.
Examples of Different Assertions:
- Assert.AreEqual (expected, actual): It verifies that two specified generic types of data are equal by using the equality operator, and it fails when the objects are not equal.
- Assert.AreNotEqual (notExpected, actual): It verifies that two specified objects are not equal, and it fails when the objects are equal.
- Assert.IsTrue (condition): It verifies that the specified condition is true, and if the condition is false, then the assert fails.
- Assert.IsFalse (condition): It verifies that the specified condition is false, and if the condition is true, then the assert fails.
- Assert.IsNull (object): It verifies that the specified object is null, and if the object is not null, then the assert fails.
- Assert.IsNotNull (object): It verifies that the specified object is not null, and if the object is null, then the assert fails.
- Assert.AreSame (expected, actual): It verifies that two specified object variables refer to the same object, and it fails when the objects are not the same.
- Assert.AreNotSame (expected, actual): It verifies that two specified object variables refer to different objects, and it fails when the objects are the same.
Error Management
Log4net is used to manage logs in the white framework automation project. As the applications are automated using the white framework, logs make it easier to monitor the automation process and identify issues. Additionally, logs help store the results of test cases for future reference.
- Install log4net:
- Open your automation project in a visual studio.
- Include your project with the log4net library:
- In the visual studio, open the “Tools” menu from the menu bar to launch the NuGet Package Manager Console.
- To install the log4net library in the project, enter the command as below:
- “Install-Package log4net -Version 2.0.8”.
- Configurations need to be set in App.config file for log4net:
- Appenders:
- It defines the appenders (such as ConsoleAppender, FileAppender, and RollingFileAppender), which decide where the log messages are sent.
- Layouts:
- Specify the log message format (e.g., PatternLayout).
- Loggers:
- Define the appenders to be used for the log messages and also specify the message level.
- Root Logger:
- Define the root logger that manages all logs without a designated logger.
- Appenders:
- Use log4net in Your Code:
- With the help of the sample code below, write the logs in the automation program.
Take Screenshot on Assert Fail
To create a screenshot of the screen when any error occurs in the automated testcase, the method below can be used which will take the snapshot of full desktop screen and saved as PNG. When an error occurs, it will take screenshot during an error and aid in the investigation.
Run Automated Testcase with Batch File
Execute the automated test cases from the batch file. Follow the steps below:
- Install test agent:
- Install an agent namely: “en_agents_for_visual_studio_2015_update_3_x86_x64_9631275.exe “
- It is easily available on the Internet; you just need to install it on the system to execute automated test cases using batch files.
- Download agent link: https://my.visualstudio.com/Downloads?q=agents%20visual%20studio%202015
- Copy bin folder:
- Copy the bin folder and paste it into the system, where visual studio is not installed and run automated test cases using batch files.
- Create a batch file:
- The batch file can be created using the shared code below in notepad and by saving it with “.bat” extension.
- Then launch the command prompt using the administrator privileges.
- Navigate to batch location.
- Execute the batch file using the command prompt.
- When test cases execution is completed
- The application is automatically closed, and the result of the test case is written in the command prompt.
- The batch file can be created using the shared code below in notepad and by saving it with “.bat” extension.
Automation of Stress Testcases
- Stress testing is used to evaluate how an application responds under excessive load conditions.
- Crucial strategies to design stress test case automation
- Mimic the application restart behavior:
- To implement the restart behavior of the application under test, we need to implement three major operations as mentioned below:
- Close/Shutdown the application
- We implement changes to close the already running application and free up the resources.
- Launch/Start the application again
- We start the application and wait for it. We can implement startup logics such as login into applications.
- Continue stress test case
- We proceed to execute the stress test case steps.
- Close/Shutdown the application
- Execute the test for multiple days:
- To evaluate the stress test case, it is compulsory to perform the test for multiple days to find out how the application responds under extreme load conditions.
- To implement this tactic, we can use looping to execute similar test steps multiple times.
- Make the test case execution days and application restart interval configurable:
- To make the test case more dynamic and robust, configure the values for test case execution and application restart into the configuration file.
- The values are read from there, and the operation is performed accordingly.
- Provide a way to stop the test case in between:
- It is important to provide a mechanism to stop the execution of long running stress test cases to ensure that test cases can be stopped early without losing necessary results whenever required.
- This can be done in multiple ways and to achieve this, we have created one utility that provides an icon in the system tray used to stop the test case execution.
- One thing to note here is that test cases are stopped after the current executing iteration is completed to ensure that test results have persisted as abnormal termination of test cases may create problems.
- The mechanism to stop test cases is to create one text file in a certain folder when clicking on the stop test case icon.
- At the end of every iteration, the test case checks if the test file was created or not. If the text file is there then it stops the test case execution from there else, it continues execution.
- To implement the restart behavior of the application under test, we need to implement three major operations as mentioned below:
- Mimic the application restart behavior:
Use Automation to Analyze Application Performance
- To find bottleneck scenarios, safeguarding the application, securing reliability, validating scalability, and improving user experience, performance analysis of the application is necessary. It helps ensure that the application can work well in a variety of scenarios and handle both expected and unexcepted loads. This involves spotting possible problems before they affect users, such as slow load times, responsiveness issues, and stability issues.
- To measure CPU utilization, we are using the process dump command line utility that is used to monitor an application for CPU spikes and creating crash dumps during a spike.
- Process dump utility can be downloaded from here: https://learn.microsoft.com/en-us/sysinternals/downloads/procdump
- To run the utility, we can launch the process by using command line arguments as below:
- “ProcessDumpFileExeLocation” is a location where executable file for utility resides.
- -accepteula option to automatically accept the Sysinternals license agreement.
- -c option to set the CPU threshold above to create a dump of the process.
- -s option to set consecutive seconds before the dump is written.
- -n option to set number of dumps to write before exiting.
- -w option to wait for the specified process to launch if it’s not running.
- “NameOfTheApplicationUnderTest” is the name of the application that needs to be attached to the process dump utility.
- “DumpResultLocation” is the location where the dump result is created.
- Our aim is to start the dump process whenever the application starts, end it when closing the application, and save the result.
- Text file is created for the dump process result with dump files, if dump has reached the CPU threshold value.
- For instance, we are monitoring performance of Visual Studio application by configuring the dump process to dump result if the CPU threshold exceeds 1% for consecutive two seconds.
- Dump result text file:
Memory Consumption Analysis of Application with Automation
- Automation in memory consumption analysis testing is really helpful in crash prevention, application performance optimization, and to improve scalability. Developers can detect and fix memory leaks, lower the frequency of Out-of-Memory (OOM) issues, and enhance the stability and dependability of their programs by automating the memory usage monitoring process.
- Automation for memory consumption analysis for an application can be constructed using various tools and techniques for both real-time monitoring and postmortem analysis.
- Use DotMemory tool to identify memory consumption:
- For.NET applications, DotMemory is a memory profiler tool that aids in analyzing memory usage and locating possible leaks. To comprehend memory traffic and object retention, this tool gathers information on memory allocation, takes snapshots of the application’s memory, and compares them.
- DotMemory command line tool can be downloaded from here: https://www.jetbrains.com/dotmemory/download/#section=commandline
- To effectively identify areas of optimization for memory consumption in .NET applications, DotMemory tool provides great features such as snapshots, retention graphs and call tree analysis.
- The code snippet below can be used to generate memory snapshot for any application or process:
- To create automation in the test case to generate memory consumption analysis, after each test case iteration memory snapshots can be taken to analyze the memory usage of the system under test.
- DotMemory snapshot can be analyzed by opening it in the DotMemory UI application where it can be compared to the other snapshots and identify memory leak, memory traffic, and get an understanding of object retention using the available views.
- Use “TypePerf” tool to identify application performance data:
- Performance information can be written to a file using the “TypePerf” command. Performance counters that are metrics offering information about how well application resources like CPU, memory, disk I/O, and network are performing, are gathered and shown using it. The operating system version, service/process status, and general system health are just a few of the application-related variables that “TypePerf” can be used to track.
- “TypePerf” command is available in Windows by default. No additional installation is required for this.
- The code snippet given below can be used to start or stop the process to generate performance data:
-
- Performance information can be written to a file using the “TypePerf” command. Performance counters that are metrics offering information about how well application resources like CPU, memory, disk I/O, and network are performing, are gathered and shown using it. The operating system version, service/process status, and general system health are just a few of the application-related variables that “TypePerf” can be used to track.
- To automate the test case and create performance data, start the process to generate performance data at the start of the test case and stop the same process at the end of the test case.
- Further, we can analyze the performance data (generated in .csv file), to identify the memory leakage at a specific time by creating graphs of the data and we can investigate application logs at specific timestamp to find the memory spike.
- For.NET applications, DotMemory is a memory profiler tool that aids in analyzing memory usage and locating possible leaks. To comprehend memory traffic and object retention, this tool gathers information on memory allocation, takes snapshots of the application’s memory, and compares them.
Limitations of White Framework
- One of its major disadvantages is the lack of active maintenance.
- It does not have backing for the latest updates and technologies as it has been developed to work with the older version of the .NET Framework and WPF technology.
- To communicate with UI elements using the white framework, UI automation tree is crucial.
- If the elements are not correctly visible in the UI automation tree (such as Custom Controls), you cannot interact with them using the White Framework.
- One of the major needs in modern testing practices is support for parallel testing, and the White Framework does not support that but sequential testing.
- As White Framework was created entirely for Windows applications, it does not support cross-platform testing. This restriction is a major challenge as cross-platform applications are becoming more common in today’s software environment.
Conclusion
- A consistent and effective way to assure the functionality and dependability of an application is to use the White Framework in automating the stress and memory tests for the WPF applications. UI automation provides great ways to mimic real world user interactions and to find out probable complications or bottlenecks early in the development cycle. White Framework is easy to use and provides extensive support for the WPF applications, which makes it an effective tool to develop automated test cases duplicating complex user scenarios.
- Memory testing tools such as DotMemory and TypePerf can be incorporated into testing procedures as it not only improves the testing procedures but also provides comprehensive insights into memory utilization and possible memory leaks. In addition to that, to evaluate how an application behaves underneath extraordinary load and to improve resources use effectively, one can use process dumps in performance testing.
- It is important to distinguish the White Framework’s disadvantages from its numerous benefits, as its disadvantages include uneven stability complications and limited help for some cutting-edge UI elements. These complications might be addressed with careful research and extra resources to reach comprehensive test coverage.
- To sum up, automation of stress and memory test cases provides noteworthy saving in time and work during the testing process. On the other hand, it concurrently improves the quality and functionality of the WPF applications. By implementing the methods and resources covered in this blog, one can develop more robust and effective testing procedures for the WPF applications, promising a flawless user experience.
Know More@ Product Testing Services