Case Study: User Reset Password via Email in Selenium WebDriver

How to support a typical user reset password test scenario with emails

Table of Contents:
· Test Design
· MailCatcher
· TempMailo
Complete test script
  1. Register a new user by email
    a random email address.
  2. Enter your email on the ‘Reset Password’ page then click the “Reset” button
  3. Wait for the reset password email with the reset link and visit the reset link
  4. Enter the new password
  5. Login with email and new password successfully
  1. Register a brand new user just for this test case only
    Essentially, include the ‘User Sign up’ test steps first. Because other team members are not aware of this new user, we don’t need to worry about the effect of password change.
  2. Receiving and reading emails
    This is covered in the previous article, but we can use a fake SMTP server or a temporary online mail service to receive emails.

With MailCatcher

Using a fake SMTP server.

  1. Register a new user
driver.find_element(:link_text, "Register").click
sign_up_page = SignUpPage.new(browser)
new_email = Faker::Internet.emailtry_for(2) { sign_up_page.enter_email(new_email) }
sign_up_page.enter_password("test02")
# on test server, the captcha is fixed to "wise"
sign_up_page.enter_captcha("wise")
sign_up_page.click_create_account
activate_url = niltry_for(10, 5) {
open_email("Account activation") { |email_body_html|
activate_url = Nokogiri::HTML(email_body_html).at_css("a#activate-link")["href"]
}
driver.get(activate_url)
try_for(3, 0.5) { expect(toast_text).to include("Account activated, you can sign in now.") }
}
visit("/sign-in")
driver.find_element(:link_text, "Forgot password?").click
reset_password_page = ResetPasswordPage.new(browser)
reset_password_page.enter_email(new_email)
reset_password_page.click_reset_button
sleep 0.75
reset_url = nil
open_email("Password Reset for WhenWise") { |email_body_html|
reset_url = Nokogiri::HTML(email_body_html).at_css("a#reset-url")["href"]
}
driver.get(reset_url)
expect(page_text).to include("Password Confirmation")
change_password_page = ChangePasswordPage.new(browser)
change_password_page.enter_new_password("pass02")
change_password_page.enter_password_confirmation("pass02")
change_password_page.click_change_password
expect(toast_text).to include("Password has been reset!")
sign_in(new_email, "pass02")
expect(toast_text).to include("You have signed in successfully")

With TempMailo

  1. Extract the email address from TempMailo
driver.get("https://tempmailo.com/")
email_address = driver.find_element(:id, "i-email")["value"]
driver.execute_script("window.open('https://whenwise.agileway.net/password_resets/new?locale=en-AU', '_blank')")
driver.switch_to.window(driver.window_handles[-1]) # last tab
forgot_password_page = ForgotPasswordPage.new(driver)
try_for(2) { forgot_password_page.enter_email(email_address) }
forgot_password_page.click_reset_password
try_for(2) { expect(page_text).to include("Email sent with password reset instructions.") }
driver.close
driver.switch_to.window(driver.window_handles[0]) # first tab
driver.find_element(:xpath, "//button[@class='prim-btn']").click
driver.find_element(:xpath, "//ul[@class='mail-items-list']/li[1]").click
frame = driver.find_element(:id, "fullmessage")
driver.switch_to.frame(frame)
driver.find_element(:partial_link_text, "password_resets").click
driver.switch_to.default_content
# switch to new tab
driver.switch_to.window(driver.window_handles[-1])
reset_password_page = ResetPasswordPage.new(driver)
reset_password_page.enter_password("test02")
reset_password_page.enter_password_confirmation("test02")
reset_password_page.click_change_password
expect(page_text).to include("Password has been reset!")

sign_in_page = SignInPage.new(driver)
sign_in_page.enter_email(email_address)
sign_in_page.enter_password("test02")
sign_in_page.click_login

expect(page_text).to include("You have signed in successfully")

Complete test script

it "Reset Password" do
driver.get("https://tempmailo.com/")
email_address = driver.find_element(:id, "i-email")["value"]
puts "Updated: #{email_address}"
# sign up with this account (manually)
sleep 10
driver.execute_script("window.open('https://whenwise.agileway.net/password_resets/new?locale=en-AU', '_blank')")
driver.switch_to.window(driver.window_handles[-1])
forgot_password_page = ForgotPasswordPage.new(driver)
try_for(2) { forgot_password_page.enter_email(email_address) }
forgot_password_page.click_reset_password
try_for(2) { expect(page_text).to include("Email sent with password reset instructions.") }
driver.close
driver.switch_to.window(driver.window_handles[0])

temp_mail_page = TempMailPage.new(driver)
temp_mail_page.click_refresh_emails
temp_mail_page.click_first_email
frame = driver.find_element(:id, "fullmessage")
driver.switch_to.frame(frame)
driver.find_element(:partial_link_text, "password_resets").click
driver.switch_to.default_content
driver.switch_to.window(driver.window_handles[-1])
reset_password_page = ResetPasswordPage.new(driver)
reset_password_page.enter_password("test02")
reset_password_page.enter_password_confirmation("test02")
reset_password_page.click_change_password
expect(page_text).to include("Password has been reset!")

sign_in_page = SignInPage.new(driver)
sign_in_page.enter_email(email_address)
sign_in_page.enter_password("test02")
sign_in_page.click_login

expect(page_text).to include("You have signed in successfully")
end

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store