Capybara: 2 ways to click links in emails
1 min read

Capybara: 2 ways to click links in emails

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

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"]

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

And you can open your email with just 2 lines:

open_email "[email protected]"
current_email.click_link "Click this link"