In the last article from the WebDriver Series, I showed you how you could automate Windows desktop apps through WinAppDriver. A framework developed by Microsoft that utilizes the WebDriver Wire Protocol and uses the same client-server architecture as Selenium WebDriver. In this post, I am going to show you how to make your tests more readable and maintainable through the integration of the popular among the Selenium folks- Page Object Design Pattern. Since I couldn't find any official documentation or articles about the usage of the Selenium built-in page objects' mechanism, I am going to show you my improved version of it.

Page Object Design Pattern WinAppDriver

You can read my previous article (Automate Windows Desktop Apps with WebDriver- WinAppDriver) to learn how to create your first desktop application's tests. As you may know, the page objects are designed to represent the UI elements of the web pages in more concise and readable way. There you can find all items' locators so that you can update them in a single location. Also, you can define common actions so that you don't copy paste code. You can check my articles about the subject in the Design Pattern Series.

Two years ago while we were working on the first version of the BELLATRIX test automation framework, I did this research so that we can have similar API for using page objects for all technologies- web, Android, iOS, and desktop.

In the following example, again I am going automate the Windows Desktop Calculator shipped with Windows 10.

Also, I am not sure that the suffix Page is correct in the context of desktop applications. So, I named my page objects- Views. Probably, we can use the words component or window as well. These classes are placed under the folder Views. Moreover, once again we are going to use partial classes to separate the different components of the page object- actions, assertions, elements. During the build process, there are combined in a single type, so we need to give the three files different names.

StandardView.Elements

Here we use the new lambda syntax for defining properties in C#. You can see how readable the code is in only 20 lines of code.

StandardView.Actions

In the StandardView.Actions file, we define a single public method that takes two integer numbers and the operation that we want to perform as a character. We have two separate private methods. The first is responsible for clicking the correct digit based on the int parameter. The second one does the same but for the operators (+, -, /, *). We have one more private method that converts the text result to decimal which we later use in the assert method.

StandardView.Assertions

Instead of repeating these three lines of code every time, we move them here.

WinAppDriver Page Objects in Tests

Below you can find the happy path tests for the Windows Calculator. The first example doesn't use page objects, and it is much more verbose. The second example uses page objects which makes it much more readable.

Calculator Tests without Page Objects

Calculator Tests with Page Objects