Graphical user interface testing is an essential part of quality assurance testing as it lets you look at your application from the user’s perspective. In this article, we’ll consider the automation of GUI testing for desktop Windows applications with Python and Pywinauto. This article will be helpful for quality assurance specialists who are looking for tools to automate Windows GUI testing.
Graphical user interface testing is aimed at ensuring that the graphical user interface (GUI) meets specifications. User interface (UI) testing covers the user’s interactions with the application through a command line or graphical controls. During GUI testing, a quality assurance (QA) specialist tests the functionality of the application through its graphical controls (icons, text fields, buttons) and evaluates the controls. This type of testing can be both manual and automated. Automated GUI testing emulates a user’s interaction with the GUI using automation tools.
According to Mike Cohn’s book Succeeding with Agile, UI testing is at the top of the test automation pyramid:
This means that UI tests should take less time and effort in comparison with unit and service tests. While unit and service tests are more isolated, they uncover application bugs at the early stages of software development. In contrast, UI tests can be conducted only at the latest stages of the development lifecycle.
The main advantage of GUI testing is that it looks at the application under test from the user’s perspective. In this way, GUI testing complements unit tests and service or API tests.
Pywinauto is a set of Python libraries for automating the testing of Windows GUI applications.
You can install the Pywinauto package by running a standard pip command:
Note that installing the 64-bit version requires running the 64-bit Python interpreter:
Pywinauto supports the following Windows GUI technologies:
- Win32 controls through the Win32 API: MFC, VB6, VCL
- MS UI Automation: WinForms, WPF, Qt5, browsers, store apps
However, it doesn’t support Tkinter, Java AWT/Swing, or GTK+.
GUI tests take the form of Python scripts that consist of Pywinauto library methods, which emulate user actions like mouse clicks/movements and keyboard key presses.
Pywinauto provides an important benefit of accessing the GUI control elements with the help of attributes. For instance:
Here, we use the full stop to access the Save button that’s located in the Save As dialog window and call the click method.
There are five main steps of writing a GUI test with Pywinauto:
- Run an application or access a running one.
- Define the main application window.
- Find the necessary control element (button, text field, drop-down list, etc.).
- Perform a user action on the control element (click on the button, enter text, etc.).
- Check the results.
Let’s consider the main steps of GUI test automation using the example of editing and saving a Notepad file:
- Open a text file in Notepad.
- Enter a text and apply formatting to it: for instance, Comic Sans, bold, size 18.
- Save the file in PDF format with the following settings: landscape orientation, paper size A3.
- Check if the PDF file was saved.
Below, we provide a guide for executing this test with pywinauto.
GUI Testing: Basic tips
To call the main Notepad window and its control elements, we need to run the application and create a new text file in it. The Notepad file is initialized in the following way:
In this example, pywinauto runs notepad.exe and opens a new file with a 10-second timeout. This way, we can make the testing process a bit smoother, as the application under test can display its windows and GUI elements with a certain delay. By default, pywinauto looks for an open dialog window and control elements during a certain period of time (approximately 5 seconds) and repeats the search if it fails.
There are also several alternative ways of accessing an application that’s already running. You can specify the process ID (PID), path to the executable, or regular expression for window name identification.
The name of the window or control element can be specified with a regular expression or a name written without spaces or other breaking symbols. Pywinauto will automatically search for the element that most closely matches the name among all opened windows.
To access control elements, you need to let Pywinauto define the application window with these elements. In our case, it’s the Notepad window.
Windows Specification is an object that describes either the application window or the control element. In our scenario, the window can be specified in the following way:
Alternatively, you can describe the window with a regular expression of the title, or specify a particular name:
Pywinauto supports the most common GUI control elements, including:
In order to display all available control elements for a specified window, you need to call the print_control_identifiers method:
This method allows you to see which control elements pywinauto has access to and which of those controls can be further automated.
You can call the control as an attribute of the window where the control is placed. A user action is applied to the element by calling the method.
In our case, after running notepad.exe, we should enter any text and apply formatting to it.
Enter text in the open document:
As a result, we’ll get entered text and indentation (due to the ^A command):
Now we’ll open the Font dialog window and edit the text font:
Here, we used access to the following controls: the main window menu, ComboBox, and Button:
Now, we need to save the file in PDF format with landscape orientation and A3 paper size.
To do so, select a virtual printer through the Notepad menu:
Then specify the document settings: landscape orientation, paper size A3:
Executing the script makes the following dialog windows open sequentially:
Print, PrintingPreference, and Microsoft Print to PDF Advanced Options.
There’s one peculiarity about the way Notepad runs processes and opens dialog windows: If we start Notepad in the 64-bit version of Windows with the help of the 32-bit Python interpreter and pywinauto, then notepad.exe is WoW64, or a 32-bit process, while the Print dialog window is a separate 64-bit process called splwow64.exe. This means that we can’t call the Print process from notepad.exe, but we can access it through a separate invocation by the process name or its PID.
However, we can call the dialog window from notepad.exe if we’re working on 32-bit Windows or run notepad.exe on 64-bit Windows through the 64-bit Python interpreter.
This peculiarity also relates to other applications where dialog windows are implemented in a similar way.
Finally, save the document and close Notepad:
When you conduct GUI testing, you often have to wait for certain events to complete, such as file loading, window rendering, and switching between windows.
In these cases, pywinauto can wait for an event or you can set a timeout:
You can also automate checking of the GUI test results depending on what is under the test. In our case, we can check the existence of the PDF file on the disk with standard Python methods, for instance, by calling os.path.exists.
Pywinauto Windows GUI automation can be a helpful addition to unit tests and API tests. However, when choosing automation tools, pay attention to their capabilities and usability features. Automating Windows GUI testing with pywinauto and Python is quite easy. Pywinauto covers a huge number of Windows GUI technologies while providing easy access to graphical elements of tested applications.
Apriorit has a team of dedicated quality assurance testers whose qualifications are confirmed with ISTQB certification. Contact us if you want to launch bug-free software.