Functional Test Refactoring: Move to Helper
Make a helper function available to all test scripts

This is one of the 6 Functional Test Refactorings (see the introduction here):
- Extract Function
- Move Function to Helper
- Move
- Extract Page Function
- Introduce Page Object
- Rename
The structure of this article is the same as “Extract Function” (see detail there). The same test project that helps you do refactoring exercises quickly:
> cd my-working-dir
> git clone https://github.com/testwisely/agiletravel-ui-testsThe test project is at my-working-dir/agiletravel-ui-tests/pre-refactoring .
Motivation
You use the same functions defined in separate test script files.
As a common practice, all test scripts in a project share a common helper. By moving a function to the helper, it is made available to all test cases.
Sample Tests (before) A login function created by Tester#1 in TestScript#10
def login(user, pass)
# ...
endAnother login function created by Tester#2 in TestScript#20
def login(user_name, password)
# ...
endIssues with the tests
- Violates DRY (Don’t Repeat Yourself) There are several cases of the same function (which might be named differently) that do the same thing.
Action
Move the function to the shared test helper, delete similar ones and use the one in the test helper.
Prerequisite
The test project follows the Maintainable Automated Test Design, has a shared test helper.
Mechanics
- Identify a function
- Move to the helper
- Rerun the test
Refactoring Steps in TestWise
- Choose the function (by moving the caret to its definition)
- Invoke refactoring (and preview)
- Confirm to apply the refactoring
Expected Result
- The function definition is now moved to the shared test helper file
After a function is moved to the helper, you may use it in other tests.
Confirmation Dialog

Sample Tests (after)
The login functions in TestScript#10 and TestScript#20 are deleted. A new function is added to the test helper.
# test_helperdef login(user_name, password)
# ...
endDemonstration (animated GIF)
The test script file in the sample project: spec/login_spec.rb
- Move a function (from a test) to the test helper Firstly, move the caret to the function name, then apply this refactoring.

2. Use an already-defined function in the test helper

3. You can quickly navigate to the definition of a function from tests, press “Ctrl+B” (or Cmd +B on macOS) on the function name.

Demonstration (Video)
- Move functions to the shared test helper

