DEV Community

loading...

Capybara: 2 ways to click links in emails

sowenjub profile image Arnaud Joubay Originally published at paraside.in on ・1 min read

If you want to write system tests you'll probably want to cover a use-case involving an email sent to a user (for instance, a sign up confirmation link).

There are at least 2 ways to do that:

With nokogiri

Add the gem to your Gemfile and run bundle install.
Chances are you'll find it in your Gemfile.lock already since it's a pretty standard gem with 5k+ stars.

group :test do
  gem "nokogiri"
end
Enter fullscreen mode Exit fullscreen mode

And you can open your email and click the line like this:

email = ActionMailer::Base.deliveries.last
html = Nokogiri::HTML(email.html_part.body.to_s)
target_link = html.at("a:contains('Click this link')")
visit target_link["href"]
Enter fullscreen mode Exit fullscreen mode

With capybara-email gem

Add the gem to your Gemfile and run bundle install. It only has 300+ stars, which is still pretty good, and it's also a rather simple gem so you could easily decide to contribute to it if your app depended on it.

group :test do
  gem "capybara-email"
end
Enter fullscreen mode Exit fullscreen mode

And you can open your email with just 2 lines:

open_email "arnaud@example.com"
current_email.click_link "Click this link"
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide