16 Refactoring: Move to Helper
Make extracted function available to all other test scripts
Learning Objectives
Review
Extract Function Refactoring
Move to Helper Refactoring
Tasks
In the previous session, we extracted the three user-sign-in steps into a function `sign_in
`.
def sign_in(login, password)
driver.find_element(:id, "username").send_keys(login)
driver.find_element(:name, "password").send_keys(password)
driver.find_element(:name, "commit").click
end
We continue to extract the sign-off step.
After that, rerun the test script `login_spec.rb
`, and it shall still work.
Now, you might wonder, how to use the `sign_in
` function in other test scripts? The answer: move the function to a shared test helper. All functions declared in this shared test helper are available to all test scripts.
While we can move the functional by hand, it is recommended.
Use “Move to helper” functional test refactoring.
Task 1. Extract the sign-off step to a function
Select the sign-off test step, and select men `Refactor` → `Extract to Function …`
In the “Extract Function” dialog, enter the function name “sign_off”.
Keep reading with a 7-day free trial
Subscribe to AgileWay’s Test Automation & Continuous Testing Blog to keep reading this post and get 7 days of free access to the full post archives.