-
-
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
You are right @l3rl4n