Using Squish functions in your own Python modules or packages

Last edited on

Overview

Here are several examples of how you can use your own Python modules in Squish.

The challenge with using the Squish functions and members in your own Python modules is this:

This means that with a test script and custom module like these...

import mymodule

def main():
    startApplication("my_application")
    mymodule.click_x()
    mymodule.click_y()
test.py
import names
from squish import *

def click_x():
    mouseClick(waitForObject(names.x), 5, 5, Button.Button1)

def click_y():
    mouseClick(waitForObject(names.y), 5, 5, Button.Button1)
mymodule.py

...the from import statement in the module mymodule will only import the functions and members that exist in the squish module at that point in time. Therefore the above may result in errors such as this:

NameError: global name 'Button' is not defined

Solution #1 - "Custom" import of current functions and members

Requirements:

import mymodule

def main():
    startApplication("my_application")
    mymodule.click_x()
    mymodule.click_y()
test.py
import names
def click_x():
    mouseClick(waitForObject(names.x), 5, 5, Button.Button1)

def click_y():
    mouseClick(waitForObject(names.y), 5, 5, Button.Button1)

# This must be done at the end of each module that
# should have automatic access to the Squish API:
import squish_module_helper
squish_module_helper.import_squish_symbols()
mymodule.py

Solution #2 - "Custom" import of current functions and members via decorator

Requirements:

import mymodule

def main():
    mymodule.click_x()
    mymodule.click_y()
test.py
import names
from squish_module_helper import import_squish_symbols_decorator

@import_squish_symbols_decorator
def click_x():
    mouseClick(waitForObject(names.x), 5, 5, Button.Button1)

@import_squish_symbols_decorator
def click_y():
    mouseClick(waitForObject(names.y), 5, 5, Button.Button1)
mymodule.py

Solution #3 - Access functions and members explicitly

import mymodule

def main():
    startApplication("my_application")
    mymodule.click_x()
    mymodule.click_y()
test.py
import test
import testData
import object
import objectMap
import squishinfo
import squish

import names

def click_x():
    squish.mouseClick(squish.waitForObject(names.x), 5, 5, squish.Button.Button1)

def click_y():
    squish.mouseClick(squish.waitForObject(names.y), 5, 5, squish.Button.Button1)
mymodule.py

Note the added prefix squish. in three places in click_x() as well as click_y().