Automated Gen GUI Testing with Python
search cancel

Automated Gen GUI Testing with Python

book

Article ID: 264225

calendar_today

Updated On:

Products

Gen Gen - Run Time Distributed Gen - Workstation Toolset Gen - Host Encyclopedia

Issue/Introduction

Gen applications are meant to work as designed based upon the data model and the associated action diagram. To ensure that the code works the way the Gen Developer intended, we recommend testing to make sure the final product meets end-user requirements.

Resolution

Automating Gen™ GUI Testing with Python

Gen applications are meant to work as designed based on the data model and the associated action diagram. To ensure that the code works the way the Gen Developer intended, we recommend testing to make sure the final product meets end-user requirements.

When writing code in programming languages like C, Java, or Python, tools exist that can be used to create unit tests to guarantee that the individual pieces work as expected. For example, in Java, suppose you have a function called sum that adds two numbers together:

You could then write a unit test to assure that the function adds the two numbers correctly:

Gen generated code, however, does not have this sort of atomic functionality. The code can be generated in C, C#, Java, or COBOL; and the code can run on the mainframe, Linux, Unix, or Windows. As a result, it is very difficult to create a unit test structure for Gen applications.

The Gen development team has found a Python package that can be used to automate the testing of Gen models for Windows GUI applications and Cooperative applications with a Windows GUI client.

Pywinauto for GUI Applications

The Gen development team has successfully used Pywinauto, an Open Source package of Python modules, to automate the testing of Windows GUI applications (i.e. Windows or Cooperative packaging). Pywinauto is a package of Python modules that can interact with a Microsoft Windows GUI by allowing a program to send mouse and keyboard actions to the GUI. These interactions can be combined with Python’s TestCase class to allow assertions to be applied to the results.

As an example, let’s take a look at how one might create an automated test for the Gen Sample model using Pywinauto

This example shows how Pywinauto can be used to test Gen GUI-based applications.

The first statement initializes the backend process that will be used to drive the application. Pywinauto supports two different backends. Win32 can handle older Windows applications built using MFC and some WinForms. There is also a “uia” backend for MS UI automation, which supports newer Windows interface objects.

The next statement starts the Gen application. The start function executes like a Windows command line, so arguments can be added as needed. Once the Application object has started the GUI application, the test program has access to the windows and controls (or UI widgets) in the application.


                                                                Sample Model Main Window

After starting the application, the next step is getting access to the main window. Pywinauto has several ways to access windows and controls. In this case, the test is retrieving the window into a variable from a dictionary entry. This scheme is best when there are embedded spaces in the window name, as there are in the example.

An important part of any GUI testing is providing sufficient wait times between events. In the sample code, the test will wait for the main window to be ready for input or 5 seconds. If the window takes too long to get ready, the test will timeout rather than attempt to continue.

The Sample model uses menus to move between windows. Pywinauto’s menu_select function handles this menu selection. The arrow (->) represents a parent-to-child menu relationship. In the example, this is the first place the test code interacts with the model application. As the code sample shows, the menu selection causes a new window to appear and the test uses the dictionary approach to get the new window. The test waits for the new window to get ready, this time without a timeout.


                                                 Sample Model: Corporate Divisions Window

The test then makes a menu selection on the Corporate Divisions window. For the next several lines, where the test is entering data onto the screen, the test uses a different Pywinauto access method for the window and the fields. Python can resolve attributes dynamically, while Pywinauto provides a best-match capability for object names. This allows the test creator to access the windows and fields directly by their name rather than pulling them from the dictionary. The only requirement for using this method of access is that the name cannot contain whitespace or any character that would not be valid for a Python variable name.


                                            Sample Model: Division Details Window

Next, the test code clicks the OK button, which should cause the Sample model to store the new division in the database and close the Division Details window. The test waits 3 seconds for the process to complete and the Corporate Divisions window to become active again.


                                               Sample Model: Corporate Divisions after Add

Finally, the testing code can evaluate the results to make sure the model code is working correctly. In this case, the test is just evaluating that a record was successfully added; however, additional assertions could be used to make sure that the data retrieved from the database matches the data that was entered.

Once the tests are complete, the Python code shuts down the sample model executable and exits. Because the testing framework is based on the Python UnitTest class, the results can be handled by Python for reporting and indications about whether the tests have passed or failed.

Conclusion

While the nature of Gen makes unit testing of model actions impractical, testing packages exist that can help developers make sure the overall model behavior meets end-user expectations. Python with Pywinauto is one of those tools that has been used by the Gen development team to evaluate changes to the Gen runtimes with great success. We hope you will use it to test your Gen applications to increase your confidence that your models work the way you desire.