Modularizing your recorded scripts for reuse (refactoring)

Last edited on

Overview

Typically a group of steps performed in one recorded test script are steps that one wants to use again in other test cases. There are several methods to turn recorded commands into reusable script pieces.

Several possibilities exist:

Variant 1 is impractical because it is hard to read and maintain.

Variants 3 is impractical because the order of execution for test cases is not clearly defined. (The order in the IDE does not necessarily represent the order in which the test cases are executed when executing the whole test suite.)

Variant 4 is impractical because there is no concept of grouping multiple test suites, requiring you to switch back and forth between test suites in the Squish IDE. Execution in one go is also not possible.

Suggested work-flow

What should work well for many cases is variant 2. To achieve this the following work-flow can be used.

Step 1: Create a test suite

Step 2: Create a test case in the test suite

Step 3: Record first actions in the test case

Step 4: End recording

At this point the script might look like this:

import names

def main():
    startApplication("myapp")
    mouseClick(names.Something)
test.py

Step 5: Move the code of that first step into a separate function and call it

import names

def main():
    startApplication("myapp")
    step1()

def step1():
    mouseClick(names.Something)
test.py

Step 6: Record the next step

To do this:

The possible result:

import names

def main():
    startApplication("myapp")
    step1()
    mouseClick(names.SomethingElse)
    snooze(1)

def step1():
    mouseClick(names.Something)
test.py

Step 7: Move the newly recorded snippet into a new function as well

import names

def main():
    startApplication("myapp")
    step1()
    step2()
    snooze(1)

def step1():
    mouseClick(names.Something)

def step2():
    mouseClick(names.SomethingElse)
test.py

Repeat the above for every step and move the resulting commands into separate functions. It is recommended to test the resulting script after every new function that you have created.

(This overall process of moving sensible pieces of code into functions for reuse in multiple test scripts or test cases, is called refactoring.)

Step 8: Create and rename a shared script for use across test cases

In Test Suite Resources expand on the toolbutton on the left and choose New Test Resource to create a new shared script:

Adjust the name of the newly created shared script as desired:

Step 9: Adjust test scripts to use to shared script

Insert a variant of from <modulename> import * or from <modulname> import <functions> into your test script to make the functions in the shared script available to it:

Note: For BDD tests using the imports inside of the "steps.*" file in the Test Suite Resources is recommended.