To run the test, let's start from the terminal at the “webdriver-ruby-practice” directory.
We first need to be sure we have access to the RSpec gem.
So, to have access to RSpec for this project directory, I'll type:
gem install rspec
Once that's complete, I'll go ahead and run the test by typing “rspec” and the name of the test, which is “form_test.rb”
rspec form_test.rb
I see immediately that there is a failure with creating a new instance of the chromedriver.
And then below that we are unable to find the chromedriver on the path.
Going back to the test, I realized that I forgot one step.
After requiring RSpec on line two, I need to add a line to require the “chromedriver-helper”, which we downloaded earlier on in this course
require "chromedriver-helper"
After adding that line, I'll go ahead and save.
Now going back to the terminal, I can run the test again by typing “rspec form_test.rb”, and press “Enter”.
Now looking at the results, I see that the test failed to validate the expectation.
I just saw this functionality work, so I must be to account for this behavior in my test somehow.
I remember that after submitting the form, it takes a second for the page to load. So, what I can do now is to wait for the banner to appear before trying to interact with it.
Going back to my test, before the expectation on line 19, I need to wait for this banner element to appear.
So, we'll add a line here, and I will define a new wait, specifying a timeout of 10 seconds
wait = Selenium::WebDriver::Wait.new(timeout: 10) # seconds
And then what I need to do is to wait until the driver can find the element with the class of “alert”, and I want to wait for this to be displayed
wait.until { driver.find_element(class: 'alert') }
Now go ahead and save this test.
Going back to the terminal, I want to run the test one more time and see what happens.
I see that the browser opens again, submits the steps to fill out the form. And then going back to the terminal, I see now that there is one example that has finished successfully and there were zero failures.
And this little dot here also indicates that one test was run and that it was run successfully.
So now you have a little bit of idea about how to run tests.
For additional practice, feel free to go back to other examples in this course and incorporate RSpec to give structure to the test and run them.
Form Test Example Code with RSpec Structure
require "selenium-webdriver"
require "chromedriver-helper"
require "rspec"
describe "automating a form" do
it "submits a form" do
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "https://formy-project.herokuapp.com/form"
driver.find_element(id: 'first-name').send_keys('John')
driver.find_element(id: 'last-name').send_keys('Doe')
driver.find_element(id: 'job-title').send_keys('QA')
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('05/25/2025')
driver.find_element(id: 'datepicker').send_keys :return
driver.find_element(css: '.btn.btn-lg.btn-primary').click
wait = Selenium::WebDriver::Wait.new(timeout: 10) # seconds
wait.until { driver.find_element(class: 'alert') }
expect(driver.find_element(class: 'alert').text).to eql('The form was successfully submitted!')
driver.quit
end
end