▲ Top

Qt4 GUI Testing with Hooq :: Blog

Qt4 GUI Testing with Hooq

Skip to comments

One of the things I've had the pleasure of working with for Mendeley is Hooq; Hooq is a GPL/LGPL-licensed system for testing Qt4 GUI applications, on multiple platforms (Linux and Windows being where most of the focus has been at the moment). It's mostly GPL, but an LGPL library is loaded into applications being tested, at runtime.

With apologies in advance for the incredibly unfriendly UI at the moment (patches welcome :p), here's a lots-of-pictures demonstration on using it to test a trivial application.

After building the demo application and Hooq, and running hooq, you'll get a screen like the following:

Screenshot of a fresh Hooq installation

For organisation, Hooq uses the concept of a 'test set' - this can be one per application, tests for different areas of the same application, or whatever you choose. First, you need to create one - select "New Test Set..." from the file menu, and you'll get another ugly dialog:

Screenshot of Hooq's Test Set dialog

After filling it in and clicking OK, you'll be back to the main screen, except that 'Test set' will be filled in (screenshot); type in 'Foo' in the box at the bottom, then click 'Record' (screenshot). The demo application will then start - if you type in "Hello, World" you should see something like this:

QTextEdit with "Hello, World" in it

Click the close button in the window decorations, and the Hooq window will now have a test entry, with run and edit buttons:

Hooq main window with a test in it

After clicking on the edit button highlighted above, you'll get a script editor (Hooq uses QtScript for test scripts):

Hooq's script editor

If you then click in the margin on the line after "QTextEdit_0.sendText(string0)", there'll then be a breakpoint marker on that line:

Hooq script editor with a breakpoint set

If you then click the run button in the toolbar, the script will stop running at that line:

Hooq after triggering a breakpoint

At this point, if you click 'pick property' in the toolbar (new icons welcome), you can then click on any widget in the application being tested, and get a list of its Q_PROPERTYs:

Q_PROPERTY list

For this example, scroll down to "plainText", and click compare; this will insert a check into the JavaScript code in the editor:

Script editor with added compare()

Remove the breakpoint, and run it a few times; it should repeat your input and quit each time, without an error; however, if you change the "string0" variable at the top from "Hello, World" to something else, like "Hello, World!", then click run, the test will error, if you don't also change the compare statement:

Thrown exception in Hooq

This is more useful when the test hasn't changed, but when the application itself has (i.e. a regression). Hooq also has a "Run All" feature, to run all the tests in a set unattended, and show a summary at the end.

As some more detailed information:

Hooq loads a shared library into the application being tested (via gdb under Linux, and a very ugly hack under Windows) which recieves every QEvent in the application, and sends user input events, encoded in XML, to a hooq client (the GUI client is shown above, but there's also a CLI one that just records and plays the XML data). In the GUI client, the XML is converted into a tree of sorts, and then a few modifications are optionally made (selectable via the "Options" menu):

Hooq options menu

These options are:

Ignore Mouse Movements
Don't create JavaScript code for mouse events, except where they end up creating drag and drop events.
Simplify Mouse Movements
Only available if the above is off; instead of creating code for every single 1-pixel mouse movement made, create simplified code, like "move in a straight line from (x1, y1) to (x2, y2) over 300 milliseconds"
Simplify Strings
Repeated keyPress/keyRelease events are composed into "sendText" events, which take a string parameter. Like the two above options, this greatly decreases the length, and increases the readability of the generated code.
Use Variables For Strings
Any string used by sendText is stored in a JavaScript variable, instead of being hardcoded every time it is used; this makes it easier to modify the script in the future.
Use Variables For Object References
This doesn't change the meaning of the code at all, but generally makes the code shorter.
Include Raw XML as Comment
Mainly useful for debugging; includes the raw XML code received over the socket as a JavaScript comment
Ignore Qt Internal Widgets
Qt in some circumstances (such as drag and drop under X11) automatically creates magic widgets; this option makes Hooq ignore them.

Comments

Re: Qt4 GUI Testing with Hooq

Posted at 2010-01-18 22:02:12 UTC by "Fred"

One other thing; the code is based on qtscriptgenerator - so, for example, you can easily access QSqlDatabase from javascript, to check that actions in the UI have given effects on the data store.

Re: Qt4 GUI Testing with Hooq

Posted at 2010-01-18 22:49:44 UTC by "Peter"

Great and important project! Ui testing is definitely missing in KDE's development process. Do you know the QtUiTest from canceled QtExtended? http://qtopia.net/doc/qtextended4.4/qtuitestmodule.html

Re: Qt4 GUI Testing with Hooq

Posted at 2010-01-19 00:03:35 UTC by "ruurd"

I know there is a commercial tool called Squish to do automated Qt UI testing, but this suits me better. BTW, thanks Fred for helping me out getting this to run on OpenSUSE 11.2.

Re: Qt4 GUI Testing with Hooq

Posted at 2010-01-19 08:50:30 UTC by "Aurélien Gâteau"

Looks very promising! Going to try it.

Re: Qt4 GUI Testing with Hooq

Posted at 2010-01-19 19:38:07 UTC by "Mihai Adrian"

Need a little help....The button Record doesn't enable if I write a test name. The Script is loaded: Loaded qt.core QtScript plugin Loaded qt.gui QtScript plugin When I first started the application, I didn't have QtScript installed, but now I have and I still can add new test name(The button Record doesn't enable) Do you have any idee ? Thank you

Re: Qt4 GUI Testing with Hooq

Posted at 2010-01-19 19:41:20 UTC by "Fred"

Have you added a test set, with a valid executable path? Hooq will enable the button if a test set is selected, a test name is entered, and a valid application path is entered for the test set.

Re: Qt4 GUI Testing with Hooq

Posted at 2010-01-20 09:53:08 UTC by "Mihai Adrian"

Thank you, the command for running aplication was wrong, I fixed, and it works now.

Re: Qt4 GUI Testing with Hooq

Posted at 2010-01-20 15:37:49 UTC by "Mihai Adrian"

Do you have any ideea how to click on a QToolButton ? What method do I call ? Where do I found any documentation ? Thank you

Re: Qt4 GUI Testing with Hooq

Posted at 2010-01-21 19:49:14 UTC by "Mihai Adrian"

Now I succed. All you have to do is record you movements and then play. Very greate application. Congratulations All the better

Re: Qt4 GUI Testing with Hooq

Posted at 2010-01-31 13:39:56 UTC by "skuda"

Hello, i am trying to make work the demo application, i have followed the steps written in the article but when i press Record the demo application it is not launched, the test it is directly added to the list, in the console where i have launched hooq a line it is added with "". If i use the edit button of the recently added test i get this "requireHooqScriptVersion(2);", if i use the run button inside the editor i get this from the console "void Interpreter::run()" i am using in Linux and i have installed qtscriptgenerator, what can be the problem? Apart from that question do i have any way to use hooq with pyqt applications? Thanks for the project.

Re: Qt4 GUI Testing with Hooq

Posted at 2010-02-04 00:13:25 UTC by "Fred"

I have no idea about pyqt, I'll look into it; could you email me what gets shown after running hooq with --spam?

Re: Qt4 GUI Testing with Hooq

Posted at 2010-02-18 18:40:04 UTC by "Mihai Adrian"

When testing arora with hooq, if the events from the aplication are to fast, it doesn't record them all. For example, if you type something in adress bar very fast, only a part of characters are captured by hooq. Any ideea how to fix this ?

Re: Qt4 GUI Testing with Hooq

Posted at 2010-02-20 11:10:31 UTC by "Fred"

What platform are you using, and are you using any virtualisation? I'm aware of an issue like that in windows under VirtualBox. As a temporary solution (which may have side effects), you can comment out "QCoreApplication::postEvent(this, new CanaryEvent(&m_ignoreEvents))" in injectedHooq/Logger.cpp

Re: Qt4 GUI Testing with Hooq

Posted at 2010-02-23 14:17:44 UTC by "Mihai Adrian"

Thank you, that works

New Comment

Drink made from wheat
Your name
Summary
Message