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:
- Squish functions and member are injected into the
squish
module at runtime after the test script has started/attached to an application.
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()
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)
...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:
- The file/module squish_module_helper.py must be available (i.e. in test script folder).
import mymodule
def main():
startApplication("my_application")
mymodule.click_x()
mymodule.click_y()
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()
Solution #2 - "Custom" import of current functions and members via decorator¶
Requirements:
- The file/module squish_module_helper.py must be available (i.e. in test script folder).
import mymodule
def main():
mymodule.click_x()
mymodule.click_y()
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)
Solution #3 - Access functions and members explicitly¶
import mymodule
def main():
startApplication("my_application")
mymodule.click_x()
mymodule.click_y()
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)
Note the added prefix squish.
in three places in click_x()
as well as click_y()
.
Related information¶
Using Squish Functions in Python Modules/Packages (Squish manual)
Python Notes (Squish manual)
Python's sys.path
Using Squish as a module in other Python scripts, applications