Module: LooksGood::CaptureElement

Extended by:
Configuration
Defined in:
lib/looks_good/capture_element.rb

Class Method Summary collapse

Methods included from Configuration

browser, default_reference_path, path, reference_path_with_browser_folders

Class Method Details

.capture(element) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/looks_good/capture_element.rb', line 8

def self.capture(element)
  # 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)
  screenshot = take_screenshot

  crop_element(screenshot, element, element_position)
end

.crop_element(image, element_to_crop, position) ⇒ Object



41
42
43
# File 'lib/looks_good/capture_element.rb', line 41

def self.crop_element(image, element_to_crop, position)
  cropped_element = image.scale!(LooksGood::Configuration.scale_amount).crop!(position[:x], position[:y], position[:width], position[:height])
end

.get_element_position(element) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/looks_good/capture_element.rb', line 31

def self.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

.take_screenshotObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/looks_good/capture_element.rb', line 18

def self.take_screenshot
  temp_dir = LooksGood::Configuration.path(:tmp)
  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