Debugging Capybara: screenshot of error page on CircleCI

来源:互联网 时间:1970-01-01


CircleCI has a concept of $CIRCLE_ARTIFACTS folder. You can put any file into this folder, and it will show all files in this folder after build is finished.

Configuring spec_helper.rb

Put this snippet into spec_helper.rb file(RSpec 3):

Ruby

RSpec.configure do |config| # ... other configuration options def save_timestamped_screenshot(page, meta) filename = File.basename(meta[:file_path]) line_number = meta[:line_number] time_now = Time.now timestamp = "#{time_now.strftime('%Y-%m-%d-%H-%M-%S.')}#{'%03d' % (time_now.usec/1000).to_i}" screenshot_name = "screenshot-#{filename}-#{line_number}-#{timestamp}.png" screenshot_path = "#{ENV.fetch('CIRCLE_ARTIFACTS', Rails.root.join('tmp/capybara'))}/#{screenshot_name}" page.save_screenshot(screenshot_path) puts "/n Screenshot: #{screenshot_path}" end config.after(:each) do |example| if example.metadata[:js] save_timestamped_screenshot(Capybara.page, example.metadata) if example.exception end end # ... other configuration optionsend RSpec.configuredo|config| # ... other configuration options defsave_timestamped_screenshot(page,meta) filename=File.basename(meta[:file_path]) line_number=meta[:line_number] time_now=Time.now timestamp="#{time_now.strftime('%Y-%m-%d-%H-%M-%S.')}#{'%03d' % (time_now.usec/1000).to_i}" screenshot_name="screenshot-#{filename}-#{line_number}-#{timestamp}.png" screenshot_path="#{ENV.fetch('CIRCLE_ARTIFACTS', Rails.root.join('tmp/capybara'))}/#{screenshot_name}" page.save_screenshot(screenshot_path) puts"/n Screenshot: #{screenshot_path}" end config.after(:each)do|example| ifexample.metadata[:js] save_timestamped_screenshot(Capybara.page,example.metadata)ifexample.exception end end # ... other configuration optionsend

In RSpec 2 example is implicit parameter, so config.after(:each) should be slightly different:

Ruby

config.after(:each) do if example.metadata[:js] save_timestamped_screenshot(Capybara.page, example.metadata) if example.exception end end config.after(:each)do ifexample.metadata[:js] save_timestamped_screenshot(Capybara.page,example.metadata)ifexample.exception end end

This configuration should work for webkit , selenium , and poltergeist drivers.

Usage

Running within CircleCI environment this script will save all screenshots with integration specs failures in the $CIRCLE_ARTIFACTS folder. Here is how it looks like:

You can open the link and see how scenario failed on the CI.

For development environment it stores all screenshots within tmp/capybara of your Rails root folder, and outputs “Screenshot path-to-file” into the console, so it’s easy to find and check it. On the Mac it’s just:

$ open path-to-file $openpath-to-file

That’s it, folks! Hope, you will save a lot of debugging hours with this simple trick.



相关阅读:
Top