A simple way to get started with testing your application with Squish is to record interactions with your application which correspond to the functionality you want to exercise. Once the recording is complete, Squish generates a test script in your preferred scripting language, containing those actions you performed on your Application Under Test (AUT). For every object with which you interact, Squish generates a property-based object name that is associated with a symbolic name. The test script displays the symbolic names, while the object names are stored in a repository referred to as the ‘Object Map’. Upon replay of your recorded session, the exact actions you recorded are performed on your AUT.
It’s easy to imagine that, with longer recording sessions, your test script could end up becoming a large wall of script code, eventually leading to difficulties in reading and maintaining the script. To overcome these issues, you may want to refactor your code. That is, you will want to create functions where necessary and perhaps add more meaningful object names and variables in the recorded script.
What is Refactoring?
Refactoring is a code improvement method that restructures your code such that it becomes cleaner and simpler, but without changing the external functionality of your test case. It allows for improved readability and easier maintenance of your script. Refactoring is best done as a step by step process and should ideally be done on tested code so that nothing breaks. It is possible that upon refactoring, some bugs might become evident. For example, when a large complex function is split into smaller, simpler functions, the design becomes easier to understand, and thus makes the detection of bugs easier. This approach to splitting a large function into smaller functions allows for code reusability, as smaller independent functions can be easily shared among other test cases or across test suites.
With the Squish IDE, it is possible to refactor the code. Here are a few examples:
1: Creating functions from the existing script code
- Select the code you want to create the function for
- Right-click, and from the context menu choose Refactoring | Extract Method…
- In the ‘Extract Method’ dialogue, enter a relevant name for the function.
- The function will be created and called in your test script.
2: Improve the readability of the object names
Rather than relying on the names generated by Squish, you have the possibility to rename the symbolic names to more meaningful ones. This will also change all existing and future instances of that object name.
- Select the name of the object you want to change
- Right click and from the context menu select Refactoring | Rename…
- In the ‘Rename’ dialogue write the new name of the object
- After clicking OK, you should see the new name appear everywhere in the script
Here is a link to a recently released YouTube video tutorial which shows the same:
3: Create variables where necessary
- Select the string (including the quotes) you want to convert into a variable
- Right-click, and from the context menu select Refactoring | Extract Local Variable…
- In the ‘Extract Local Variable’ dialogue check the option ‘Also replace duplicates’
- Enter the name of the variable
The variable will appear as a part of your script and all references to the string will be replaced by the variable.
The refactoring capabilities of the Squish IDE don’t end here. The same can be applied to object-oriented script code and BDD-type test cases.