Using Squish as a module in other Python scripts, applications

Last edited on

Overview

It is possible to automate user interfaces using the Squish API from existing Python scripts using a special Python module called squishtest.

Obtaining a Squish package with the correct Python configuration

The squishtest module functionality is available free of charge and is included in all Squish packages.

However, if you are using a Python installation with a different point-release (or architecture) from what is included in the Squish package, the Python module will not be compatible with your Python installation. In this case, you need a Squish package custom built with a Python version that matches your Python installation.

To provide such a package we need the following from you:

To determine the exact version and information about UCS-2 or UCS-4 please send us the output generated by running the following command:

python -c "import sys; print sys.version; print sys.maxunicode"
Execute in a shell

Using the squishtest module in Python scripts

A simple Python script using squishtest might look like this:

import squishtest

import names

# Must set a test result:
squishtest.setTestResult("xml3", "testresultdir")

# Must specify which GUI toolkit support Squish
# should use for this application.
# 
# This depends on the Squish edition; possible values:
#
#  Android, iOS, Java, Mac, Qt, Tk, Web, Windows
squishtest.testSettings.setWrappersForApplication("addressbook", "Qt")

# Launch the application:
squishtest.startApplication("addressbook")

# Start interacting with the application...
squishtest.mouseClick(squishtest.waitForObject(...))
myscript.py

Setup

The above example test script can be executed with a plain Python interpreter, as long as the environment is configured correctly for finding the squishtest module, its shared library dependencies and a few other items, as shown below:

# Path to Squish package:
export SQUISH_DIR=/home/myuser/squish-6.5.2

# To find the "squishtest" module:
export PYTHONPATH=$SQUISH_DIR/lib:$PYTHONPATH

# To find shared libraries squishtest depends on:
export LD_LIBRARY_PATH=$SQUISH_DIR/lib:$LD_LIBRARY_PATH

# To find these Squish modules:
#  objectmaphelper (possibly used in names.py)
#  remotesystem
#  screen
#  toplevelwindow
export PYTHONPATH=$SQUISH_DIR/lib/python:$PYTHONPATH

# Launch squishserver in background:
"$SQUISH_DIR/bin/squishserver" --verbose &

# Execute Python script that uses squishtest:
python myscript.py
On Unix: Execute in a shell/terminal
:: Path to Squish package:
set SQUISH_DIR=C:\Users\myuser\squish-6.5.2

:: To find the "squishtest" module and shared libraries
:: it depends on:
set PYTHONPATH=%SQUISH_DIR%\bin;%PYTHONPATH%

:: To find these Squish modules:
::  objectmaphelper (possibly used in names.py)
::  remotesystem
::  screen
::  toplevelwindow
set PYTHONPATH=%SQUISH_DIR%\lib\python;%PYTHONPATH%

:: Launch squishserver in background:
start "" "%SQUISH_DIR%\bin\squishserver" --verbose

:: Execute Python script that uses squishtest:
python myscript.py
On Windows: Execute in cmd.exe

Functions provided by the squishtest module

setDebugFlags(debugLog_flags_string)

This functions allows configuring the verbosity of the ‘squishserver’ process’ diagnostic and status output. (Also see squishrunner –debugLog .)

Status: Optional

Default: Not set

Example:

# Execute this before startApplication()
# and attachToApplication():
squishtest.setDebugFlags("alpw")

setHookSubprocesses(boolean)

This is the equivalent to enabling sub-process hooking in the settings of a test suite (in the Squish IDE in test suite settings > Hook into sub-processes launched by the application (in the tab AUT).

Status: Optional

Default: True

Example:

squishtest.setHookSubprocesses(True)

setResultDir(result_dir_string)

Defines the output directory for fail images, etc. to use for certain report generators.

Status: Optional

Default: Not set

Example:

squishtest.setResultDir("C:\\Users\\myuser\\squish_results_dir")

setTestResult(report_generator_name, result_dir_string)

Status: Mandatory

Default: Not set

Example:

squishtest.setTestResult("xml3", "C:\\Users\\myuser\\squish_xml_results")