How to automate GUI application ? (GUI automation I)

World of tanks

Is there something more diffcult to automate than functional system testing of GUI based application which is not web based one? There are few things I guess, for sure this would be system integration for GUI based applications which interact with each other. Anyway most of automation tasks are easier: command line interface, web service interface, web interface. For all these interfaces we can automaticaly interact with the system instantly; JMeter, Selenium can help with web service and web, it is very easy to programmatically read CLI as well.I faced the problem of GUI system testing automation when I once jumped into the project when there was an application with client side created in Java running on Windows. At first I reached for Quick Test Professional because it was available in the company instantly by a mean of a floating licence. I heard much about Mercury/HP product so I was sure this is good choice.My impressions? Well… would you like to go for a weekend trip with a tank? You will get to the destination point producing incredible noise and will consume like 300 litres of fuel. It will be hot inside and no room for more than 3 people I suppose. It will be extremely expensive ride. But you will eventually get there. And believe it or not, people do it – they drive tanks every day! Still, one advantage is clear: it can do most of the tasks you can think of in automation testing area, this is really universal vehicle. I almost forgot: you also need to be Visual Basic fan. You are, aren’t you ?

Go flexible

I just thought something more suitable has to exists and came across Sikuli. It looked promising at the beginning and after few months I still couldn’t find any significant disadvantages that would force me to change the tool to other one. It is working and producing results. The conclusion is, it is worth at least to try!

Actually there are 2 versions of Sikuli which are developed independently of each other: http://www.sikulix.com/ (http://www.sikuli.org/)  and http://slides.sikuli.org/  (present in Maven repository). When I refer to Sikuli I always means the first one.

The architecture of the solution looks like this. As you can see you can both use Jython and Java to write scripts. However the main effort of the page is focused on Jython API. I discourage you from staying with this API. There is also bundled editor which allows you to rapidly see how it works. Again, do not use this for extended period of time unless you just check image recognition (it is really good for the latter if one needs to adjust the pattern similarity). Try the editor, write simple Jython scripts and leave it. This is really a flexible solution but in my opinion you have to switch to Java.

Jython vs Java with Sikuli

Why is that, I discourage from using Jython? It is a nice object oriented scripting language interpreted to byte code which is run on Java virtual machine. Well, here we go: “interpreted”. Every single run of the test requires interpretation. And it takes time which we would like to use for something more useful than waiting. Let alone my regression suite I was using at work was starting nearly 20 seconds! One can also say Jython is more readable than Java. I can agree it is cleaner but there is a price you pay which in my opinion is just too high: you loose possibility to integrate seamlessly with your Java project (if this is your technology), you will make developers and managers be more reluctant to start using the solution, you will have problems when trying to use some BDD framework on top of it. When your test become more complicated you will have to reach out to Java libraries anyway like I had to do when I was writing test case which included activities over ssh. There are minor issues with Jython as well like for example running Sikuli on secondary display while in Java it works perfect. Anyway, if you know Jython only and have Java nightmares stick to Jython, otherwise go for Java. The flexibility which comes along is awesome in my opinion.

Simple example

Let’s start with the simple example. This is fully functional example demonstrating the usage of Sikuli which will run if you have Sikuli and Notepad++ available on your computer. In this example Notepad++ is started, cursor is moved to the center of Notepad++ window and after 1 second pause text is written to the tab. I marked lines which are related to Sikuli libraries with yellow colour.

Diving into details

The main idea that drives Sikuli is this is image based concept. That is one needs many small screenshots of the application to let this testing framework run. Thus it operates more on user level of abstraction and it is closer to the human perception.

The other important point is it is not a “record-replay” tool. You do not record anything, you just write tests like you would write any regular application. By that mean you are test driven developmnent (TDD) and (behaviour driven development) BDD ready!
Let us look closer at how we can apply Sikuli to automate popular GUI Notepad++ application in Windows.
I am going to use version 6.8.3 in Windows 7 Home Premium version.

The main concept

The main concept can be illustrated by this drawing:

idea

The framework concept

The idea behind the framework is to be able to use meaningful “pieces” which use Sikuli in the background to simulate any action user can do in the application. They should have names close to english language, should present consistent grammar and possibly not have any parameters so that no free text is entered by the user.

 

As you can see it is very clear which user actions are simulated.

The main idea to use Sikuli is that when it starts, the framework should recognize the main window region and smaller regions so that images can be found quickly as well as to provide maintainability of the scope and context for actions in the code properly. Each region is handled by controller

Class overview

From the code point of view we should remember about reasonable design to allow easy development of the framework as well as its maintanance.

If you are interested in details please take a look yourself at the source code. I am sharing it here under GUI_automation_part1 branch.

Look and feel

Please watch the movie which shows the actual run. One can see red rectangles there which show the regions Sikuli is using.

Short summary

In the article I showed you how to simulate user actions on GUI which is not web based one. It was much about Java however this is the price we have to pay to have the exact testing framework we need. In the real project a help from developer could be required to make the framework correct from coding point of view.
Please note I was just showing the possibility of simulating user actions. I was calling the framework “testing framework” few times but I did not say anything about how to write automated test cases, nor about how to run them. This would be too much for the single article. Still I plan to continue the subject and come back to this in the near future. Stay tuned…