6.14. Python Notes

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. 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)

The Squish API functions that return strings always return strings of type Unicode (i.e., of type unicode); not 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:

Python
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.

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

Python
import __builtin__

def main():
    startApplication("aut")
    restore_python_bool_int_long()

def restore_python_bool_int_long():
    global bool
    global int
    global long
    bool = __builtin__.bool
    int = __builtin__.int
    long = __builtin__.long

Restoring object and type is advised against, because they are used used in snippets recorded by Squish.

6.14.4.1. 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.

To access Python's bool() function one can use __builtin__.bool() (which requires first importing the __builtin__ module, i.e. import __builtin__).

6.14.4.2. 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.

To access Python's int() function one can use __builtin__.int() (which requires first importing the __builtin__ module, i.e. import __builtin__).

6.14.4.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.

To access Python's long() function one can use __builtin__.long() (which requires first importing the __builtin__ module, i.e. import __builtin__).

6.14.4.4. 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.

To handle this one can create old-style classes or do import __builtin__ and inherit from __builtin__.object instead of object.

6.14.4.5. 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 as __builtin__.type() (requires first importing the __builtin__ module, i.e. import __builtin__) 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:

Python
import sys
sys.path.append("my/path")

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(waitForObject(":Some_Menu"), "File")

6.14.7. 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 prefers books, a good Python 2 book is Core Python Programming by Wesley Chun.