<commit_before>require "capybara-screenshot"

Before do |scenario|
  Capybara::Screenshot.final_session_name = nil
end

After do |scenario|
  if Capybara::Screenshot.autosave_on_failure && scenario.failed?
    Capybara.using_session(Capybara::Screenshot.final_session_name) do
      filename_prefix = Capybara::Screenshot.filename_prefix_for(:cucumber, scenario)

      saver = Capybara::Screenshot.new_saver(Capybara, Capybara.page, true, filename_prefix)
      saver.save
      saver.output_screenshot_path

      # Trying to embed the screenshot into our output."
      if File.exist?(saver.screenshot_path)
        require "base64"
        #encode the image into it's base64 representation
        image = open(saver.screenshot_path, 'rb') {|io|io.read}
        saver.display_image
        #this will embed the image in the HTML report, embed() is defined in cucumber
        encoded_img = Base64.encode64(image)

        # cucumber5 deprecates embed in favor of attach
        if respond_to? :attach
          attach(encoded_img, 'image/png')
        else
          embed(encoded_img, 'image/png;base64', "Screenshot of the error")
        end
      end
    end
  end
end
<commit_msg>fix embedding screenshots into cucumber HTML reports<commit_after>require "capybara-screenshot"

Before do |scenario|
  Capybara::Screenshot.final_session_name = nil
end

After do |scenario|
  if Capybara::Screenshot.autosave_on_failure && scenario.failed?
    Capybara.using_session(Capybara::Screenshot.final_session_name) do
      filename_prefix = Capybara::Screenshot.filename_prefix_for(:cucumber, scenario)

      saver = Capybara::Screenshot.new_saver(Capybara, Capybara.page, true, filename_prefix)
      saver.save
      saver.output_screenshot_path

      # Trying to embed the screenshot into our output."
      if File.exist?(saver.screenshot_path)
        image = open(saver.screenshot_path, 'rb') {|io|io.read}
        saver.display_image

        # cucumber5 deprecates embed in favor of attach
        if respond_to? :attach
          #this will embed the image in the HTML report, attach() is defined in cucumber
          attach(image, 'image/png')
        else
          #encode the image into it's base64 representation
          require "base64"
          encoded_img = Base64.encode64(image)
          #this will embed the image in the HTML report, embed() is defined in cucumber
          embed(encoded_img, 'image/png;base64', "Screenshot of the error")
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.08054226475279107, 'fraction_numerical': 0.008771929824561403, 'mean_word_length': 3.202680067001675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2745949', 'n_tokens_mistral': 812, 'n_tokens_neox': 770, 'n_words': 200}