Case Study: User Sign up Automated Test in Selenium WebDriver
Using temporary mail services to support a typical user signup test scenario
This is included in the “How to in Selenium WebDriver” series.
Table of Contents:
· Test Design
· Email checking with MailCatcher
· Email checking with TempMailO
∘ Complete Test Script (TempMailo)
· Another Temporary Free Email Provider, Temp-Mail
This article will show how to implement user-sign-up automated tests (in Selenium) using two Email Checking mechanisms:
MailCatcher: fake SMTP server
TempMailO: an online temporary email provider
Test Design
Create a new user via the signup form
Try to log in with the new account, and expect “the user not activated yet” message
Wait for the activation email with the account activation link
Click the activation link in the email
Log in as the new user successfully
The key to this test is to receive and read the account activation email. But how do we handle emails as part of automated tests, given that we might need a new unique email account for each test execution? (Considering in the context of Continuous Testing)
Email checking with MailCatcher
MailCatcher is a local fake SMTP server (not actually sending emails out, but enough for verification), which can be very handy for testing emails locally. For more details see this article.
Setting up to use an SMTP server in your app servers is not always possible (may require dev support). I strongly recommend using MailCatcher, it is easy to set up, and well worth it. I understand some of you might be resistant to using a fake SMTP server at work, so this article will focus on the other approach: temporary emails. Anyway, below is the selenium test script using MailCatcher.
Test Script
Below is the Raw Selenium test (Ruby) based on our test design, using MailCatcher.
it "User sign up with email" do
sign_up_page = SignUpPage.new(browser)
new_email = Faker::Internet.email
puts new_email
try_for(2) { sign_up_page.enter_email(new_email) }
sign_up_page.enter_password("test02")
fail_safe{ sign_up_page.enter_captcha("wise") }
sign_up_page.click_create_account
expect(page_text).to include("Please check your email to activate your account.")
visit("/sign-in")
sign_in(new_email, "test02")
try_for(3) { expect(page_text).to include("not activated yet") }
activate_url = nil
try_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.") }
}
sign_in(new_email, "test02")
expect(page_text).to include("Find Business")
end
open_email
is a reusable function defined in the helper, which checks emails in MailCatcher based on the email subject.
Example Use:
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) { expect(toast_text).to include("Account activated, you can sign in now.") }
}
Email checking with TempMailO
https://tempmailo.com/
This is one temporary mail provider I was able to get working reliably.
The steps in my automated test:
1. Extract the email address from TempMailo
I extract the one-use-only email out and save it as email_address
, so that I can continue to use it later in the test script.
driver.get("https://tempmailo.com/")
email_address = driver.find_element(:id, "i-email")["value"]
2. Visit WhenWise in a new browser tab and register a new user account with this email
driver.execute_script("window.open('https://whenwise.agileway.net/sign-up', '_blank')")
driver.switch_to.window(driver.window_handles[-1])
The switch_to.window
line changes the focus to the last tab.
From here, sign up with the email address.
driver.find_element(:id, "email").send_keys(email_address)
driver.find_element(:id, "password").send_keys("test01")
driver.find_element(:id, "create-account").click
sleep 0.2
expect(page_text).to include("Please check your email to activate your account.")
3. Try to log in with the newly created account (expect failure)
Since the account isn’t activated, the user should not be able to sign in yet. Verify that sign-in fails.
sign_in_page = SignInPage.new(driver)
sign_in_page.enter_email(email_address)
sign_in_page.enter_password("test01")
sign_in_page.click_login
try_for(2) { expect(page_text).to include("The account is not activated yet") }
4. Switch back to TempMailo and wait for the account confirmation email (sent from WhenWise)
Close the current tab to focus back on the TempMailo tab.
driver.close
driver.switch_to.window(driver.window_handles[0]) # first tab
Back at TempMailo, refresh the inbox and the password reset email will come in. This will take around 10 seconds.
# Wait for email to appear
wait = Selenium::WebDriver::Wait.new(:timeout => 25)
wait.until { driver.find_element(:xpath, "//li[@class='mail-item']/div[@class='title' and text()='noreply@whenwise.com']") }
Click the email on the sidebar to open it.
driver.find_element(:xpath, "//ul[@class='mail-items-list']/li[1]").click
5. Click the account activation link in the email
The actual email’s contents (right panel) are inside an iFrame (for more on testing with iFrames, check out Automated Testing Frames in Selenium WebDriver), so, switch to the iFrame to click the link. The link will open in a new tab:
frame = driver.find_element(:id, "fullmessage")
driver.switch_to.frame(frame)
driver.find_element(:partial_link_text, "account_activations").click
6. In WhenWise, confirm the account was created by signing-in
driver.switch_to.default_content
driver.switch_to.window(driver.window_handles[-1]) # last tab
From the new browser tab, the rest of the testcase is straightforward. Enter the new password and verify sign in is successful.
expect(page_text).to include("Account activated, you can sign in now.")login_page = LoginPage.new(driver)
login_page.enter_email(email_address)
login_page.enter_password("test01")
login_page.click_login
expect(page_text).to include("You have signed in successfully")
By the way, the above test scripts have been refactored to use Maintainable Automated Test Design.
Complete Test Script (TempMailo)
it "Sign up new user" do
driver.get("https://tempmailo.com/")
email_address = driver.find_element(:id, "i-email")["value"]
puts "New user: #{email_address}"
driver.execute_script("window.open('https://whenwise.agileway.net/sign-up', '_blank')")
driver.switch_to.window(driver.window_handles[-1])
driver.find_element(:id, "email").send_keys(email_address)
driver.find_element(:id, "password").send_keys("test01")
driver.find_element(:id, "create-account").click
sleep 0.2
expect(page_text).to include("Please check your email to activate your account.")
sign_in_page = SignInPage.new(driver)
sign_in_page.enter_email(email_address)
sign_in_page.enter_password("test01")
sign_in_page.click_login
try_for(2) { expect(page_text).to include("The account is not activated yet") }
driver.close
driver.switch_to.window(driver.window_handles[0])
temp_mail_page = TempMailPage.new(driver)
temp_mail_page.click_refresh_emails
# wait for email to come in, timeout: 25 seconds
wait = Selenium::WebDriver::Wait.new(:timeout => 25)
wait.until { driver.find_element(:xpath, "//li[@class='mail-item']/div[@class='title' and text()='noreply@whenwise.com']") }
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, "account_activations").click
driver.switch_to.default_content
driver.switch_to.window(driver.window_handles[-1])
expect(page_text).to include("Account activated, you can sign in now.")
login_page = LoginPage.new(driver)
login_page.enter_email(email_address)
login_page.enter_password("test01")
login_page.click_login
expect(page_text).to include("You have signed in successfully")
end
I also tried another mail service (tempail.com), here are some notes I took for the test.
Another Temporary Free Email Provider, Temp-Mail
At first, I tried to use tempail.com, this temporary mail service is free, but it has unpredictable behaviour. Occasionally there are Captchas (once completed manually it doesn’t appear the next 2–3 times).
And advertisement pop-ups intermittently when you try to read an email.
I could get it working, however, it is too much of a hassle and quite a bit of work to make automated test scripts reliable. Overall, I recommend using TempMailo as part of automated tests as it is quite reliable and does not have popup advertisements.