<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qttestlib-manual.qdoc --> <title>Chapter 3: Simulating GUI Events | Qt Test 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qttest-index.html">Qt Test</a></td><td >Chapter 3: Simulating GUI Events</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qttest-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <link rel="prev" href="qttestlib-tutorial2-example.html" /> <link rel="next" href="qttestlib-tutorial4-example.html" /> <p class="naviNextPrevious headerNavi"> <a class="prevPage" href="qttestlib-tutorial2-example.html">Chapter 2</a> <span class="naviSeparator"> ◦ </span> <a class="nextPage" href="qttestlib-tutorial4-example.html">Chapter 4</a> </p><p/> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#writing-a-gui-test">Writing a GUI Test</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Chapter 3: Simulating GUI Events</h1> <span class="subtitle"></span> <!-- $$$tutorial3-brief --> <p>Howe to simulate GUI events.</p> <!-- @@@tutorial3 --> <!-- $$$tutorial3-description --> <div class="descr"> <a name="details"></a> <p>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.</p> <p>In this chapter we will see how to write a simple GUI test.</p> <a name="writing-a-gui-test"></a> <h2 id="writing-a-gui-test">Writing a GUI Test</h2> <p>This time, let's assume you want to test the behavior of our <a href="../qtwidgets/qlineedit.html">QLineEdit</a> class. As before, you will need a class that contains your test function:</p> <pre class="cpp"> <span class="preprocessor">#include <QtWidgets></span> <span class="preprocessor">#include <QtTest/QtTest></span> <span class="keyword">class</span> TestGui: <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span> { Q_OBJECT <span class="keyword">private</span> <span class="keyword">slots</span>: <span class="type">void</span> testGui(); }; </pre> <p>The only difference is that you need to include the Qt GUI class definitions in addition to the <a href="qtest.html">QTest</a> namespace.</p> <pre class="cpp"> <span class="type">void</span> TestGui<span class="operator">::</span>testGui() { <span class="type"><a href="../qtwidgets/qlineedit.html">QLineEdit</a></span> lineEdit; <span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>keyClicks(<span class="operator">&</span>lineEdit<span class="operator">,</span> <span class="string">"hello world"</span>); QCOMPARE(lineEdit<span class="operator">.</span>text()<span class="operator">,</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">"hello world"</span>)); } </pre> <p>In the implementation of the test function we first create a <a href="../qtwidgets/qlineedit.html">QLineEdit</a>. Then we simulate writing "hello world" in the line edit using the <a href="qtest.html#keyClicks">QTest::keyClicks</a>() function.</p> <p><b>Note: </b>The widget must also be shown in order to correctly test keyboard shortcuts.</p><p><a href="qtest.html#keyClicks">QTest::keyClicks</a>() 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 <a href="qtest.html#keyClick">QTest::keyClick</a>(), <a href="qtest.html#keyPress">QTest::keyPress</a>(), <a href="qtest.html#keyRelease">QTest::keyRelease</a>(), <a href="qtest.html#mouseClick">QTest::mouseClick</a>(), <a href="qtest.html#mouseDClick">QTest::mouseDClick</a>(), <a href="qtest.html#mouseMove">QTest::mouseMove</a>(), <a href="qtest.html#mousePress">QTest::mousePress</a>() and <a href="qtest.html#mouseRelease">QTest::mouseRelease</a>() functions to simulate the associated GUI events.</p> <p>Finally, we use the <a href="qtest.html#QCOMPARE">QCOMPARE</a>() macro to check if the line edit's text is as expected.</p> <p>As before, to make our test case a stand-alone executable, the following two lines are needed:</p> <pre class="cpp"> QTEST_MAIN(TestGui) <span class="preprocessor">#include "testgui.moc"</span> </pre> <p>The <a href="qtest.html#QTEST_MAIN">QTEST_MAIN</a>() 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.</p> <p>Files:</p> <ul> <li><a href="qttestlib-tutorial3-testgui-cpp.html">tutorial3/testgui.cpp</a></li> <li><a href="qttestlib-tutorial3-tutorial3-pro.html">tutorial3/tutorial3.pro</a></li> </ul> </div> <!-- @@@tutorial3 --> <p class="naviNextPrevious footerNavi"> <a class="prevPage" href="qttestlib-tutorial2-example.html">Chapter 2</a> <span class="naviSeparator"> ◦ </span> <a class="nextPage" href="qttestlib-tutorial4-example.html">Chapter 4</a> </p> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>