Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 0fa98113182de331ab32ad1b2f1a8c991c99daa7 @bcardarella bcardarella committed Mar 4, 2012
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
1 .rspec
@@ -0,0 +1 @@
+--colour
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in capybara-email.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Brian Cardarella
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
+# Capybara::Email
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'capybara-email'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install capybara-email
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/capybara/email/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ['Brian Cardarella']
+ gem.email = ['bcardarella@gmail.com', 'brian@dockyard.com']
+ gem.description = %q{Test your ActionMailer emails in Capybara}
+ gem.summary = %q{Test your ActionMailer emails in Capybara}
+ gem.homepage = 'https://github.com/dockyard/capybara-email'
+
+ gem.executables = `git ls-files -- bin/*`.split('\n').map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split('\n')
+ gem.test_files = `git ls-files -- {spec}/*`.split('\n')
+ gem.name = 'capybara-email'
+ gem.require_paths = ['lib']
+ gem.version = Capybara::Email::VERSION
+
+ gem.add_development_dependency 'actionmailer'
+ gem.add_development_dependency 'capybara'
+ gem.add_development_dependency 'rspec'
+end
@@ -0,0 +1,6 @@
+require 'capybara/email/version'
+
+module Capybara
+ module Email
+ end
+end
@@ -0,0 +1,5 @@
+module Capybara
+ module Email
+ VERSION = "0.0.1"
+ end
+end
@@ -0,0 +1,6 @@
+module Capybara::Email
+ autoload :Node, 'capybara/email/node'
+ autoload :Driver, 'capybara/email/driver'
+ autoload :Version, 'capybara/email/version'
+end
+
@@ -0,0 +1,41 @@
+class Capybara::Email::Driver < Capybara::Driver::Base
+ attr_reader :email
+
+ def initialize(email)
+ @email = email
+ end
+
+ def follow(method, path, attributes = {})
+ Capybara.current_session.driver.follow(method, path, attributes)
+ end
+
+ def body
+ dom.to_xml
+ end
+
+ def dom
+ @dom ||= Nokogiri::HTML(source)
+ end
+
+ def find(selector)
+ dom.xpath(selector).map { |node| Capybara::Email::Node.new(self, node) }
+ end
+
+ def source
+ if email.mime_type == 'text/plain'
+ convert_to_html(email.body.encoded)
+ else
+ email.body.encoded
+ end
+ end
+
+ private
+
+ def convert_to_html(text)
+ "<html><body>#{convert_links(text)}</body></html>"
+ end
+
+ def convert_links(text)
+ text.gsub(%r{(https?://\S+)}, %q{<a href="\1">\1</a>})
+ end
+end
@@ -0,0 +1,35 @@
+class Capybara::Email::Node < Capybara::Driver::Node
+ def text
+ native.text
+ end
+
+ def [](name)
+ string_node[name]
+ end
+
+ def value
+ string_node.value
+ end
+
+ def click
+ driver.follow(:get, self[:href].to_s)
+ end
+
+ def tag_name
+ native.node_name
+ end
+
+ def visible?
+ string_node.visible?
+ end
+
+ def find(locator)
+ native.xpath(locator).map { |node| self.class.new(driver, node) }
+ end
+
+ private
+
+ def string_node
+ @string_node ||= Capybara::Node::Simple.new(native)
+ end
+end
@@ -0,0 +1,7 @@
+require 'capybara/email'
+require 'capybara/email/rspec/helpers'
+
+RSpec.configure do |config|
+ config.include Capybara::Email::RSpecHelpers, :type => :request
+ config.include Capybara::Email::RSpecHelpers, :type => :acceptance
+end
@@ -0,0 +1,30 @@
+module Capybara::Email::RSpecHelpers
+ attr_accessor :current_email, :current_emails
+
+ def all_emails
+ ActionMailer::Base.deliveries
+ end
+
+ def emails_sent_to(recipient)
+ self.current_emails = all_emails.select { |email| email.to.include?(recipient) }.map do |email|
+ driver = Capybara::Email::Driver.new(email)
+ Capybara::Node::Document.new(Capybara.current_session, driver)
+ end
+ end
+
+ def first_email_sent_to(recipient)
+ self.current_email = emails_sent_to(recipient).first
+ end
+ alias :open_email :first_email_sent_to
+
+ def current_emails
+ @current_emails || []
+ end
+
+ def clear_emails
+ all_emails.clear
+ self.current_emails = nil
+ self.current_email = nil
+ end
+ alias :clear_email :clear_emails
+end
@@ -0,0 +1,5 @@
+module Capybara
+ module Email
+ VERSION = '0.0.1'
+ end
+end
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+feature 'Integration test' do
+ background do
+ clear_email
+ Capybara.app = TestApp
+ end
+
+ scenario 'html email' do
+ deliver(html_email)
+ open_email('test@example.com')
+ current_email.click_link 'example'
+ page.should have_content 'Hello world!'
+ current_email.should have_content 'This is only a html test'
+ end
+
+ scenario 'plain text email' do
+ deliver(plain_email)
+ open_email('test@example.com')
+ current_email.click_link 'http://example.com'
+ page.should have_content 'Hello world!'
+ current_email.should have_content 'This is only a plain test.'
+ end
+end
+
+class TestApp
+ def self.call(env)
+ [200, {"Content-Type" => "text/plain"}, ["Hello world!"]]
+ end
+end
+
+def deliver(email)
+ ActionMailer::Base.deliveries << email
+end
+
+def html_email
+ Mail::Message.new(:body => <<-HTML, :content_type => 'text/html', :to => 'test@example.com')
+<html>
+ <body>
+ <p>
+ This is only a html test.
+ <a href="http://example.com">example</a>
+ </p>
+ </body>
+</html>
+ HTML
+end
+
+def plain_email
+ Mail::Message.new(:body => <<-PLAIN, :content_type => 'text/plain', :to => 'test@example.com')
+This is only a plain test.
+http://example.com
+ PLAIN
+end
@@ -0,0 +1 @@
+require 'spec_helper'
@@ -0,0 +1,6 @@
+require 'rspec'
+require 'action_mailer'
+require 'capybara/rspec'
+require 'capybara/email/rspec'
+require 'capybara/spec/driver'
+require 'pry-nav'

0 comments on commit 0fa9811

Please sign in to comment.