DEV Community

Arnaud Joubay
Arnaud Joubay

Posted on • Originally published at paraside.in on

1 1

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

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay