Welcome to mirror list, hosted at ThFree Co, Russian Federation.

docs_screenshot_helpers.rb « helpers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aa3aad0a7401febe2ac3021096ac09a520317506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

require 'fileutils'
require 'mini_magick'

module DocsScreenshotHelpers
  extend ActiveSupport::Concern

  def set_crop_data(element, padding)
    @crop_element = element
    @crop_padding = padding
  end

  def crop_image_screenshot(path)
    element_rect = @crop_element.evaluate_script("this.getBoundingClientRect()")

    width = element_rect['width'] + (@crop_padding * 2)
    height = element_rect['height'] + (@crop_padding * 2)

    x = element_rect['x'] - @crop_padding
    y = element_rect['y'] - @crop_padding

    image = MiniMagick::Image.new(path)
    image.crop "#{width}x#{height}+#{x}+#{y}"
  end

  included do |base|
    after do |example|
      filename = "#{example.description}.png"
      path = File.expand_path(filename, 'doc/')
      page.save_screenshot(path)

      if @crop_element
        crop_image_screenshot(path)
        set_crop_data(nil, nil)
      end
    end
  end
end