-
-
Save sb8244/55246c51e469524f2abd0c17dd3c574e to your computer and use it in GitHub Desktop.
Capybara Visual Regression Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Support | |
module ScreenshotHelper | |
def ensure_pixel_perfection | |
PixelPerfect.new(page) | |
end | |
class PixelPerfect | |
attr_reader :page | |
def initialize(page) | |
@page = page | |
end | |
def of(name) | |
@name = name.parameterize | |
self | |
end | |
def using(selector) | |
@selector = selector | |
self | |
end | |
def retina | |
@retina = true | |
self | |
end | |
def call | |
raise ArgumentError unless @name && @selector | |
image = screenshot_element | |
if existing_image_path.exist? | |
diff_image, pixels_changed = existing_image.compare_channel(image, Magick::AbsoluteErrorMetric) | |
percent_change = pixels_changed / (existing_image.rows * existing_image.columns) * 100 | |
if pixels_changed > 0 | |
diff_image.write(diff_path) | |
raise "ScreenshotHelper: `#{@selector}` had %#{percent_change.round(2)} pixel change. A diff has been saved to #{diff_path}" | |
end | |
else | |
FileUtils.copy(image_path, existing_image_path) | |
end | |
ensure | |
image_path.delete | |
end | |
private | |
def image_path | |
Rails.root.join("tmp", "#{@name}.png") | |
end | |
def diff_path | |
Rails.root.join("tmp", "diff-#{@name}.png") | |
end | |
def existing_image_path | |
Rails.root.join("regression_specs", "fixtures", "#{@name}.png") | |
end | |
def existing_image | |
Magick::Image.read(existing_image_path)[0] | |
end | |
def screenshot_element(padding: 25) | |
el_count = page.all(@selector).count | |
raise "ScreenshotHelper: #{count} elements found on the page for `#{@selector}`" unless el_count == 1 | |
el = page.find(@selector) | |
location = el.native.location | |
size = el.native.size | |
if @retina | |
location.x *= 2 | |
location.y *= 2 | |
size.width *= 2 | |
size.height *= 2 | |
padding *= 2 | |
end | |
image = page.save_screenshot(image_path) | |
cropped_image = Magick::Image.read(image)[0].crop(location.x - padding/2, location.y - padding/2, size.width + padding, size.height + padding) | |
cropped_image.write(image) | |
cropped_image | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should line 68 be interpolating
el_count
as opposed tocount
?Also I had to edit line 45 to be
image_path.delete if File.file?(image_path)
. I had a new image that didn't have a screenshot saved and matched 2 elements in the dom. So it was triggering the raise on line 68, but I wasn't actually seeing that error message because it was failing again in the ensure since didn't exist to delete.Errno::ENOENT: No such file or directory @ dir_s_rmdir - /Users/brian/src/project/tmp/person-row.png