Article - Modularizing your recorded scripts for reuse (refactoring)

Skip to end of metadata
Go to start of metadata

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: Record everything into a single test suite, with a single test case, in a single function containing the commands for steps 1 to 5 (main()).
  • Variant 2: Record everything into a single test suite containing single test cases and multiple functions (each function represents and contains the commands for its step).
  • Variant 3: Record everything into a single test suite, with multiple test cases, each representing one of the steps.
  • Variant 4: Recording everything into multiple test suites, each test suite representing one of the steps.

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:

test.py

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

test.py

Step 6: Record the next step

To do this:

  • Add the snooze() command at the bottom of the main() function.
  • Set a breakpoint on this new line.
  • Execute to the breakpoint.
  • Start recording a snippet ("Record Snippet").
  • Perform the next step.
  • End the recording.
  • End test case/script execution (so that Squish inserts the newly recorded snippet).

The possible result:

test.py

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

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 source(findFile(...)) into your test script to make the functions in the shared script available to it:

Note: For BDD tests using source(findFile(...)) inside of the "steps.*" file in the Test Suite Resources is generally recommended.

Related Information

Labels:
function function Delete
refactor refactor Delete
optimize optimize Delete
enhance enhance Delete
scripting scripting Delete
robust robust Delete
share share Delete
improve improve Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.