rajatthareja/ReportBuilder

Encoding issue

Closed this issue ยท 7 comments

I have an issue when converting json reports.

Code:

ReportBuilder.configure do |config|
      config.report_title = 'test'
      config.report_types = %i[json]
      config.input_path = /path/to/the/report/folder
      config.json_report_path = final_report
    end
    ReportBuilder.build_report

report_builder 1.8
ruby 2.5.0
Example json file:
https://raw.githubusercontent.com/faceless7171/report_builder_bug/master/test.json
Error:

Traceback (most recent call last):
	8: from /home/user/project/auto-tests/runner.rb:210:in `<main>'
	7: from /home/user/project/auto-tests/runner.rb:57:in `run'
	6: from /var/lib/gems/2.5.0/gems/report_builder-1.8/lib/report_builder.rb:76:in `build_report'
	5: from /var/lib/gems/2.5.0/gems/report_builder-1.8/lib/report_builder/builder.rb:26:in `build_report'
	4: from /var/lib/gems/2.5.0/gems/report_builder-1.8/lib/report_builder/builder.rb:26:in `open'
	3: from /var/lib/gems/2.5.0/gems/report_builder-1.8/lib/report_builder/builder.rb:27:in `block in build_report'
	2: from /usr/lib/ruby/2.5.0/json/common.rb:286:in `pretty_generate'
	1: from /usr/lib/ruby/2.5.0/json/common.rb:286:in `generate'
/usr/lib/ruby/2.5.0/json/common.rb:286:in `encode': "\x89" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)

It looks like by default cucumber using ASCII-8BIT encoding for image files attached to the report and report_builder is trying to decode it to the UTF-8.
Code from cucumber json formatter:

def embed(src, mime_type, _label)
        if File.file?(src)
          content = File.open(src, 'rb', &:read)
          data = encode64(content)
        else
          if mime_type =~ /;base64$/
            mime_type = mime_type[0..-8]
            data = src
          else
            data = encode64(src)
          end
        end
        test_step_embeddings << { mime_type: mime_type, data: data }
      end

File.open with b read mode:

"b"  |  Binary file mode (may appear with
     |  any of the key letters listed above).
     |  Suppresses EOL <-> CRLF conversion on Windows. And
     |  sets external encoding to ASCII-8BIT unless explicitly
     |  specified.

Same problem here, did you have any workaround for this?

We also have this same problem. We have set Report Builder to generate the html and json reports. We use the html for a single snapshot and the resulting json to aggregate all test runs into metrics on the overall performance of the test suite.

This is a gem of a gem so I would like to hear any solutions to this issue that don't involve removing the embedded images.

@thiagotrentin @PhilipJordan
Try to use this code for embed images:

def add_screenshot
  windows.each do |window|
    within_window window do
      file_path = File.join(InstanceVars.dirs.screenshots, "#{Time.new.strftime('%Y%m%d%H%M%S')}#{rand(10**10)}.png")
      page.driver.browser.save_screenshot file_path
      base64_img = Base64.encode64(File.open(file_path, 'r:UTF-8', &:read))
      embed(base64_img, 'image/png;base64')
    end
  end
end

Let me know if it helps, for me this error is not appearing anymore.

@thiagotrentin @PhilipJordan
Try to use this code for embed images:

def add_screenshot
  windows.each do |window|
    within_window window do
      file_path = File.join(InstanceVars.dirs.screenshots, "#{Time.new.strftime('%Y%m%d%H%M%S')}#{rand(10**10)}.png")
      page.driver.browser.save_screenshot file_path
      base64_img = Base64.encode64(File.open(file_path, 'r:UTF-8', &:read))
      embed(base64_img, 'image/png;base64')
    end
  end
end

Let me know if it helps, for me this error is not appearing anymore.

@faceless7171
I just tried and it worked for me!
In my case, the problem seems to be intermittent so I'll follow this issue for some days and after I'll send you one feedback.
thanks a lot

@thiagotrentin @PhilipJordan
Try to use this code for embed images:

def add_screenshot
  windows.each do |window|
    within_window window do
      file_path = File.join(InstanceVars.dirs.screenshots, "#{Time.new.strftime('%Y%m%d%H%M%S')}#{rand(10**10)}.png")
      page.driver.browser.save_screenshot file_path
      base64_img = Base64.encode64(File.open(file_path, 'r:UTF-8', &:read))
      embed(base64_img, 'image/png;base64')
    end
  end
end

Let me know if it helps, for me this error is not appearing anymore.

@faceless7171
I just tried and it worked for me!
In my case, the problem seems to be intermittent so I'll follow this issue for some days and after I'll send you one feedback.
thanks a lot

@faceless7171 after a few days and a lot of executions I hadn't seen this issue again, so I think your solution works for me too.
thanks for your help!

I had the same problem, but this line resolved:

base64_img = Base64.encode64(File.open(file_path, 'r:UTF-8', &:read))

Thank you for your help!

I had the same problem, but this line resolved:

base64_img = Base64.encode64(File.open(file_path, 'r:UTF-8', &:read))

Thank you for your help!

Hi @mickhill-qa, I had some trouble using this aproach with CucumberReportsPlugin on Jenkins and soved the issue using this code:
attach("data:image/png;base64,#{page.driver.browser.screenshot_as(:base64)}", 'image/png')
Hope it helps