Sha256: beab3f3ee7c84b3c3c97c6068ade8f5b9159bdb1ed06818a9af502570036a14d

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

require 'fileutils'

module Gatling
  class CaptureElement

    def initialize element_to_capture, *element_to_exclude
      @reference_image_path = Gatling::Configuration.reference_image_path
      @element_to_capture = element_to_capture
      # @element_to_exclude = element_to_exclude.first

    end

    def capture
      # Getting the element position before screenshot because of a side effect
      # of WebDrivers getLocationOnceScrolledIntoView method which scrolls the page
      # regardless of whether the object is in view or not
      element_position = get_element_position @element_to_capture
      screenshot = self.take_screenshot
      screenshot = exclude(screenshot, @element_to_exclude) if @element_to_exclude
      crop_element(screenshot, @element_to_capture, element_position)
    end

    def take_screenshot
      temp_dir = File.join(@reference_image_path, 'temp')
      FileUtils.mkdir_p(temp_dir) unless File.exists?(temp_dir)
      #captures the uncropped full screen
      begin
        temp_screenshot_filename = File.join(temp_dir, "temp-#{Process.pid}.png")
        Capybara.page.driver.browser.save_screenshot(temp_screenshot_filename)
        temp_screenshot = Magick::Image.read(temp_screenshot_filename).first
      rescue
        raise "Could not save screenshot to #{temp_dir}. Please make sure you have permission"
      end
    end

    def get_element_position element
      element = element.native
      position = Hash.new{}
      position[:x] = element.location.x
      position[:y] = element.location.y
      position[:width] = element.size.width
      position[:height] = element.size.height
      position
    end

    def crop_element image, element_to_crop, position
      @cropped_element = image.crop(position[:x], position[:y], position[:width], position[:height])
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gatling-1.1.2 lib/gatling/capture_element.rb
gatling-1.1.1 lib/gatling/capture_element.rb
gatling-1.1.0 lib/gatling/capture_element.rb