class Browserly::Screenshot

Attributes

file[R]

Public Class Methods

new(url, height_selector = '') click to toggle source
# File lib/browserly/screenshot.rb, line 8
def initialize(url, height_selector = '')
  @url = url
  @height_selector = height_selector
end

Public Instance Methods

driver() click to toggle source
# File lib/browserly/screenshot.rb, line 17
def driver
  @driver ||= Browserly::Driver.new(Browserly.configuration.remote_driver, Browserly.configuration.chrome_args).driver
end
perform() click to toggle source
# File lib/browserly/screenshot.rb, line 13
def perform
  take! && close_session!
end

Private Instance Methods

close_session!() click to toggle source
# File lib/browserly/screenshot.rb, line 34
def close_session!
  return true if driver.close && driver.quit
rescue => e
  error(:session, e.message)
  false
end
filename() click to toggle source
# File lib/browserly/screenshot.rb, line 53
def filename
  File.join(Browserly.configuration.output_dir, "#{Time.now.to_i}.png")
end
js() click to toggle source
# File lib/browserly/screenshot.rb, line 41
    def js
      <<-JAVASCRIPT
        element = document.getElementById('#{@height_selector}');

        if(element != null) {
          return element.clientHeight;
        } else {
          return null
        };
      JAVASCRIPT
    end
take!() click to toggle source
# File lib/browserly/screenshot.rb, line 23
def take!
  driver.navigate.to @url
  @height = driver.execute_script(js) unless @height_selector.nil? || @height_selector == ''
  @height ||= Browserly.configuration.height
  driver.manage.window.resize_to(Browserly.configuration.width, @height)
  @file = driver.save_screenshot(filename)&.path
  return true if File.exist?(@file)
  error(:file, 'Unable to save file')
  false
end