Created
August 15, 2012 19:55
-
-
Save RSpace/3363086 to your computer and use it in GitHub Desktop.
Browser testing with Test::Unit, Capybara, Poltergeist and PhantomJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Feature < Test::Unit::TestCase | |
include Capybara::DSL, FactoryGirl::Syntax::Methods | |
def teardown | |
Capybara.reset_sessions! | |
Capybara.use_default_driver | |
end | |
add_teardown_hook do |context| | |
context.instance_eval do | |
if @passed.blank? | |
screenshot! | |
output_url | |
end | |
end | |
end | |
def with(widget_class) | |
yield widget_class.new(self) | |
end | |
def screenshot! | |
screenshot_dir = PODIO_ROOT + "test/screenshots/" | |
page.driver.render("#{screenshot_dir}#{@__name__}.png", :full => true) | |
source = page.evaluate_script("document.getElementsByTagName('html')[0].outerHTML") rescue nil | |
File.open("#{screenshot_dir}#{@__name__}.html", 'w') {|f| f.write(source) } if source | |
end | |
def output_url | |
puts "Failed at: #{current_url}" | |
end | |
# Assert that we are now on the expected page - allows the page 6 seconds to load | |
def assert_path(relative_path) | |
matched = false | |
20.times do | |
matched = (current_url =~ %r{#{relative_path}}) | |
break if matched | |
sleep(0.3) | |
end | |
assert matched, "Current url #{current_url} does not match the path #{relative_path}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bundler' | |
Bundler.setup(:default, :ci, :debug) | |
require 'test/unit' | |
require 'capybara' | |
require 'capybara/dsl' | |
require 'capybara/poltergeist' | |
require 'factory_girl' | |
# Determine environment and root | |
PODIO_ENV = ENV['PODIO_ENV'] || 'staging' | |
PODIO_ROOT = File.dirname(__FILE__) + '/../' | |
# Require test files | |
Dir.glob(PODIO_ROOT + 'lib/*', &method(:require)) | |
Dir.glob(PODIO_ROOT + 'test/widgets/*', &method(:require)) | |
Dir.glob(PODIO_ROOT + 'test/fixtures/*', &method(:require)) | |
# Configure Capybara | |
Capybara.configure do |config| | |
config.default_driver = :poltergeist | |
config.javascript_driver = :poltergeist | |
config.app_host = { | |
'development' => 'http://...', | |
'staging' => 'https://...', | |
'qapodio' => 'https://...' | |
}[PODIO_ENV] | |
config.run_server = false | |
config.default_wait_time = 10 | |
end | |
# Configure Poltergeist | |
Capybara.register_driver :poltergeist do |app| | |
Capybara::Poltergeist::Driver.new(app, | |
:inspector => '/Applications/Chromium.app/Contents/MacOS/Chromium', # TO USE: page.driver.debug | |
:window_size => [1280, 1024] | |
) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Widget | |
def initialize(feature) | |
@feature = feature | |
end | |
def method_missing(method_sym, *args) | |
if @feature.respond_to?(method_sym) | |
@feature.send(method_sym, *args) | |
else | |
super | |
end | |
end | |
class << self | |
def action(name, &body) | |
define_method name do |*args| | |
self.instance_exec(*args, &body) | |
end | |
end | |
def property(name, &body) | |
define_method name do | |
self.instance_eval(&body) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Figured out the reason for my confusion. PhantomJS was actually failing due to the recommended GA register FB social event call:
FB.Event.subscribe('edge.create', function(targetUrl) { [...]
This little beauty caused my site to fail with Poltergeist, while it worked when I tried for instance www.dr.dk :P
Removed the FB.Event calls, and everything worked as expected :)