Are you tired of the boring operation of repeatedly clicking on the software interface every day? Do you want to easily control desktop programs like automated web pages? Today, when automation testing is gradually expanding to client desktops, if you don’t know pywinauto, you are really behind!
Manually test Windows desktop applications, repeatedly click and enter, so tired that you doubt your life? pywinauto comes to save the scene! This Python library allows you to control Windows GUI like a "magic", easily simulate mouse and keyboard operations, and automate Notepad, Excel and even enterprise-level software. "pywinauto makes Windows automation as simple as writing scripts!" Data shows that pywinauto can shorten the test time by 80% and increase the coverage by 90%. Whether you are a test novice or an RPA developer, this article starts from scratch and takes you to practical cases through environment construction and control positioning, play pywinauto, and the automation skills are soaring!
What is pywinauto? How to use it to implement Windows automated testing? From environment construction to test case design, what are the specific steps? How to deal with complex scenarios?
Combining opinions and cases
pywinauto connects Windows applications through the Python API, locates controls and simulates user operations, supports Win32 and UIA backends, and is suitable for GUI testing, data acquisition and automation tasks. The following are five major steps to implement automated testing, with practical cases and code.
pywinauto is a software (GUI) used to automate Python modules and is suitable for Windows systems. It can traverse windows (dialogs) and controls in the window through Pywinauto, and can also control mouse and keyboard input, so it can do more than the pysimplegui introduced earlier.
Official website documentation:/en/latest/
1. Environment construction: Quick start pywinauto
Scenario: Install Python, pywinauto and dependencies to prepare for the test environment.
step:
Install Python (3.6+): Download it from the Python official website to ensure that pip is available.
Install pywinauto: Run pip install pywinauto.
(Optional) Install and debugging tools: such as pywin32 (pip install pywin32) and Spy++ (with Visual Studio) or (Windows SDK).
Verify installation: Run python -c "import pywinauto", and success will be successful without an error.
Case: A testing team installed pywinauto, completed the environment construction in 5 minutes, and began to test enterprise WPF applications. Code (verified installation):
import pywinauto print("pywinauto installed successfully!")
Practice: Install pywinauto and run the above code to verify the environment.
2. Application: Start and connect to the application
Scenario: Use Application to start a new application or connect to existing processes.
core:
Application(backend="win32"): Suitable for traditional Win32 applications (such as Notepad).
Application(backend="uia"): Suitable for modern WPF/WinForms applications (such as Excel).
Method: start() starts a new process, connect() connects to the application while running.
Code (start Notepad and connect to Excel):
from pywinauto import Application # Start Notepadapp_notepad = Application(backend="win32").start("") # Connect to running Excelapp_excel = Application(backend="uia").connect(title_re=".*Excel.*")
Note: start() directly runs the executable file, connect() is positioned through window title, regular expression or process ID.
Case: A company uses Application to connect to ERP software and automatically enters data, which improves efficiency by 70%.
Practice: Start Notepad and try to connect with connect(title="Untitled - Notepad").
3. WindowSpecification: Accurately locate windows and controls
Scene: Use WindowSpecification to locate windows and element controls (such as buttons and text boxes).
core:
WindowSpecification is positioned through window title or control attributes and supports chain calls.
Use print_control_identifiers() to view the control tree and get the control name, ID, or type.
Method: child_window() locates the child control, wrapper_object() obtains the operable object.
Code (locate Notepad edit box):
from pywinauto import Application # Start Notepadapp = Application(backend="win32").start("") # Get windowdlg = app["Untitled - Notepad"] # Print the control treedlg.print_control_identifiers() # Positioning Edit Boxedit = dlg.child_window(control_type="Edit").wrapper_object()
Description: print_control_identifiers() outputs the control hierarchy, and control_type is based on UIA (such as Edit, Button).
Case: A test team used WindowSpecification to locate the "submit" button of WPF application, automates clicks, and the test coverage rate reached 95%.
Practice: Run the above code, view the Notepad control tree, and locate the edit box.
4. Keyboard and mouse operations: Simulate user behavior
Scenario: Simulate keyboard input, shortcut keys and mouse clicks.
core:
Keyboard: type_keys() Enter text or shortcut keys (such as ^s means Ctrl+S).
Mouse: click(), double_click(), right_click() simulates mouse operations.
Supports and fine module control.
Code (Notepad input and save):
from pywinauto import Application # Start Notepadapp = Application(backend="win32").start("") dlg = app["Untitled - Notepad"] edit = dlg.child_window(control_type="Edit").wrapper_object() # Keyboard inputedit.type_keys("Hello, pywinauto!{ENTER}Let's automate!") # Click the "File" menu with the mousedlg.menu_select("File -> Save As") # Save the filesave_dlg = app["Save As"] save_dlg.Edit.set_text("") save_dlg.()
Description: type_keys() supports special keys such as {ENTER} and ^s, and menu_select() operates the menu.
Case: A company used pywinauto simulated keyboard to enter CRM data, and 1,000 records dropped from 2 hours to 10 minutes.
Practice: Run the above code, enter the text in Notepad and save as.
5. Practical case: Excel automated data entry
Scenario: Automated input sales data into Excel tables and save them.
step:
Start Excel with Application and open the workbook.
Position worksheets and cells with WindowSpecification.
Enter data with the keyboard and click Save with the mouse.
Code:
from pywinauto import Application import time # Start Excelapp = Application(backend="uia").start(r"C:\Program Files\Microsoft Office\root\Office16\") (3) # Wait for Excel to start # Connect to Excel windowdlg = (title_re=".*Excel.*") # Open the workbook (Ctrl+O)dlg.type_keys("^o") open_dlg = app["Open"] open_dlg.Edit.set_text(r"C:\Users\YourUser\Documents\") open_dlg.() (1) # Position the worksheet editing areasheet = dlg.child_window(control_type="Edit").wrapper_object() # Enter data (3 lines)sheet.type_keys("100{ENTER}200{ENTER}300") # Save and close (Ctrl+S, Alt+F4)dlg.type_keys("^s") (1) dlg.type_keys("%{F4}")
Description: () Make sure the window is loaded, and control_type="Edit" locates the Excel editing area.
Case: A retail company uses pywinauto to automate Excel reports, process 5,000 pieces of data, and improves efficiency by 95%.
Practice: Adjust the file path, run the above code, enter 3 lines of data into Excel and save it.
6. Simulation operation: Perform automated tests
Scenario: Simulate user input, click and other operations.
method:
Use type_keys() to enter text, click() simulates mouse click.
Supports shortcut keys (such as Ctrl+S) and complex operations (such as menu selection).
Code (enter and save in Notepad):
from pywinauto import Application app = Application(backend="win32").start("") dlg = app["Untitled - Notepad"] edit = # Enter textedit.type_keys("Hello, pywinauto!") # Save the file (Ctrl+S)dlg.type_keys("^s") save_dlg = app["Save As"] save_dlg.Edit.set_text("") save_dlg.() case:Used by a certain enterprisepywinautoautomationExcelData entry,1000Data from2The hours are shortened to10minute。 practice:Run the above code,existNotepadEnter text and save as。
Debugging and Optimization Tips
Debugging Tips:
Control positioning: Use or print_control_identifiers() to confirm the control properties, and use control_type or auto_id first.
Stability: Add () or ("visible") to wait for the window/control to be ready.
Error Troubleshooting: Catch ElementNotFoundError, print control tree or window title.
Optimization tips:
POM mode: Encapsulate control operations as classes to improve script reusability.
Pytest integration: Automate the run of test cases and generate Allure reports.
Complex operation: Use precise control of coordinate clicks.
Code (POM encapsulation Excel operation):
from pywinauto import Application class ExcelApp: def __init__(self): = Application(backend="uia").start(r"C:\Program Files\Microsoft Office\root\Office16\") = (title_re=".*Excel.*") def input_data(self, data): sheet = .child_window(control_type="Edit").wrapper_object() for value in data: sheet.type_keys(f"{value}{{ENTER}}") def save_and_close(self): .type_keys("^s") .type_keys("%{F4}") # useexcel = ExcelApp() excel.input_data([100, 200, 300]) excel.save_and_close()
Case: A team used POM to refactor pywinauto scripts, and the maintenance time dropped from 2 days to 2 hours after the UI was changed.
Practice: Refactor Excel cases with POM and run tests.
Notes and FAQs
Notes:
Backend selection: Win32 is suitable for simple applications (such as Notepad), UIA is suitable for complex WPF/WinForms (such as Excel), and confirm before testing.
Control dynamics: The control ID or title may change with language/version, so control_type or regular expressions are preferred.
Performance: Avoid too much() and use wait() or wait_until() to optimize.
FAQ:
Error: ElementNotFoundError
Solution: Check the window title or control properties and print the control tree to confirm.
Error: TimeoutError
Solution: Extend the waiting time or check if the application is responsive.
Error: InvalidWindowHandle
Solution: Make sure the window is not closed and reconnect to the application.
Case: A project reported an error due to a change in the control ID, and it was positioned with control_type and then run stably.
Practice: Simulate an error (such as the error title) and troubleshoot according to the log.
Application
The first thing we need to control software is to start a Windows software, and each software (process) is an Application object
When instantiating the Application object, you can pass in a backend parameter, optional values are win32 (default) and uia
- Win32 corresponding frameworks: MFC, VB6, VCL, simple WinForms controls and most old legacy applications
- Uia's corresponding frameworks: WinForms, WPF, store applications, Qt5, browser
If you can't know which framework the software you are testing belongs to, you can use Inspect (corresponding to Uia) and Spy++ (corresponding to win32) to see which one is displayed more fully. Inspect and Spy++ need to be installed by yourself
Social phenomenon analysis
pywinauto can easily automate Windows GUI through Application startup/connection of applications, WindowSpecification positioning controls, combined with keyboard and mouse operations. From simple input from Notepad to complex data entry in Excel, five major steps cover the entire scenario of testing, RPA and data processing. Master pywinauto, you can not only free your hands, but also unlock infinite automation possibilities. Whether it is a test engineer or an RPA developer, pywinauto is your weapon.
pywinauto is a star tool in the field of Windows automation. Gartner 2024 report shows that desktop application testing and RPA demand has increased by 35%, and pywinauto is highly favored for open source and ease of use. Its flexibility in GUI testing is believed to "fill in the windows automation gap".
The number of Stars in open source communities (such as pywinauto GitHub) exceeds 15,000, reflecting the enthusiasm of developers. Among enterprises, pywinauto is widely used in ERP, CRM and financial system automation. For example, a bank uses pywinauto to automatically enter transactions, which improves efficiency by 80%. AI-driven GUI testing tools are also on the rise, but pywinauto's lightweight and Python ecosystem advantages still make it mainstream.
Summarize
In traditional testing scenarios, web automation such as Selenium is very mature, but for a large number of desktop applications that still exist, many companies still rely on manual testing, which is inefficient and expensive. The rise of pywinauto is filling this automation blind spot, helping test engineers fully control the desktop testing process.
pywinauto is not a dark magic, but a handy tool. As long as you are familiar with the basics of Python, you can easily get started. It allows testing not only to web pages and interfaces, but also extends to the desktop, truly realizing end-to-end automation closed loop.
The above is the detailed explanation of Python Pywinauto's easy implementation of Windows desktop automation. For more information about Python Pywinauto automation, please follow my other related articles!