17. Move test steps into Rspec hooks
Make the code more concise and clear.
Learning Objectives
Move refactoring
move multiple test steps to `before(:all)` hook
Auto-complete a function (defined in test helper)
Move refactoring
move single test step to `before(:all)` hook
move test steps to `before(:each)` hook
Tasks
Task 1. Move sign-in steps to `before(:all)`
1. passenger_spec.rb
before(:all) do
@driver = Selenium::WebDriver.for(browser_type, browser_options)
driver.manage().window().resize_to(1280, 800)
driver.get("https://travel.agileway.net")
end
after(:all) do
driver.quit unless debugging?
end
it "User can enter passenger details" do
driver.find_element(:id, "username").send_keys("agileway")
driver.find_element(:name, "password").send_keys("testwise")
driver.find_element(:name, "commit").click
expect(page_text).to include("Signed in!")
driver.find_element(:xpath, "//input[@value='oneway']").click
# more steps ...
end
Select the three sign-in steps, and then select the menu “Refactor” → “Move …”.
Select “1 Move to before(:all)
”.
Preview in the popup Move dialog.
Click the “Refactor” button to confirm.
The test script fragment becomes:
Keep reading with a 7-day free trial
Subscribe to The Agile Way to keep reading this post and get 7 days of free access to the full post archives.