Squish/Using Helper Objects: Difference between revisions
Alan Ezust (talk | contribs) |
|||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[Category | [[Category:Squish]] | ||
= Using Helper Objects = | = Using Helper Objects = | ||
When writing a Squish test, it is often helpful to provide a helper object in your application code to provide information your test can't easily access. A helper object, in this context, is a QObject that provides methods useful to Squish tests that is instantiated in the application for Squish to find. A helper object may be a good fit if you're trying to verify application state that can't easily be checked using native Qt and Squish functions. | When writing a Squish test, it is often helpful to provide a helper object in your application code to provide information your test can't easily access. A helper object, in this context, is a '''QObject''' or a '''QQuickItem''' (widgets or QML) that provides methods useful to Squish tests that is instantiated in the application for Squish to find. A helper object may be a good fit if you're trying to verify application state that can't easily be checked using native Qt and Squish functions. | ||
As a contrived example, it might be useful to provide a helper object to get the color of a model rendered in a QGLWidget instead of trying to use a screenshot verification. Ideally a helper object's methods should ''not'' have side effects, as an ideal Squish test should only be testing what a user can do with normal input. | As a contrived example, it might be useful to provide a helper object to get the color of a model rendered in a QGLWidget instead of trying to use a screenshot verification. Ideally a helper object's methods should ''not'' have side effects, as an ideal Squish test should only be testing what a user can do with normal input. | ||
== An Example C++ Helper Object == | ==An Example C++ Helper Object== | ||
The following class definition provides the method '''SquishHelper::help''', which Squish can invoke and get an integer result from. Note that '''SquishHelper::help''' is flagged as '''Q_INVOKABLE''', this is important! A method used by Squish must be accessible via Qt's metaobject system, which means methods that are either slots or '''Q_INVOKABLE'''. | The following class definition provides the method '''SquishHelper::help''', which Squish can invoke and get an integer result from. Note that '''SquishHelper::help''' is flagged as '''Q_INVOKABLE''', this is important! A method used by Squish must be accessible via Qt's metaobject system, which means methods that are either slots or '''Q_INVOKABLE'''. | ||
Line 13: | Line 13: | ||
<code>#include <QObject> | <code>#include <QObject> | ||
class SquishHelper : public QObject | class SquishHelper : public QObject { | ||
Q_OBJECT | |||
public: | public: | ||
SquishHelper(QObject* parent); | SquishHelper(QObject* parent); | ||
Line 22: | Line 22: | ||
};</code> | };</code> | ||
After defining the class, merely instantiate it somewhere and optionally give it a name if there may be multiple instances. Make sure that the QObject parent of the helper object is a GUI element Squish shows in its object tree for the application, such as the | After defining the class, merely instantiate it somewhere and optionally give it a name if there may be multiple instances. Make sure that the '''QObject''' parent of the helper object is a GUI element Squish shows in its object tree for the application, such as the '''QMainWindow'''. This ensures that Squish can successfully look up the object later. | ||
<code>… | <code>… | ||
SquishHelper* helper = new SquishHelper(this); | SquishHelper* helper = new SquishHelper(this); | ||
Line 28: | Line 28: | ||
…</code> | …</code> | ||
== Using a Helper Object From Squish == | ==For QML/QtQuick Applications== | ||
If you wish to place the helper object somewhere in a QML GUI, it is better to derive from '''QQuickItem'''. This way, you will also be able to see it in Squish's '''Application Objects''' View. | |||
==Using a Helper Object From Squish == | |||
With the above helper object, looking up and invoking methods on the object is trivial. For instance, to look up the object and check the result of help in Python, you could do: | With the above helper object, looking up and invoking methods on the object is trivial. For instance, to look up the object and check the result of help in Python, you could do: |
Latest revision as of 12:25, 13 August 2024
Using Helper Objects
When writing a Squish test, it is often helpful to provide a helper object in your application code to provide information your test can't easily access. A helper object, in this context, is a QObject or a QQuickItem (widgets or QML) that provides methods useful to Squish tests that is instantiated in the application for Squish to find. A helper object may be a good fit if you're trying to verify application state that can't easily be checked using native Qt and Squish functions.
As a contrived example, it might be useful to provide a helper object to get the color of a model rendered in a QGLWidget instead of trying to use a screenshot verification. Ideally a helper object's methods should not have side effects, as an ideal Squish test should only be testing what a user can do with normal input.
An Example C++ Helper Object
The following class definition provides the method SquishHelper::help, which Squish can invoke and get an integer result from. Note that SquishHelper::help is flagged as Q_INVOKABLE, this is important! A method used by Squish must be accessible via Qt's metaobject system, which means methods that are either slots or Q_INVOKABLE.
#include <QObject>
class SquishHelper : public QObject {
Q_OBJECT
public:
SquishHelper(QObject* parent);
~SquishHelper();
Q_INVOKABLE int help() const; //only offer a non-const method if you have a good reason
};
After defining the class, merely instantiate it somewhere and optionally give it a name if there may be multiple instances. Make sure that the QObject parent of the helper object is a GUI element Squish shows in its object tree for the application, such as the QMainWindow. This ensures that Squish can successfully look up the object later.
…
SquishHelper* helper = new SquishHelper(this);
helper->setName("Helper1");
…
For QML/QtQuick Applications
If you wish to place the helper object somewhere in a QML GUI, it is better to derive from QQuickItem. This way, you will also be able to see it in Squish's Application Objects View.
Using a Helper Object From Squish
With the above helper object, looking up and invoking methods on the object is trivial. For instance, to look up the object and check the result of help in Python, you could do:
…
helper = findObject("{name='Helper1' type='SquishHelper'}")
test.compare(helper.help(), 0)
…