{{announcement.body}}
{{announcement.title}}

Windows Apps GUI Test Automation Using PyWinAuto

DZone 's Guide to

Windows Apps GUI Test Automation Using PyWinAuto

In this article, we will understand how to create a test automation script for Windows application using simple Python Scripting.

· Performance Zone ·
Free Resource

Overview

In this article, we will understand how to create test automation script for Windows application using simple Python Scripting. We will walk through a scenario of automating a desktop application using PyWinAuto.

Scenario

Amy is an automation tester working XYZ project. Amy has knowledge in basic python scripting and frameworks. Amy is looking for opensource tool based on python to automate desktop applications.

Amy needs help in accomplishing the test automation of windows desktop applications using PyWinAuto.

Scenario Solution

Before we start helping Amy in completing her task of automating the desktop applications, let us try to deep dive and understand important concepts of the tool PyAutoWin.

What Is PyWinAuto?

PyWinAuto is combination of few python modules to automate Windows based GUI applications. This allows to interact with Windows apps using Mouse and Keyboard actions.

PyWinAuto supports the following Windows technologies:

  • Win32 controls through the Win32 API: MFC, VB6, VCL
  • MS UI Automation: WinForms, WPF, Qt5, browsers, store apps

Installation of PyWinAuto

We can install PyWinAuto using the Pip install command. Let us navigate to command Prompt and execute the pip install below command :
pip install -U pywinauto --user

pip install -U pywinauto --user


In order to check the version, use the command:

print(pywinauto.__version__)

print(pywinauto.__version__)

Steps to Be Followed

Steps by step process of automating desktop application using pywinauto:

Step 1: You can use either Python IDLE as an IDE or Eclipse as an IDE by adding the Pydev Plugin.

Step 2: Run the application which you want to automate

Step 3: Define the main application window

Step 4: Inspect the necessary GUI control elements ( Text fields , drop-down list, buttons etc.)

Step 5: Perform the relevant action on that GUI control element

Step 6: Analyze the results

Demo 1

Let us see the demo of sample desktop application i.e. opening a notepad, writing content and further saving the notepad.

Let us follow the step by step process using a demo. The sample application we are using here is Notepad.

Step 1: In this demo , we have used Python IDLE as IDE to write the program

Step 2: Launching the notepad application using the below command

notepad application command

Step 3: Define the main application window

main application window

Step 4: Inspect the necessary GUI control elements ( Textfields,drop-down list, buttons etc.)

Pywinauto supports the most common GUI control elements and in order to display all available control elements for a specified window, we need to call the print_control_identifiers method.

print_control_identifiers

Step 5: Perform the relevant action on that GUI control element

Upon executing the above command, the following control identifiers are displayed

Now let us perform some action i.e write content in the notepad and save it.

save content

Save the notepad file with name

save_win

Demo 2

Let us create program on a different windows application i.e Calculator and let us use Eclipse IDE for this scenario.

Step 1: In this demo , we have used Eclipse as IDE to write the program

Step 2: Install Pydev plugin in Eclipse. In Eclipse Navigate to Help -> Marketplace-> search for Pydev

Step 3: Click on install button

PyDev

Step 4: After installing Pydev Plugin , we can start writing python programs in Eclipse IDE.

Step 5: As we have already installed PyAutoWin, the python module imports the required libraries. Below is the python script to invoke calculator application and perform addition operation on it.

@author

Finding Elements With Pywinauto

In order to inspect elements, we can either use Inspect tool or UISpy tool. In this above demo we have used the control identifiers provided by PyAutoWin built-in function.

Pywinauto offers a function called "print_control_identifiers". This will print all the information about the object that we wish to "inspect".

Below is the snapshot of the control identifiers:

control identifiers

Attributes Within Application

There are many Attributes available which can be used to inspect the different objects within the application.

Few of them are listed below:

  • class_name: Elements with this window class
  • class_name_re: Elements whose class matches this regular expression
  • parent: Elements that are children of this
  • process: Elements running in this process
  • title: Elements with this text
  • title_re: Elements whose text matches this regular expression
  • top_level_only: Top level elements only (default=**True**)
  • visible_only: Visible elements only (default=**True**)
  • enabled_only: Enabled elements only (default=False)
  • best_match: Elements with a title similar to this
  • handle: The handle of the element to return
  • ctrl_index: The index of the child element to return
  • found_index: The index of the filtered-out child element to return
  • predicate_func: A user provided hook for a custom element validation
  • active_only: Active elements only (default=False)
  • control_id: Elements with this control id
  • control_type: Elements with this control type (string; for UIAutomation elements)
  • auto_id: Elements with this automation id (for UIAutomation elements)
  • framework_id: Elements with this framework id (for UIAutomation elements)
  • backend: Back-end name to use while searching (default=None means current active backend)

Conclusion

PyWinAuto which is a set of python modules can be used to automate the Microsoft Windows GUI. This also allows users to send Mouse and keyboard actions to windows dialogs.

PyWinAuto can be considered as one of the good choices when it comes to opensource tools for Windows desktop applications.

Topics:
open source, performance, python, pywinauto, test automation, testing, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}