Class: OnlyofficeWebdriverWrapper::HeadlessHelper

Inherits:
Object
  • Object
show all
Includes:
HeadlessVideoRecorder, RealDisplayTools, RubyHelper
Defined in:
lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb

Overview

Class for using headless gem

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RubyHelper

#debug?, #os_64_bit?

Methods included from RealDisplayTools

#real_display_connected?, #xrandr_result

Methods included from HeadlessVideoRecorder

#recorded_video_file, #start_capture, #stop_capture

Constructor Details

#initialize(resolution_x: 1680, resolution_y: 1050, record_video: true) ⇒ HeadlessHelper

Returns a new instance of HeadlessHelper.



24
25
26
27
28
29
30
# File 'lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb', line 24

def initialize(resolution_x: 1680,
               resolution_y: 1050,
               record_video: true)
  @resolution_x = resolution_x
  @resolution_y = resolution_y
  @record_video = record_video
end

Instance Attribute Details

#headless_instanceHeadless

Returns instance of headless object.

Returns:

  • (Headless)

    instance of headless object



16
17
18
# File 'lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb', line 16

def headless_instance
  @headless_instance
end

#record_videoTrue, False (readonly)

Returns is video should be recorded.

Returns:

  • (True, False)

    is video should be recorded



22
23
24
# File 'lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb', line 22

def record_video
  @record_video
end

#resolution_xInteger

Returns x resolution of virtual screen.

Returns:

  • (Integer)

    x resolution of virtual screen



18
19
20
# File 'lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb', line 18

def resolution_x
  @resolution_x
end

#resolution_yInteger

Returns y resolution of virtual screen.

Returns:

  • (Integer)

    y resolution of virtual screen



20
21
22
# File 'lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb', line 20

def resolution_y
  @resolution_y
end

Instance Method Details

#running?Boolean

Check if headless session currently running

Returns:

  • (Boolean)

    result of this check



81
82
83
# File 'lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb', line 81

def running?
  !headless_instance.nil?
end

#should_start?True, False

Check if should start headless

Returns:

  • (True, False)

    result



34
35
36
37
38
39
# File 'lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb', line 34

def should_start?
  return false if debug?
  return false if OSHelper.mac?

  true
end

#startvoid

This method returns an undefined value.

Start headless session



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb', line 43

def start
  create_session = if real_display_connected?
                     should_start?
                   else
                     true
                   end
  return unless create_session

  OnlyofficeLoggerHelper.log('Starting Headless Session')
  begin
    @headless_instance = Headless.new(reuse: false,
                                      destroy_at_exit: true,
                                      dimensions: "#{@resolution_x + 1}x#{@resolution_y + 1}x24",
                                      video: { provider: :ffmpeg })
  rescue StandardError => e
    OnlyofficeLoggerHelper.log("xvfb not started with problem #{e}")
    WebDriver.clean_up(true)
    @headless_instance = Headless.new(reuse: false,
                                      destroy_at_exit: true,
                                      dimensions: "#{@resolution_x + 1}x#{@resolution_y + 1}x24",
                                      video: { provider: :ffmpeg })
  end
  headless_instance.start
  start_capture
end

#stopvoid

This method returns an undefined value.

Stop current headless session



71
72
73
74
75
76
77
# File 'lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb', line 71

def stop
  return unless running?

  OnlyofficeLoggerHelper.log('Stopping Headless Session')
  stop_capture
  headless_instance.destroy
end

#take_screenshot(scr_path = '/tmp/screenshot.png') ⇒ void

This method returns an undefined value.

Take a screenshot of current headless session

Parameters:

  • scr_path (String) (defaults to: '/tmp/screenshot.png')

    Path to store screenshot



88
89
90
91
92
93
# File 'lib/onlyoffice_webdriver_wrapper/helpers/headless_helper.rb', line 88

def take_screenshot(scr_path = '/tmp/screenshot.png')
  return unless running?

  headless_instance.take_screenshot(scr_path)
  OnlyofficeLoggerHelper.log("Took Screenshot to file: #{scr_path}")
end