Squish offers a C++ API which allows to do a tighter integration or to solve some specific issues. This chapter discusses the available C++ APIs.
Record hints allow an application to influence the Squish event recorder while a user records a test script. Using a record hint an application can insert comments or function calls into the test script at a given time.
For this purpose Squish provides a
RecordHint class which is defined in the file
recordhint.h in Squish's
include directory. The public API is implemented
inline in this file, so the application only needs to include this
file and not link against any additional library.
Let's assume we have an application which also defines a
function myfunc which we have also wrapped so a
test script can access this function. After the user clicks a certain
button in the application we want the test script to call
myfunc.
To do this, we add the following C++ code at the point where the button click is handled:
Squish::RecordHint comment( Squish::RecordHint::Comment, "Call myfunc" ); comment.send(); Squish::RecordHint callmyfunc( Squish::RecordHint::Function, "myfunc" ); callmyfunc.send();
Now when recording a script and clicking on the button, e.g. the following Python script will be generated:
def main():
...
clickButton("....")
# Call myfunc
myfunc()
This small example shows when and how to use record hints. The
recordhint.h file shows the complete available
API.
Usually, Squish hooks into the AUT without requiring any special preparations. However, in some cases (like on AIX) this is not possible due to technical limitations.
For these cases, you can use the built-in hook approach. This approach requires minimal modifications of your AUT:
Include the qtbuiltinhook.h header
file, which can be found in Squish's include
directory, in your application's code where
main() is defined or the
QApplication object is
constructed.
Call the function
Squish::installBuiltinHook() after you
constructed your QApplication
object.
Example:
#include <qapplication.h>
#include "qtbuiltinhook.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
Squish::installBuiltinHook();
...
return a.exec();
}
This is all the preparation you need to enable your program for testing on platforms where the preloading mechanism is not available. It does not hurt if you leave in this code on other platforms as well -- in this case, the function recognizes that nothing needs to be done.
The function Squish::installBuiltinHook()
is a lightweight function that is fairly safe to leave in the final
program.
The function Squish::installBuiltinHook()
does the following:
If the environment variable
SQUISH_PREFIX is not set, it returns
immediately.
Otherwise it tries to load the shared library
squishqtbuiltinhook in the lib
(or bin) directory in
SQUISH_PREFIX and tries to resolve the
squishqtbuiltinhook_init symbol in that library. If
either one fails, it returns without doing anything
further.
Finally, if everything worked so far it calls the
squishqtbuiltinhook_init() function. This function
is responsible for the hooking.
The Squish::installBuiltinHook() function
returns true if the hooking succeeded, i.e. if
the application is executed under Squish. Otherwise it returns
false.