Module: QAT::Web::Browser::Screenshot

Extended by:
Screenshot
Includes:
Logger
Included in:
Screenshot
Defined in:
lib/qat/web/browser/screenshot.rb

Overview

Module to handle browser screenshots.

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#read_screenshot_file(page, image_path) ⇒ Object

Helper for reading file, in cucumber 6 this could be reverted to path directly

Since:

  • 1.0.0



31
32
33
34
# File 'lib/qat/web/browser/screenshot.rb', line 31

def read_screenshot_file page, image_path
  file            = page.save_screenshot image_path
  File.open file
end

#screenshot_filenameObject

Since:

  • 1.0.0



36
37
38
# File 'lib/qat/web/browser/screenshot.rb', line 36

def screenshot_filename
  File.basename(@screenshot_path)
end

#screenshot_pathString

Default screenshot path. Can be set with #screenshot_path=.

Returns:

  • (String)

    Screenshot path

Since:

  • 1.0.0



43
44
45
# File 'lib/qat/web/browser/screenshot.rb', line 43

def screenshot_path
  @screenshot_path || ::File.join('public', "browser_#{Time.new.strftime("%H%M%S%L")}.png")
end

#screenshot_path=(value) ⇒ Object

Set new default screenshot path.

Parameters:

  • value (String)

    Screenshot path

Since:

  • 1.0.0



50
51
52
# File 'lib/qat/web/browser/screenshot.rb', line 50

def screenshot_path= value
  @screenshot_path = value
end

#take_screenshot(page = Capybara.current_session, path = screenshot_path) ⇒ String/NilClass

Function to take a browser screenshot. Will handle the usage of driver that don’t support screenshots.

Parameters:

  • page (Capybara::Session) (defaults to: Capybara.current_session)

    Browser to take screenshot from

  • path (String) (defaults to: screenshot_path)

    File path to save screenshot file

Returns:

  • (String/NilClass)

    File path to where the screenshot file was saved or nil if the browser doesn’t support screenshots

Since:

  • 1.0.0



19
20
21
22
23
24
25
26
27
28
# File 'lib/qat/web/browser/screenshot.rb', line 19

def take_screenshot page = Capybara.current_session ,  path = screenshot_path
  log.info { "Saving screenshot to #{path}" }
  raise ArgumentError.new "File #{path} already exists! Choose another filename" if ::File.exist? path
  path = read_screenshot_file page, path
  log.info { "Screenshot available" }
  path
rescue Capybara::NotSupportedByDriverError
  log.warn {"Driver #{page.mode} does not support screenshots!"}
  return nil
end