System tests generally consist of a series of actions and verification that the expected behaviour took place. When testing a Qt Extended application, this usually takes the form of simulating the input of specific text and verifying that the application subsequently displays the correct text. Testing necessarily includes navigating through forms and menus, activating buttons and comboboxes, and similar tasks. QtUiTest makes this very easy by supplying simple yet powerful navigation and input/output methods.
Basic Input
Using QtUitest, it is possible to simulate individual keyclicks, perhaps the most simple form of user interaction that can be simulated.
In the above example, the calls to keyClick() simulate a few individual key clicks, exactly as if the user had physically pressed keys on the device.
However, there are several disadvantages to this approach. Firstly, it is extremely verbose. Secondly, it means that the test will only work on a keypad device. To avoid these problems, the enter() function is provided:
On a keypad device, the above example has the same affect as the previous example, but is more concise. However, this test now has the additional advantage that it can work on a touchscreen device, as enter() knows how to simulate the necessary touchscreen events to input the given text.
Input Into Specific Widgets
In both of the examples above, input would be delivered to whichever widget is currently focused. In practice, what we often wish to do is enter text into a series of widgets displayed to the user.
For example, consider the following screen for entering the details of a new contact.
It would be possible to enter text in each field on this screen by using enter() and keyClick(Qt.Key_Down). However, the test would easily break if the order of fields were changed, or if fields were added or removed, and the test would not work on a touchscreen device.
To solve this problem, enter() (and many other functions in QtUiTest) take an optional Query Path parameter, which specifies which widget to operate on. The most common usage of query paths takes the form "LabelText", which refers to any widget which can receive input and is named or labeled "LabelText".
Entering text into a few fields on the screen featured above is achieved by the following example:
In the above example, if any of the specified fields are moved, the test will continue to work as expected. If any of the specified fields are removed, renamed, or no longer visible, the test will fail with a message indicating it cannot find the specified field.
Selecting Items from Lists and Menus, and Activating Buttons
Often, a user will be presented with a list of items or a context menu. QtUiTest provides a simple way to navigate to and select items in any list- or menu-like widget. The select() function will select an item from a list, combo box, menu or tab widget by looking at the display text for each item. It can also be used to activate a button with the given text.
mytestfunction: function() {
// Select "Show contacts" from the options (context) menu.
select("Show contacts", optionsMenu());
// Select "Bob" in the currently shown list.
select("Bob");
// Activate the "Edit" button to edit Bob's details.
select("Edit");
// Fill in the "Gender" field (a combo box) for Bob.
select("Male", "Gender");
}
select() allows a test to be reused by keypad and touchscreen devices. For instance, consider select("Edit") from the above example. On a touchscreen device, this will simply result in a click being simulated at the co-ordinates of the Edit button. On a keypad device, keyclicks will be simulated to navigate to and activate the Edit button.
Output
Built-in Qt and Qt Extended widgets can be queried for their currently displayed text. The getText() function is the most common way of doing this.
The above example simply enters "Yoyo Corporation" into the "Company" field, then verifies that the field actually contains the text "Yoyo Corporation". Note that it is not necessary to explicitly call compare immediately after the enter command: it would have failed if the enter text had failed.
The next chapter explains how to make use of data driven testing.
Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.
Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).
Qt dans le magazine
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter
ou par MP !