snap-diff / snap_diff-capybara

Track changes in Capybara screenshots
MIT License
56 stars 14 forks source link

Support for raw images? #126

Open kwerle opened 2 weeks ago

kwerle commented 2 weeks ago

I am interested in using this gem, but we also have some unit tests that return raw images. Is it possible/easy to incorporate this gem into tests like:

  image_fixture_path = file_fixture("expected_images/image_for_this_test.png")
  actual_image = some.internal_api_call
  expect_actual_image_to_match(actual_image, image_fixture_path)
pftg commented 2 weeks ago

Yes, I’ll share later how I did it

Thanks, Paul

On Wed 28. Aug 2024 at 20:46, Kurt Werle @.***> wrote:

I am interested in using this gem, but we also have some unit tests that return raw images. Is it possible/easy to incorporate this gem into tests like:

image_fixture_path = file_fixture("expected_images/image_for_this_test.png") actual_image = some.internal_api_call expect_actual_image_to_match(actual_image, image_fixture_path)

— Reply to this email directly, view it on GitHub https://github.com/snap-diff/snap_diff-capybara/issues/126, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA6WE2KL5RQCQNEAYQXWCDZTYLJNAVCNFSM6AAAAABNI4DZPWVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQ4TENZXGM2DSNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

pftg commented 2 weeks ago

@kwerle, here is:


  def assert_not_different_images(last_report, previous_report)
    require 'capybara/screenshot/diff/image_compare'
    time_details_block = Region.from_edge_coordinates(60.0, 130.0, 130.0, 150.0)
    compare = Capybara::Screenshot::Diff::ImageCompare.new(
      last_report,
      previous_report,
      skip_area: [time_details_block],
      driver: :vips
    )

    assert_not compare.different?, compare.error_message
  end

and here is some example how I used it:

 def test_downloads_assessment_summary
    Capybara.using_wait_time(Capybara.default_max_wait_time * 5) do
      page.driver.browser.timeout = page.driver.browser.timeout * 5
      click_on 'Download Report'
    end

    downloaded_report = downloaded_file_path('JetThoughts.com - AssessmentCompany.pdf')

    assert_predicate downloaded_report, :exist?, 'should download assessment summary report'

    if Capybara::Screenshot.enabled
      last_report = store_downloaded_file(downloaded_report)

      previous_report = fixture_file('JetThoughts.com - AssessmentCompany.pdf')

      assert_not_different_images(last_report, previous_report)
    end
  ensure
    FileUtils.rm(downloaded_file_path('JetThoughts.com - AssessmentCompany.pdf'), force: true)
  end