The Agile Way

The Agile Way

Share this post

The Agile Way
The Agile Way
15 Refactoring: Extract Sign In Function
Selenium Training Workbook

15 Refactoring: Extract Sign In Function

Extract common test steps into a reusable function.

Zhimin Zhan's avatar
Zhimin Zhan
Dec 06, 2022
∙ Paid
1

Share this post

The Agile Way
The Agile Way
15 Refactoring: Extract Sign In Function
Share

For now, you can consider refactoring a process of refining automated test scripts, for easier to maintain and read.

Learning Objectives

  • Test Maintenance

  • DRY

  • Extract Function Refactoring

To perform this exercise, open the exsiting TestWise project you created in earlier session, Or simply download this test project.

A Simplified Test Maintenance Case Study

Previously, we created two test cases in a test script file (login_spec.rb).

  it "User can sign in OK" 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(:link_text, "Sign off").click
  end

  it "User can sign in failed" do
    driver.find_element(:id, "username").send_keys("agileway")
    driver.find_element(:name, "password").send_keys("badpass")
    driver.find_element(:name, "commit").click
    expect(page_text).to include("Invalid email or password")
  end

During live coaching, I change the Agile Travel server: `username` => `login`.

Run the test script again.

Both test cases failed. Check the error under the “Test Ouptut” tab.

As expected, the element with ID `username` is no longer on the page (now `login`).

I update the test script on two test steps: lines 18 and 26.

Run it again, and pass!

What do we learn from the above case study?

Most beginners were quite satisfied with the above practice. It seems logical, 70+% of so-called test professionals (at work) would think so too. But it is not right! Try run all test scripts in the `spec` folder.

Except the `login_spec.rb` (the one we just changed), all other tests failed. You know the cause, right?

Will you change each of these four failed tests? Probably. But I am sure you will start the question about the approach. How about 100 tests, or even 500 tests?

Task: Extract Function Refactoring

You must have observed this: all test scripts containing these three user login steps.

    driver.find_element(:id, "username").send_keys("agileway")
    driver.find_element(:name, "password").send_keys("testwise")
    driver.find_element(:name, "commit").click

When talking about a website ( everyday life, outside software development ), we usually just say “Sign in” or “Login”. So, the solution is to somehow these three login steps reusable in a form.

Perform the extract function refactoring in TestWise

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.

Already a paid subscriber? Sign in
© 2025 Zhimin Zhan
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share