Module: AutoBrowse::Pagelike
- Included in:
- Page
- Defined in:
- lib/auto_browse/page.rb
Instance Method Summary collapse
-
#bounding_box(element) ⇒ Object
returns [top, left, bottom, right] of element.
-
#coords(element) ⇒ Object
returns [x, y] of element.
-
#coords_dimensions(element) ⇒ Object
returns [x, y, width, height] of element.
- #delete_temp_screenshot ⇒ Object
-
#dimensions(element) ⇒ Object
returns [width, height] of element.
-
#element_screenshot(element, output_file_path, containing_frame_offset_coords = []) ⇒ Object
containing_frame_offset_coords is an array of the form: [ [outer_frame.x, outer_frame.y], [inner_frame.x, inner.frame.y] ] that lists the [top, left] cords of all (if any) frames that contain the given element.
-
#full_page_screenshot(path) ⇒ Object
this method will only work if the browser window has focus; otherwise, this method will raise a timeout exception.
- #goto(product_url) ⇒ Object
- #mouse ⇒ Object
- #move(x, y) ⇒ Object
- #page ⇒ Object
- #scroll_to(*args, **options) ⇒ Object
- #temp_screenshot(path = "./tmp_screenshot.png") ⇒ Object
-
#viewport_screenshot(path) ⇒ Object
this method will only work if the browser window has focus; otherwise, this method will raise a timeout exception.
-
#with_temp_screenshot(temp_screenshot_path = "./tmp_screenshot.png", &blk) ⇒ Object
example: with_temp_screenshot do |tmp_path| puts "temp screenshot is at #tmp_path" end.
Instance Method Details
#bounding_box(element) ⇒ Object
returns [top, left, bottom, right] of element
30 31 32 33 34 35 36 |
# File 'lib/auto_browse/page.rb', line 30 def bounding_box(element) top = element.evaluate_script("this.getBoundingClientRect().top;").to_i left = element.evaluate_script("this.getBoundingClientRect().left;").to_i bottom = element.evaluate_script("this.getBoundingClientRect().bottom;").to_i right = element.evaluate_script("this.getBoundingClientRect().right;").to_i [top, left, bottom, right] end |
#coords(element) ⇒ Object
returns [x, y] of element
39 40 41 42 |
# File 'lib/auto_browse/page.rb', line 39 def coords(element) top, left, bottom, right = *bounding_box(element) [left, top] end |
#coords_dimensions(element) ⇒ Object
returns [x, y, width, height] of element
53 54 55 56 57 58 |
# File 'lib/auto_browse/page.rb', line 53 def coords_dimensions(element) top, left, bottom, right = *bounding_box(element) height = bottom - top width = right - left [left, top, width, height] end |
#delete_temp_screenshot ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/auto_browse/page.rb', line 110 def delete_temp_screenshot if @last_screenshot && @last_screenshot_path && File.exist?(@last_screenshot_path) File.delete(@last_screenshot_path) @last_screenshot_path = nil @last_screenshot = nil end end |
#dimensions(element) ⇒ Object
returns [width, height] of element
45 46 47 48 49 50 |
# File 'lib/auto_browse/page.rb', line 45 def dimensions(element) top, left, bottom, right = *bounding_box(element) height = bottom - top width = right - left [width, height] end |
#element_screenshot(element, output_file_path, containing_frame_offset_coords = []) ⇒ Object
containing_frame_offset_coords is an array of the form: [ [outer_frame.x, outer_frame.y], [inner_frame.x, inner.frame.y] ] that lists the [top, left] cords of all (if any) frames that contain the given element
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/auto_browse/page.rb', line 72 def element_screenshot(element, output_file_path, containing_frame_offset_coords = []) raise "A temporary screenshot must be taken before trying to capture an element screenshot." unless @last_screenshot x, y, width, height = *coords_dimensions(element) frame_offset_x, frame_offset_y = *containing_frame_offset_coords.reduce([0, 0]) {|memo, coord_pair| [ memo[0] + coord_pair[0], memo[1] + coord_pair[1] ] } page_x = frame_offset_x + x page_y = frame_offset_y + y # this imagemagick command performs the crop on the full screenshot # convert images/captcha/20210128_161014/tiles.png -crop 95x95+123+154 +repage images/captcha/20210128_161014/tile0.png image = MiniMagick::Image.open(@last_screenshot_path) image. do |cmd| cmd.crop "#{width}x#{height}+#{page_x}+#{page_y}" cmd.repage.+ end FileUtils.mkdir_p(File.dirname(output_file_path)) image.write(output_file_path) end |
#full_page_screenshot(path) ⇒ Object
this method will only work if the browser window has focus; otherwise, this method will raise a timeout exception
66 67 68 |
# File 'lib/auto_browse/page.rb', line 66 def full_page_screenshot(path) page.save_screenshot(path, full: true) # this delegates to https://github.com/rubycdp/ferrum/blob/3a2dc276ba312831487b05cb6e176cae5a7375a4/lib/ferrum/page/screenshot.rb#L31 end |
#goto(product_url) ⇒ Object
25 26 27 |
# File 'lib/auto_browse/page.rb', line 25 def goto(product_url) page.visit(product_url) end |
#mouse ⇒ Object
17 18 19 |
# File 'lib/auto_browse/page.rb', line 17 def mouse browser.mouse end |
#move(x, y) ⇒ Object
21 22 23 |
# File 'lib/auto_browse/page.rb', line 21 def move(x, y) browser.move(x, y) end |
#page ⇒ Object
9 10 11 |
# File 'lib/auto_browse/page.rb', line 9 def page browser.driver end |
#scroll_to(*args, **options) ⇒ Object
13 14 15 |
# File 'lib/auto_browse/page.rb', line 13 def scroll_to(*args, **) page.scroll_to(*args, **) end |
#temp_screenshot(path = "./tmp_screenshot.png") ⇒ Object
103 104 105 106 107 108 |
# File 'lib/auto_browse/page.rb', line 103 def temp_screenshot(path = "./tmp_screenshot.png") delete_temp_screenshot if @last_screenshot @last_screenshot_path = path @last_screenshot = (@last_screenshot_path) end |
#viewport_screenshot(path) ⇒ Object
this method will only work if the browser window has focus; otherwise, this method will raise a timeout exception
61 62 63 |
# File 'lib/auto_browse/page.rb', line 61 def (path) page.save_screenshot(path, full: false) # this delegates to https://github.com/rubycdp/ferrum/blob/3a2dc276ba312831487b05cb6e176cae5a7375a4/lib/ferrum/page/screenshot.rb#L31 end |
#with_temp_screenshot(temp_screenshot_path = "./tmp_screenshot.png", &blk) ⇒ Object
example: with_temp_screenshot do |tmp_path| puts "temp screenshot is at #tmp_path" end
97 98 99 100 101 |
# File 'lib/auto_browse/page.rb', line 97 def with_temp_screenshot(temp_screenshot_path = "./tmp_screenshot.png", &blk) temp_screenshot(temp_screenshot_path) blk.call(@last_screenshot_path) delete_temp_screenshot end |