6.14. Python Notes

Table of Contents

6.14.1. Default File Encoding (UTF-8)
6.14.2. Default type of string return values (Unicode)
6.14.3. Squish's Python Modules
6.14.4. Python Symbols Hidden by Squish
6.14.5. Importing Custom Python Modules
6.14.6. Using Squish Functions in Python Modules/Packages
6.14.7. squishtest: Module for embedding Squish in Python Applications
6.14.8. Python Language Documentation

6.14.1. Default File Encoding (UTF-8)

Squish assumes that all test.py files are UTF-8 encoded.

If you expect to edit such files outside of the Squish IDE, we recommend putting the following line at the start of each test.py file:

# -*- coding: utf-8 -*-

This is purely a comment, but a Python-aware editor will notice it, and will correctly load and save using the UTF-8 encoding that Squish expects, as a result.

Other text editors (which do not recognize the above notation) typically must be configured to save the Squish test scripts using UTF-8 encoding explicitly.

6.14.2. Default type of string return values (Unicode)

For Python 2.x, Squish API functions that return strings always return strings of type unicode instead of str. So if you need to check the type, use isinstance(string, unicode) or isinstance(string, basestring).

6.14.3. Squish's Python Modules

Squish's Python-specific extension modules are loaded automatically by internally executing the equivalent of the following statements after attaching to or starting an AUT:

import test
import testData
import object
import objectMap
import squishinfo
from squish import *

The objects (functions, classes, etc.) exposed by the squish module depend on the currently hooked up application. If no application is hooked up (via startApplication or attachToApplication) most objects will be "missing" from this module until an application is hooked up.

This means that it is not necessary to import them yourself unless you are developing your own standalone Python module. See Using Squish Functions in Python Modules/Packages (Section 6.14.6) for information on using the above modules in your own Python modules or packages.

6.14.4. Python Symbols Hidden by Squish

The wildcard import (from squish import *) of the squish module means that some of Python's built-in objects are "hidden" by Squish objects that have the same names but completely different behavior.

The objects hidden by Squish are object, bool, int, long and type.

However, please note that this "hiding" only takes place for the test script itself (and only when executing test cases, not when using the squishtest module). This means that this "hiding" is not being done for standard or custom Python modules and packages.

Some of the original symbols can be restored after calling startApplication() or attachToApplication(). For example:


import sys
# Check if Python 3:
if sys.version_info[0] == 3:
  import builtins
  # Make Python 2 look like Python 3:
  import __builtin__ as builtins

def main():

def restore_python_bool_int_long():
    global bool
    global int
    global long
    global pytype
    bool = builtins.bool
    int = builtins.int
    long = builtins.long
    pytype = builtins.type

Restoring object and type is advised against, because they are used in snippets recorded by Squish. Squish's bool vs. Python's bool

Squish's bool represents the Squish type for a bool in the wrapper's C and C++ code.

Like the other hidden symbols, to access Python's bool() function, one can use it from the package __builtin__ (Python 2) or builtins (Python 3). Squish's int vs. Python's int

Squish's int represents the Squish type for an integer in the wrapper's C and C++ code.

Like the other hidden symbols, to access Python's int() function one can use it from the package __builtin__ (Python 2) or builtins (Python 3). Squish's long vs. Python's long

Squish's long represents the Squish type for a long in the wrapper's C and C++ code.

Like the other hidden symbols, to access Python's long() function one can use it from the package __builtin__ (Python 2) or builtins (Python 3). Squish's object vs. Python's object

Squish's object module has the same name as the base class of all Python 2 new-style classes, thus hiding Python's object symbol.

If you need to use Python 2's object symbol, you can access it from the package __builtin__.

In Python 3 there is no need to do anything since we don't explicitly inherit object, and it is inherited by default if no other class is specified. Squish's type() vs. Python's type()

The Squish type functions are used to simulate keyboard input by the user.

While Python's built-in type function is available from package __builtin__ in Python 2, or builtins in Python 3, it is better Python practice to use the isinstance function.

6.14.5. Importing Custom Python Modules

If you need to import custom modules that are not in sys.path you can make them available either by setting (or extending if already set) the PYTHONPATH environment variable with the path or paths to the module or modules you want to import, or you can extend sys.path at the start of your test scripts like this:

import sys

You can then import any module from my/path as normal, e.g., import mymodule.

[Note]Path Separators

Most scripting languages, including Python, understand Unix-style paths that use / as a path separator, even on Windows. However, if you really want to use Windows-style paths with the \ path separator, you must either escape them (e.g., "my\\path") or use a raw string (e.g., r"my\path").

6.14.6. Using Squish Functions in Python Modules/Packages

If you create a Python module or package that uses the functionality in the squish module, we recommend importing the squish module like this...

import squish

...and not like this (wildcard import variant):

from squish import *

This is because the objects (functions, members, classes) provided by the squish module depend on the currently hooked up application. So if no application is hooked up by the time from squish import * is being executed, many objects will not be available directly, but one needs to access them through the squish module.

Consequentially, the objects (functions, members, classes) of the squish module must then be accessed through the squish module:

import squish

def do_something():
    squish.activateItem(squish.waitForObject(":Some_Menu"), "File")

6.14.7.  squishtest: Module for embedding Squish in Python Applications

Squish tests in Python can be executed in 3 ways:

  1. From the IDE

  2. From squishrunner command line

  3. From a Python main program that uses squishtest module.

The squishtest Python module attempts to duplicate the functionality of squishrunner by offering the complete Squish API (Section 6.3). Members are injected into this module, just as they are normally injected into the global namespace of Squish test suites. Similarly, after the call to startApplication or attachToApplication, the appropriate edition-specific APIs (e.g. Qt, Mac, Windows, Web, Java) are injected into the module dynamically.

Detailed setup steps, as well as an example Python script, can be found in this knowledgebase article.

6.14.8. Python Language Documentation

In addition to the Squish extension API, the full set of Python language features and modules is available for scripting. The Python Documentation page has links to the Python documentation for the current and older versions. If you prefer books, a good Python 2 book is Core Python Programming by Wesley Chun, and a good Python 3 book is Programming in Python 3 by Mark Summerfield. -->