Squish/Using Custom Properties: Difference between revisions
No edit summary |
m (style) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
=Using Squish and custom properties= | [[Category:Squish]] | ||
= Using Squish and custom properties = | |||
To fully test your application, there might be situations where you need to modify the application source code to add properties to specific widgets that can be tested from Squish. There are essentially two ways of doing this: | To fully test your application, there might be situations where you need to modify the application source code to add properties to specific widgets that can be tested from Squish. There are essentially two ways of doing this: | ||
==1) Use the Q_PROPERTY macro== | == 1) Use the Q_PROPERTY macro == | ||
If you have access to the source code for the widget you want to change, you can simply use the Q_PROPERTY macro to add a property to the widget:< | If you have access to the source code for the widget you want to change, you can simply use the Q_PROPERTY macro to add a property to the widget: | ||
<code>class MyWidget : public QWidget | |||
{ | |||
Q_OBJECT | |||
Q_PROPERTY(QString nameOfProperty READ getValue) | |||
public: | |||
… | |||
QString getValue() const { return m_memberVar; } | |||
… | |||
private: | |||
QString m_memberVar; | |||
}</code> | |||
This property will then be accessible in Squish using the Squish Spy just like any other widget property, and turned into a verification point simply by recording: | This property will then be accessible in Squish using the Squish Spy just like any other widget property, and turned into a verification point simply by recording: | ||
==2) Use dynamic properties== | <code>waitFor("object.exists(':NameOfObject')", 20000) | ||
object = findObject(":NameOfObject") | |||
value = object.nameOfProperty | |||
test.compare(value, "Expected value")</code> | |||
== 2) Use dynamic properties == | |||
In the case where you do not have access to the source code for the widget, but instead only to the application using it, you cannot use the Q_PROPERTY approach as described above, but will need to use dynamic properties instead. This is essentially using the QObject::setProperty() function in the application source code to set the value of the property on the widget. This function takes two parameters, the name of the property (which you will later use from Squish) and the value to set: | In the case where you do not have access to the source code for the widget, but instead only to the application using it, you cannot use the Q_PROPERTY approach as described above, but will need to use dynamic properties instead. This is essentially using the QObject::setProperty() function in the application source code to set the value of the property on the widget. This function takes two parameters, the name of the property (which you will later use from Squish) and the value to set: | ||
<code> | |||
myWidget->setProperty("nameOfProperty", "Expected value" | |||
</code> | |||
The dynamic properties are however not seen automatically by Squish, so you cannot access them in the Squish Spy. Instead, we need to call the QObject::property() function to get the value. This is nothing different from calling other Qt functions from your Squish scripts: | The dynamic properties are however not seen automatically by Squish, so you cannot access them in the Squish Spy. Instead, we need to call the QObject::property() function to get the value. This is nothing different from calling other Qt functions from your Squish scripts: | ||
= | <code> | ||
waitFor("object.exists(':NameOfObject')", 20000) | |||
object = findObject(":NameOfObject") | |||
value = object.property("nameOfProperty").toString() | |||
test.compare(value, "Expected value") | |||
</code> |
Latest revision as of 10:00, 11 August 2020
Using Squish and custom properties
To fully test your application, there might be situations where you need to modify the application source code to add properties to specific widgets that can be tested from Squish. There are essentially two ways of doing this:
1) Use the Q_PROPERTY macro
If you have access to the source code for the widget you want to change, you can simply use the Q_PROPERTY macro to add a property to the widget:
class MyWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(QString nameOfProperty READ getValue)
public:
…
QString getValue() const { return m_memberVar; }
…
private:
QString m_memberVar;
}
This property will then be accessible in Squish using the Squish Spy just like any other widget property, and turned into a verification point simply by recording:
waitFor("object.exists(':NameOfObject')", 20000)
object = findObject(":NameOfObject")
value = object.nameOfProperty
test.compare(value, "Expected value")
2) Use dynamic properties
In the case where you do not have access to the source code for the widget, but instead only to the application using it, you cannot use the Q_PROPERTY approach as described above, but will need to use dynamic properties instead. This is essentially using the QObject::setProperty() function in the application source code to set the value of the property on the widget. This function takes two parameters, the name of the property (which you will later use from Squish) and the value to set:
myWidget->setProperty("nameOfProperty", "Expected value"
The dynamic properties are however not seen automatically by Squish, so you cannot access them in the Squish Spy. Instead, we need to call the QObject::property() function to get the value. This is nothing different from calling other Qt functions from your Squish scripts:
waitFor("object.exists(':NameOfObject')", 20000)
object = findObject(":NameOfObject")
value = object.property("nameOfProperty").toString()
test.compare(value, "Expected value")