Permalink
Browse files

Create email_helper.rb

  • Loading branch information...
1 parent 6978720 commit 87f51bf92021e4521b895fb84d9933b4d52430e3 @oriolgual oriolgual committed Dec 6, 2012
Showing with 42 additions and 0 deletions.
  1. +42 −0 email_helper.rb
View
@@ -0,0 +1,42 @@
+#
+# Usage:
+#
+# class MyFeature < Spinach::FeatureSteps
+# include Spinach::MailHelpers
+# end
+#
+module Spinach
+ module MailHelpers
+ include Spinach::DSL
+
+ # Returns the last sent mail.
+ def last_mail
+ # Uncomment if you use Sidekiq with ActionMailer
+ # Sidekiq::Extensions::DelayedMailer.drain
+ ActionMailer::Base.deliveries.first
+ end
+
+ # Returns an array with all the links in the email body.
+ def links_in_email(email)
+ URI.extract(email.to_s, ['http', 'https'])
+ end
+
+ # Visits a link in the provided email. Will visit the last mail if no regex given
+ #
+ # email - The email to search for links.
+ # regex - An optional regex to match the link to
+ #
+ def click_link_in_email(email, regex = nil)
+ links = links_in_email(email)
+ link = if regex
+ links.detect do |link|
+ regex.match(link)
+ end
+ else
+ links.first
+ end
+
+ visit URI.parse(link).request_uri
+ end
+ end
+end

0 comments on commit 87f51bf

Please sign in to comment.