Transcripted Summary

We can do some more cleanup in order to make the test cleaner and easier to read.

First, I can extract all the steps to submit a form into a single method.

  1. To do that, I'll start by defining a new method on line 11 called — def submit_form(driver) — and then what I can do is copy the lines of the test from lines 20 up until line 28 and insert those into the submit_form method. One thing that I'll need to do is to pass the driver as a parameter here so that the driver can be used in the method and in the test below.

  2. Then what I can do is to copy this method name and to paste it on line 27 after navigating to the form — submit_form(driver)

I can also clean up this test around the expectation. Expectations should be clearly defined in tests, so let's not move that.

But we can write a method to get the banner text.

  1. So, to do that I'll go back up to line 23, define a new method called get_banner_text and go ahead and pass in the driver as a parameter

    def get_banner_text(driver)
    
  2. Then what I can do is to copy these two lines which explicitly wait for the element class to appear and cut and in the body of the get banner text method, I can paste these two lines.

  3. Next, I'll define a variable called “banner” and tell the driver to find the element with the class of “alert”

    banner = driver.find_element(class: 'alert')
    
  4. And then on line 27 what I'll do is define another variable called banner_text and get the text value of the element with the class “alert” and then this will be returned when I call this method

    banner_text = banner.text
    
  5. So back in the test, what I want to do now is to define a new variable called “actual_banner_text” and then call this “get_banner_text” method passing in the parameter of driver

    actual_banner_text = get_banner_text(driver)
    
  6. And then what I can do is to use this “actual_banner_text” variable and then I can replace that to the call that's happening to get the actual value of that banner text here

    expect(actual_banner_text).to eql($expected_banner_text)
    

# Clean Form Test Code with Methods

require "selenium-webdriver"

require "rspec"

require "chromedriver-helper"

$first_name = 'John'

$last_name = 'Doe'

$job_title = 'QA'

$date = '05/25/2025'

$expected_banner_text = 'The form was successfully submitted!'

def submit_form(driver)

  driver.find_element(id: 'first-name').send_keys($first_name)

  driver.find_element(id: 'last-name').send_keys($last_name)

  driver.find_element(id: 'job-title').send_keys($job_title)

  driver.find_element(id: 'radio-button-2').click

  driver.find_element(id: 'checkbox-2').click

  driver.find_element(css: 'option[value="2"]').click

  driver.find_element(id: 'datepicker').send_keys($date)

  driver.find_element(id: 'datepicker').send_keys :return

  driver.find_element(css: '.btn.btn-lg.btn-primary').click

end

def get_banner_text(driver)

  wait = Selenium::WebDriver::Wait.new(timeout: 10)

  wait.until { driver.find_element(class: 'alert').displayed? }

  banner = driver.find_element(class: 'alert')

  banner_text = banner.text

end

describe "automating a form" do

  it "submits a form" do

    driver = Selenium::WebDriver.for :chrome

    driver.navigate.to "https://formy-project.herokuapp.com/form"

    submit_form(driver)

    actual_banner_text = get_banner_text(driver)

    expect(actual_banner_text).to eql($expected_banner_text)

    driver.quit

  end

end

Notice now that the test itself here is much easier to read and it's very clear of what the test is doing.

I'll hop over to the terminal, run the test again. I see that this test does in fact complete successfully, even after the refactoring that we've made. So that's really great.

So, these are just a couple examples of how you can clean up a test to make it more readable.

I'm going to go ahead and stop here now and thank you for watching this course on learning Selenium WebDriver with Ruby.

I hope that you've learned something new and feel more empowered to go out and write more automation tests. Let me know if you have any feedback or questions, I can help answer. I'd love to hear from you.

Cheers.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR