Module: OnlyofficeWebdriverWrapper::WebdriverScreenshotHelper

Included in:
WebDriver
Defined in:
lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_screenshot_helper.rb

Overview

Working with screenshots

Constant Summary collapse

SCREENSHOT_CONTENT_TYPE =

Returns content type of png image.

Returns:

  • (String)

    content type of png image

'image/png'

Instance Method Summary collapse

Instance Method Details

#amazon_s3_wrapperOnlyofficeS3Wrapper::AmazonS3Wrapper

Returns s3 wrapper.

Returns:

  • (OnlyofficeS3Wrapper::AmazonS3Wrapper)

    s3 wrapper



12
13
14
15
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_screenshot_helper.rb', line 12

def amazon_s3_wrapper
  @amazon_s3_wrapper ||= OnlyofficeS3Wrapper::AmazonS3Wrapper.new(bucket_name: 'webdriver-wrapper-screenshots',
                                                                  region: 'us-east-1')
end

#get_screenshot(path_to_screenshot = "#{screenshot_folder}/#{StringHelper.generate_random_string}.png") ⇒ void

This method returns an undefined value.

Get screenshot of current window

Parameters:

  • path_to_screenshot (String) (defaults to: "#{screenshot_folder}/#{StringHelper.generate_random_string}.png")

    place to store local screenshot



48
49
50
51
52
53
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_screenshot_helper.rb', line 48

def get_screenshot(path_to_screenshot = "#{screenshot_folder}/#{StringHelper.generate_random_string}.png")
  screenshot_folder = File.dirname(path_to_screenshot)
  FileUtils.mkdir_p(screenshot_folder) unless File.directory?(screenshot_folder)
  @driver.save_screenshot(path_to_screenshot)
  OnlyofficeLoggerHelper.log("get_screenshot(#{path_to_screenshot})")
end

#get_screenshot_and_upload(path_to_screenshot = nil) ⇒ String

Get screenshot of current windows and upload it to cloud storage

Parameters:

  • path_to_screenshot (String) (defaults to: nil)

    place to store local screenshot

Returns:

  • (String)

    url of public screenshot



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_screenshot_helper.rb', line 25

def get_screenshot_and_upload(path_to_screenshot = nil)
  path_to_screenshot ||= "#{screenshot_folder}/#{StringHelper.generate_random_string}.png"
  begin
    get_screenshot(path_to_screenshot)
    cloud_screenshot = publish_screenshot(path_to_screenshot)
    FileUtils.rm_rf(path_to_screenshot)
    OnlyofficeLoggerHelper.log("upload screenshot: #{cloud_screenshot}")
    return cloud_screenshot
  rescue Errno::ENOENT => e
    begin
      @driver.save_screenshot(path_to_screenshot)
      OnlyofficeLoggerHelper.log("Cant upload screenshot #{path_to_screenshot}. Error: #{e}")
    rescue Errno::ENOENT => e
      @driver.save_screenshot("tmp/#{File.basename(path_to_screenshot)}")
      OnlyofficeLoggerHelper.log("Upload screenshot to tmp/#{File.basename(path_to_screenshot)}. Error: #{e}")
    end
  end
  path_to_screenshot
end

#screenshot_folderString

Returns location of screenshot to take.

Returns:

  • (String)

    location of screenshot to take



18
19
20
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_screenshot_helper.rb', line 18

def screenshot_folder
  '/tmp/screenshot/WebdriverError'
end

#webdriver_screenshot(screenshot_name = SecureRandom.uuid) ⇒ String

Make a screenshot by webdriver methods

Parameters:

  • screenshot_name (String) (defaults to: SecureRandom.uuid)

    random name for file

Returns:

  • (String)

    text string with screenshot file location



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_screenshot_helper.rb', line 58

def webdriver_screenshot(screenshot_name = SecureRandom.uuid)
  begin
    link = get_screenshot_and_upload("#{screenshot_folder}/#{screenshot_name}.png")
  rescue StandardError => e
    OnlyofficeLoggerHelper.log("Error in get screenshot: #{e}. Trying to get headless screenshot")
    if @headless.running?
      @headless.take_screenshot("#{screenshot_folder}/#{screenshot_name}.png")
    else
      system_screenshot("#{screenshot_folder}/#{screenshot_name}.png")
    end
    link = publish_screenshot("#{screenshot_folder}/#{screenshot_name}.png")
  end
  "screenshot: #{link}"
end