A website template with lots of features, built with ruby on rails.

email_steps.rb 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # Commonly used email steps
  2. #
  3. # To add your own steps make a custom_email_steps.rb
  4. # The provided methods are:
  5. #
  6. # last_email_address
  7. # reset_mailer
  8. # open_last_email
  9. # visit_in_email
  10. # unread_emails_for
  11. # mailbox_for
  12. # current_email
  13. # open_email
  14. # read_emails_for
  15. # find_email
  16. #
  17. # General form for email scenarios are:
  18. # - clear the email queue (done automatically by email_spec)
  19. # - execute steps that sends an email
  20. # - check the user received an/no/[0-9] emails
  21. # - open the email
  22. # - inspect the email contents
  23. # - interact with the email (e.g. click links)
  24. #
  25. # The Cucumber steps below are setup in this order.
  26. module EmailHelpers
  27. def current_email_address
  28. # Replace with your a way to find your current email. e.g @current_user.email
  29. # last_email_address will return the last email address used by email spec to find an email.
  30. # Note that last_email_address will be reset after each Scenario.
  31. last_email_address || "example@example.com"
  32. end
  33. end
  34. World(EmailHelpers)
  35. #
  36. # Reset the e-mail queue within a scenario.
  37. # This is done automatically before each scenario.
  38. #
  39. Given /^(?:a clear email queue|no emails have been sent)$/ do
  40. reset_mailer
  41. end
  42. #
  43. # Check how many emails have been sent/received
  44. #
  45. Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/ do |address, amount|
  46. unread_emails_for(address).size.should == parse_email_count(amount)
  47. end
  48. Then /^(?:I|they|"([^"]*?)") should have (an|no|\d+) emails?$/ do |address, amount|
  49. mailbox_for(address).size.should == parse_email_count(amount)
  50. end
  51. Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject "([^"]*?)"$/ do |address, amount, subject|
  52. # @email = ActionMailer::Base.deliveries.last
  53. # @email.to.should include address
  54. # @email.subject.should subject
  55. # CucumberExternalResqueWorker.reset_counter
  56. # Resque.remove_queue(:send_contact_message_queue)
  57. # CucumberExternalResqueWorker.process_all
  58. unread_emails_for(address).select { |m| m.subject =~ Regexp.new(Regexp.escape(subject)) }.size.should == parse_email_count(amount)
  59. end
  60. Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject \/([^"]*?)\/$/ do |address, amount, subject|
  61. unread_emails_for(address).select { |m| m.subject =~ Regexp.new(subject) }.size.should == parse_email_count(amount)
  62. end
  63. Then /^(?:I|they|"([^"]*?)") should receive an email with the following body:$/ do |address, expected_body|
  64. open_email(address, :with_text => expected_body)
  65. end
  66. #
  67. # Accessing emails
  68. #
  69. # Opens the most recently received email
  70. When /^(?:I|they|"([^"]*?)") opens? the email$/ do |address|
  71. open_email(address)
  72. end
  73. When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
  74. open_email(address, :with_subject => subject)
  75. end
  76. When /^(?:I|they|"([^"]*?)") opens? the email with subject \/([^"]*?)\/$/ do |address, subject|
  77. open_email(address, :with_subject => Regexp.new(subject))
  78. end
  79. When /^(?:I|they|"([^"]*?)") opens? the email with text "([^"]*?)"$/ do |address, text|
  80. open_email(address, :with_text => text)
  81. end
  82. When /^(?:I|they|"([^"]*?)") opens? the email with text \/([^"]*?)\/$/ do |address, text|
  83. open_email(address, :with_text => Regexp.new(text))
  84. end
  85. #
  86. # Inspect the Email Contents
  87. #
  88. Then /^(?:I|they) should see "([^"]*?)" in the email subject$/ do |text|
  89. current_email.should have_subject(text)
  90. end
  91. Then /^(?:I|they) should see \/([^"]*?)\/ in the email subject$/ do |text|
  92. current_email.should have_subject(Regexp.new(text))
  93. end
  94. Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
  95. current_email.default_part_body.to_s.should include(text)
  96. end
  97. Then /^(?:I|they) should see \/([^"]*?)\/ in the email body$/ do |text|
  98. current_email.default_part_body.to_s.should =~ Regexp.new(text)
  99. end
  100. Then /^(?:I|they) should see the email delivered from "([^"]*?)"$/ do |text|
  101. current_email.should be_delivered_from(text)
  102. end
  103. Then /^(?:I|they) should see "([^\"]*)" in the email "([^"]*?)" header$/ do |text, name|
  104. current_email.should have_header(name, text)
  105. end
  106. Then /^(?:I|they) should see \/([^\"]*)\/ in the email "([^"]*?)" header$/ do |text, name|
  107. current_email.should have_header(name, Regexp.new(text))
  108. end
  109. Then /^I should see it is a multi\-part email$/ do
  110. current_email.should be_multipart
  111. end
  112. Then /^(?:I|they) should see "([^"]*?)" in the email html part body$/ do |text|
  113. current_email.html_part.body.to_s.should include(text)
  114. end
  115. Then /^(?:I|they) should see "([^"]*?)" in the email text part body$/ do |text|
  116. current_email.text_part.body.to_s.should include(text)
  117. end
  118. #
  119. # Inspect the Email Attachments
  120. #
  121. Then /^(?:I|they) should see (an|no|\d+) attachments? with the email$/ do |amount|
  122. current_email_attachments.size.should == parse_email_count(amount)
  123. end
  124. Then /^there should be (an|no|\d+) attachments? named "([^"]*?)"$/ do |amount, filename|
  125. current_email_attachments.select { |a| a.filename == filename }.size.should == parse_email_count(amount)
  126. end
  127. Then /^attachment (\d+) should be named "([^"]*?)"$/ do |index, filename|
  128. current_email_attachments[(index.to_i - 1)].filename.should == filename
  129. end
  130. Then /^there should be (an|no|\d+) attachments? of type "([^"]*?)"$/ do |amount, content_type|
  131. current_email_attachments.select { |a| a.content_type.include?(content_type) }.size.should == parse_email_count(amount)
  132. end
  133. Then /^attachment (\d+) should be of type "([^"]*?)"$/ do |index, content_type|
  134. current_email_attachments[(index.to_i - 1)].content_type.should include(content_type)
  135. end
  136. Then /^all attachments should not be blank$/ do
  137. current_email_attachments.each do |attachment|
  138. attachment.read.size.should_not == 0
  139. end
  140. end
  141. Then /^show me a list of email attachments$/ do
  142. EmailSpec::EmailViewer::save_and_open_email_attachments_list(current_email)
  143. end
  144. #
  145. # Interact with Email Contents
  146. #
  147. When /^(?:I|they|"([^"]*?)") clicks in the link? "([^"]*?)" in the email$/ do |address, link|
  148. visit_in_email(link, address)
  149. end
  150. When /^(?:I|they) click the first link in the email$/ do
  151. click_first_link_in_email
  152. end
  153. #
  154. # Debugging
  155. # These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command.
  156. # Patches accepted. ;)
  157. #
  158. Then /^save and open current email$/ do
  159. EmailSpec::EmailViewer::save_and_open_email(current_email)
  160. end
  161. Then /^save and open all text emails$/ do
  162. EmailSpec::EmailViewer::save_and_open_all_text_emails
  163. end
  164. Then /^save and open all html emails$/ do
  165. EmailSpec::EmailViewer::save_and_open_all_html_emails
  166. end
  167. Then /^save and open all raw emails$/ do
  168. EmailSpec::EmailViewer::save_and_open_all_raw_emails
  169. end