sj26 / rspec_junit_formatter

RSpec results that your CI can read
http://rubygems.org/gems/rspec_junit_formatter
MIT License
302 stars 122 forks source link

Add screenshot in the JUnit output for system tests #103

Closed matteeyah closed 1 year ago

matteeyah commented 1 year ago

Summary

RSpec system tests produce a failure screenshot. The path of the screenshot is printed out when the test fails.

Proposal

Parse the test output and grab the path to the screenshot and output it the JUnit XML output.

aiomaster commented 1 year ago

You can configure it the hacky way if you like. I wanted to have my screenshots made with the rails screenshot helper easily accessable in Gitlab-CI, so I did the following:

module ScreenshotForCiHelper
  def take_screenshot
    super
    @screenshot_image_path = absolute_image_path.delete_prefix(Rails.root.to_s)
  end
end

RSpec.configure do |config|
  config.include ScreenshotForCiHelper, type: :system
  config.around(:each, type: :system) do |example|
    @screenshot_image_path = nil
    example.run
    example.metadata[:stdout] = "[[ATTACHMENT|#{@screenshot_image_path}]]" unless @screenshot_image_path.nil?
  end
end

Now everytime a screenshot is taken, its path is saved in the JUinit XML output like gitlab needs it. See: https://docs.gitlab.com/ee/ci/testing/unit_test_reports.html#view-junit-screenshots-on-gitlab

sj26 commented 1 year ago

This gem is for outputting junit compatible xml. There is nothing in that standard for outputting screenshots like this. I'd suggest moving to a dedicated format that can have attachments, or writing an extension to integration your screenshot tool with rspec output :-)