Module: Screenshot
- Defined in:
- lib/screenshot.rb,
lib/screenshot/version.rb
Constant Summary collapse
- VERSION =
"0.0.3"
Class Method Summary collapse
- .bottom_right_x_coordinates(input_rectangles) ⇒ Object
- .bottom_right_x_y(input_rectangles) ⇒ Object
- .bottom_right_y_coordinates(input_rectangles) ⇒ Object
- .capture(file_name, page_elements) ⇒ Object
- .rectangle(rectangles, offset_rectangle = [0, 0, 0, 0]) ⇒ Object
- .top_left_x_coordinates(input_rectangles) ⇒ Object
- .top_left_x_y(input_rectangles) ⇒ Object
- .top_left_y_coordinates(input_rectangles) ⇒ Object
- .zoom_browser(rate) ⇒ Object
Instance Method Summary collapse
- #coordinates_from_page_element(page_element) ⇒ Object
- #coordinates_from_page_elements(page_elements) ⇒ Object
- #crop_image(path, page_elements, offset_element) ⇒ Object
- #highlight(element, color = '#FF00FF') ⇒ Object
Class Method Details
.bottom_right_x_coordinates(input_rectangles) ⇒ Object
83 84 85 86 87 |
# File 'lib/screenshot.rb', line 83 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
95 96 97 |
# File 'lib/screenshot.rb', line 95 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
89 90 91 92 93 |
# File 'lib/screenshot.rb', line 89 def self.bottom_right_y_coordinates(input_rectangles) input_rectangles.collect do |rectangle| rectangle[1] + rectangle[3] end end |
.capture(file_name, page_elements) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/screenshot.rb', line 4 def self.capture(file_name, page_elements) screenshot_directory = ENV['LANGUAGE_SCREENSHOT_PATH'] || 'screenshots' FileUtils.mkdir_p screenshot_directory screenshot_path = "#{screenshot_directory}/#{file_name}" @browser.screenshot.save screenshot_path crop_image screenshot_path, page_elements, nil end |
.rectangle(rectangles, offset_rectangle = [0, 0, 0, 0]) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/screenshot.rb', line 45 def self.rectangle(rectangles, offset_rectangle = [0, 0, 0, 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 # We are calculating the offset co-ordinates x_offset = offset_rectangle[0] y_offset = offset_rectangle[1] # The new rectangle is constructed with all the co-ordinates calculated above [top_left_x + x_offset, top_left_y + y_offset, width, height] end |
.top_left_x_coordinates(input_rectangles) ⇒ Object
71 72 73 74 75 |
# File 'lib/screenshot.rb', line 71 def self.top_left_x_coordinates(input_rectangles) input_rectangles.collect do |rectangle| rectangle[0] end end |
.top_left_x_y(input_rectangles) ⇒ Object
99 100 101 |
# File 'lib/screenshot.rb', line 99 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
77 78 79 80 81 |
# File 'lib/screenshot.rb', line 77 def self.top_left_y_coordinates(input_rectangles) input_rectangles.collect do |rectangle| rectangle[1] end end |
.zoom_browser(rate) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/screenshot.rb', line 13 def self.zoom_browser(rate) rate.abs.times do direction = rate > 0 ? :add : :subtract @browser.send_keys [:control, direction] end end |
Instance Method Details
#coordinates_from_page_element(page_element) ⇒ Object
67 68 69 |
# File 'lib/screenshot.rb', line 67 def 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
61 62 63 64 65 |
# File 'lib/screenshot.rb', line 61 def 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, offset_element) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/screenshot.rb', line 20 def crop_image(path, page_elements, offset_element) if offset_element offset_rectangle = coordinates_from_page_element(offset_element) else offset_rectangle = [0, 0, 0, 0] end rectangles = coordinates_from_page_elements(page_elements) crop_rectangle = rectangle(rectangles, offset_rectangle) 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(element, color = '#FF00FF') ⇒ Object
103 104 105 |
# File 'lib/screenshot.rb', line 103 def highlight(element, color = '#FF00FF') @current_page.execute_script("arguments[0].style.border = 'thick solid #{color}'", element) end |