Module: Screenshot

Defined in:
lib/screenshot.rb,
lib/screenshot/version.rb

Constant Summary collapse

VERSION =
'0.0.7'

Class Method Summary collapse

Class Method Details

.bottom_right_x_coordinates(input_rectangles) ⇒ Object



75
76
77
78
79
# File 'lib/screenshot.rb', line 75

def self.bottom_right_x_coordinates(input_rectangles)
  input_rectangles.collect do |rectangle|
    rectangle[0] + rectangle[2]
  end
end

.bottom_right_x_y(input_rectangles) ⇒ Object



87
88
89
# File 'lib/screenshot.rb', line 87

def self.bottom_right_x_y(input_rectangles)
  [bottom_right_x_coordinates(input_rectangles).max, bottom_right_y_coordinates(input_rectangles).max]
end

.bottom_right_y_coordinates(input_rectangles) ⇒ Object



81
82
83
84
85
# File 'lib/screenshot.rb', line 81

def self.bottom_right_y_coordinates(input_rectangles)
  input_rectangles.collect do |rectangle|
    rectangle[1] + rectangle[3]
  end
end

.capture(browser, file_name, page_elements, padding = 0) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/screenshot.rb', line 4

def self.capture(browser, file_name, page_elements, padding = 0)
  screenshot_directory = ENV['LANGUAGE_SCREENSHOT_PATH'] || 'screenshots'
  FileUtils.mkdir_p screenshot_directory
  screenshot_path = "#{screenshot_directory}/#{file_name}"

  browser.screenshot.save screenshot_path
  self.crop_image screenshot_path, page_elements, padding
end

.coordinates_from_page_element(page_element) ⇒ Object



59
60
61
# File 'lib/screenshot.rb', line 59

def self.coordinates_from_page_element(page_element)
  [page_element.element.wd.location.x, page_element.element.wd.location.y, page_element.element.wd.size.width, page_element.element.wd.size.height]
end

.coordinates_from_page_elements(page_elements) ⇒ Object



53
54
55
56
57
# File 'lib/screenshot.rb', line 53

def self.coordinates_from_page_elements(page_elements)
  page_elements.collect do |page_element|
    coordinates_from_page_element page_element
  end
end

.crop_image(path, page_elements, padding) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/screenshot.rb', line 21

def self.crop_image(path, page_elements, padding)
  rectangles = self.coordinates_from_page_elements(page_elements)
  crop_rectangle = rectangle(rectangles, padding)

  top_left_x = crop_rectangle[0]
  top_left_y = crop_rectangle[1]
  width = crop_rectangle[2]
  height = crop_rectangle[3]

  require 'chunky_png'
  image = ChunkyPNG::Image.from_file path

  # It happens with some elements that an image goes off the screen a bit,
  # and chunky_png fails when this happens
  width = image.width - top_left_x if image.width < top_left_x + width

  image.crop!(top_left_x, top_left_y, width, height)
  image.save path
end

.highlight(current_page, element, color = '#FF00FF') ⇒ Object



95
96
97
# File 'lib/screenshot.rb', line 95

def self.highlight(current_page, element, color = '#FF00FF')
  current_page.execute_script("arguments[0].style.border = 'thick solid #{color}'", element)
end

.rectangle(rectangles, padding = 0) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/screenshot.rb', line 41

def self.rectangle(rectangles, padding = 0)
  top_left_x, top_left_y = top_left_x_y rectangles
  bottom_right_x, bottom_right_y = bottom_right_x_y rectangles

  # Finding width and height
  width = bottom_right_x - top_left_x
  height = bottom_right_y - top_left_y

  # The new rectangle is constructed with all the co-ordinates calculated above
  [top_left_x - padding, top_left_y - padding, width + padding * 2, height + padding * 2]
end

.top_left_x_coordinates(input_rectangles) ⇒ Object



63
64
65
66
67
# File 'lib/screenshot.rb', line 63

def self.top_left_x_coordinates(input_rectangles)
  input_rectangles.collect do |rectangle|
    rectangle[0]
  end
end

.top_left_x_y(input_rectangles) ⇒ Object



91
92
93
# File 'lib/screenshot.rb', line 91

def self.top_left_x_y(input_rectangles)
  [top_left_x_coordinates(input_rectangles).min, top_left_y_coordinates(input_rectangles).min]
end

.top_left_y_coordinates(input_rectangles) ⇒ Object



69
70
71
72
73
# File 'lib/screenshot.rb', line 69

def self.top_left_y_coordinates(input_rectangles)
  input_rectangles.collect do |rectangle|
    rectangle[1]
  end
end

.zoom_browser(browser, rate) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/screenshot.rb', line 13

def self.zoom_browser(browser, rate)
  command_key = (/darwin/ =~ RUBY_PLATFORM) ? :command : :control
  rate.abs.times do
    direction = rate > 0 ? :add : :subtract
    browser.send_keys [command_key, direction]
  end
end