paint-brush
Introduction to Winforms UI Automation with Python & Appiumby@pjcast
4,763 reads
4,763 reads

Introduction to Winforms UI Automation with Python & Appium

by Phil CastanedaApril 10th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Microsoft announced some time ago that Coded UI automation was being deprecated. The recommended replacement for testing was Appium with WinAppDriver. Using python instead is that building your automation test just to run it is an unneeded step - you just check your scripts into your repository and checkout/run as an automated process. Python is a interpreted language with an awesome interactive shell - one can write & debug at the same time. Using C#, that is not possible out of the box (without writing a custom shell / interactive terminal)

Company Mentioned

Mention Thumbnail
featured image - Introduction to Winforms UI Automation with Python & Appium
Phil Castaneda HackerNoon profile picture

Microsoft announced some time ago that Coded UI automation was being deprecated - and that the recommended replacement for testing was Appium with WinAppDriver.

I plan to show how easy it is to get started automating with Appium for a basic DotNet Core application. We will be using python for reasons mentioned below.

Please clone the git repository from here to go along with this article.

Using Python?

Why use python - my app is in C# and Winforms (or WPF) and there are Nuget packages for connecting Appium... what gives? That's a valid point, and I wouldn't fault one for using C# directly for the tests. However, a strong argument for using python instead is that building your automation test just to run it is an unneeded step - you just check your scripts into your repository and checkout/run as an automated process.

Also, python is a interpreted language with an awesome interactive shell - one can write & debug at the same time. While there are ways to alter running .Net code during debugging, it is not very practical.

Being able to write a UI test, set a break point (e.g. with a python IDE, Visual Studio Code, etc), execute the script; and once the break point is hit, having the ability to run any additional python commands in the shell - UI Element lookup, an Assert, or some kind of debug dump - is priceless.

In fact, you could leave a shell up for an indefinite amount of time, keeping the Appium WebDriver instance in scope and develop a whole scenario, interactively. Using C#, that is not possible out of the box (without writing a custom shell / interactive terminal)...

Getting Started

You will need to be on Windows 10 and install (or already have) the following prerequisites:

  1. Turn on Win10 Dev Mode for WinAppDriver to function
  2. Python 3.7+ (3.8 also works fine)
  3. WinAppDriver
  4. Example C# Winform App and python example (above)

Getting sample code, and building the .Net Core Winform sample. dotnet run will simply launch the app to ensure it built. After the application starts, you can close it - WinAppDriver will start the app up during automation run.

git clone https://github.com/pjcast/apium_winform_samples.git
cd apium_winform_samples/winforms/basic/Basic
dotnet restore
dotnet build
dotnet run

Start Win App Driver in separate cmd prompt (leave running)


C:\Program Files (x86)\Windows Application Driver>WinAppDriver.exe
Windows Application Driver listening for requests at: http://127.0.0.1:4723/
Press ENTER to exit.

Run Python sample:

python basic_sample.py

You should see the sample application startup, tab around, check some UI state, then type some characters, and finally app exits.

Files in python\ folder:

  • basic.json - a JSON file container various locator methods for the UI elements. Provides structure of elements not hard coded in the python script itself. Also, can easily be reused by many python files.
  • appium_helpers.py - Loads JSON files, and wraps them into dynamic python accessor classes. Provides methods to get UI elements directly, or with a delay get_wait(). Often times, you need to wait for UI to become available; other times, you expect it is already there.
  • basic_sample.py - Creates connection to winappdriver/appium, and performs several UI actions.

The basic sample app and python script are just meant to be a very quick introduction of the power and extensibility afforded by python. While creating a very small framework to build off of, hopefully that will prove useful to you.

As time permits, will be looking to extend the examples and provide python wrappers for interacting with more complex Winform Controls (such as DataGridViews). Your UI tests should be as lean as possible, and build off of reusuable UI object repositories (JSON in this case), and reusable python UI wrappers. You don't want to have code to select X row, X column in a data grid in dozens of different python script files.

Win App Driver / Appium is definitely a welcome replacement for Coded UI. Appium is very accessible to anyone - free, open source, and with the support of Microsoft (via Win App Driver), definetly worth looking into. It is also fantastic that Microsoft open sourced their example automation tests and their UI Recorder app. Although, the recorder app needs some work, the source is there to improve.

The most important part of the Windows Appium integration (WinAppDriver) has yet to be open sourced... If Microsoft open sourced that, improvements on xpath query performance and other lacking features could be done by community members.