Squish & Python

Squish & Python

Squish is shipped with different scripting languages allowing to start recording or writing tests cases in any of them without lengthily setup. The list is: Ruby, JavaScript, Perl, Tcl and the one of interest in this article Python. The intent of this article is to provide information that might help new users of Squish to come up with more advance test cases, be it via code sharing or use of module/packages.


Did you know …

you can extend the Python installation used by Squish to make more script modules available to your test scripts

During evaluation or for simple test scenario it is common that test scripts are contained in their own file. But once the requirements for testing start to get more complicated it can happens that two or more test scripts have some common code, this is usually handled by putting the shared code into a single file that is imported when needed. There is two ways to import script modules in your main test script: source() and Python’s import.

  • Python’s import:
    Importing a module in a test script is the same as importing a module in Python, as long as the PYTHONPATH contain the folder(s) where your modules are stored then you can import them. Extending the PYTHONPATH can be done before starting either the IDE/squishrunner/server or in the test script itself, something along the lines of:

    import sys
    import MyImportantModule
    import AnotherModule

    This is the preferred method as this enable some functionality like code completion in the IDE. You can find it in our manual here

    Note: If MyImportantModule use some Squish function, it will have to import the necessary Squish’s modules. You can find more information about it here.

  • source():
    One of the function available in a test script is source(), this function is equivalent to an include, i.e it pastes the content of the sourced file where the source() call is made. Assuming that MyImportantModule look like:

    def myFunction():
        test.log("Hello world ! (from MyImportantModule)")

    Then our test script would look like:

    source(findFile("scripts", "MyImportantModule.py"))
    # will be replace by the content of the file, i.e:
    #def myFunction():
    #    test.log("Hello world ! (from MyImportantModule)")
    import AnotherModule
    def main():

you can change the Python installation which Squish uses

While writing your own module can be helpful at times, it is also known that Python have numerous modules/packages ready to be used. At this point it is worth noting that the Python shipped with Squish is stripped from some modules and pip. So in order to have access to the vast number of modules, we have explained the few steps to follow in order to use a different Python than the one we ship. You can find theses in the following knowledge base article: https://kb.froglogic.com/display/KB/Changing+the+Python+installation+used+by+Squish+binary+packages

you can automate applications using the Squish script API in Python scripts executed via a standard Python interpreter

One reason, among others, when this can prove useful: some scripts are used to automate/test your application and you would like to augment them with different Squish functions/features. An explanation on how to achieve it is already available in our knowledge base: https://kb.froglogic.com/display/KB/Using+Squish+as+a+module+in+other+Python+scripts%2C+applications


Leave a reply

Your email address will not be published. Required fields are marked *