Skip to content

Instantly share code, notes, and snippets.

@tommeier
Created May 18, 2011 02:06
Show Gist options
  • Save tommeier/977863 to your computer and use it in GitHub Desktop.
Save tommeier/977863 to your computer and use it in GitHub Desktop.
Save screenshots when using Selenium, Capybara and Ubuntu server
#WIP
#spec/spec_helper.rb
RSpec.configure do |config|
#Only for selenium based tested filtered with :js => true on tests
#eg: describe "Some test", :js => true do
# end
config.around(:each, :js => true) do |example|
Capybara.current_driver = :selenium
result = example.run
unless result.blank? || result == true
require 'selenium-webdriver' #>= 0.2
#Example failed
images_root = File.join(Rails.root, 'tmp')
FileUtils.mkdir_p(images_root) unless File.exists?(images_root)
file_name = "#{File.basename(example.metadata[:example_group][:file_path]).to_s.underscore.gsub('.','_')}"
file_name += "_line_#{example.metadata[:example_group][:line_number]}.png"
page.driver.browser.save_screenshot(File.join(images_root, file_name))
end
Capybara.use_default_driver
end
end
@tommeier
Copy link
Author

tommeier commented Jul 9, 2011

Also - If anyone else gets caught out by the recent issue with shared connections on Active Record (eg. missing data, only when you load on the selenium example) : Add this to your your spec helper (thanks José)

class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil

def self.connection
@@shared_connection || retrieve_connection
end
end

Forces all threads to share the same connection. This works on

Capybara because it starts the web server in a thread.

ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment