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()
mymodule.py
from squish import *

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

def click_y():
    mouseClick(waitForObject(":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 - 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

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

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

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

Solution #2 - “Custom” import of current functions and members

Requirements:

import mymodule

def main():
    startApplication("my_application")
    mymodule.click_x()
    mymodule.click_y()
test.py
import squish_module_helper

def click_x():
    squish_module_helper.import_squish_symbols()
    mouseClick(waitForObject(":x"), 5, 5, Button.Button1)

def click_y():
    squish_module_helper.import_squish_symbols()
    mouseClick(waitForObject(":y"), 5, 5, Button.Button1)
mymodule.py

Solution #3 - “Custom” import of current functions and members via decorator

Requirements:

import mymodule

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

@squish_module_helper.import_squish_symbols
def click_x():
    mouseClick(waitForObject(":x"), 5, 5, Button.Button1)

@squish_module_helper.import_squish_symbols
def click_y():
    mouseClick(waitForObject(":y"), 5, 5, Button.Button1)
mymodule.py

Related information