IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Chapter 3: Simulating GUI Events

Howe to simulate GUI events.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Chapter 3: Simulating GUI Events▲

<Unknown command>contentspage{Qt Test Tutorial}{Contents}

Qt Test features some mechanisms to test graphical user interfaces. Instead of simulating native window system events, Qt Test sends internal Qt events. That means there are no side-effects on the machine the tests are running on.

In this chapter we will see how to write a simple GUI test.

Writing a GUI Test▲

This time, let's assume you want to test the behavior of our https:// class. As before, you will need a class that contains your test function:

 
Sélectionnez
#include &lt;QtWidgets&gt;
#include &lt;QtTest/QtTest&gt;

class TestGui: public QObject
{
    Q_OBJECT

private slots:
    void testGui();

};

The only difference is that you need to include the Qt GUI class definitions in addition to the https:// namespace.

 
Sélectionnez
void TestGui::testGui()
{
    QLineEdit lineEdit;

    QTest::keyClicks(&amp;lineEdit, "hello world");

    QCOMPARE(lineEdit.text(), QString("hello world"));
}

In the implementation of the test function we first create a https://. Then we simulate writing "hello world" in the line edit using the https://() function.

The widget must also be shown in order to correctly test keyboard shortcuts.

https://() simulates clicking a sequence of keys on a widget. Optionally, a keyboard modifier can be specified as well as a delay (in milliseconds) of the test after each key click. In a similar way, you can use the https://(), https://(), https://(), https://(), https://(), https://(), https://() and https://() functions to simulate the associated GUI events.

Finally, we use the https://() macro to check if the line edit's text is as expected.

As before, to make our test case a stand-alone executable, the following two lines are needed:

 
Sélectionnez
QTEST_MAIN(TestGui)
#include "testgui.moc"

The https://() macro expands to a simple main() method that runs all the test functions, and since both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt's introspection work.

List of Files▲

Files:

List of Files▲

  • tutorial3/testgui.cpp

  • tutorial3/tutorial3.pro

https://   https://

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.