Module: MediawikiSelenium::HeadlessHelper

Defined in:
lib/mediawiki_selenium/helpers/headless_helper.rb

Overview

Adds support to Environment for running sessions in a headless mode using Xvfb. Video will be recorded for the display and saved for failed scenarios if a headless_capture_path environment variable is configured.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_or_reuse_display(env) ⇒ Headless

Creates a global headless display using the given environment's configuration. If a display has already been created once before, it is simply returned.

Parameters:

  • env (Environment)

    Environment for which to start headless.

Returns:

  • (Headless)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mediawiki_selenium/helpers/headless_helper.rb', line 19

def create_or_reuse_display(env)
  return @_display unless @_display.nil?

  options = { video: { provider: :libav, codec: 'libx264' } }

  display = env.lookup(:headless_display, default: nil)
  options[:display] = display unless display.nil?

  if env.lookup(:headless_reuse, default: true).to_s == 'false'
    options[:reuse] = false
  end

  if env.lookup(:headless_destroy_at_exit, default: true).to_s == 'false'
    options[:destroy_at_exit] = false
  end

  @_display = Headless.new(options)
  @_display.start

  @_display
end

.destroy_displayObject

Destroys the global headless display created by create_or_reuse_display.



44
45
46
47
# File 'lib/mediawiki_selenium/helpers/headless_helper.rb', line 44

def destroy_display
  @_display.destroy if @_display
  @_display = nil
end

.display_created?true, false

Whether a global headless display has been created.

Returns:

  • (true, false)


53
54
55
# File 'lib/mediawiki_selenium/helpers/headless_helper.rb', line 53

def display_created?
  !@_display.nil?
end

Instance Method Details

#browserObject

Starts a headless display and starts recording before the Environment opens a browser for the first time.



63
64
65
66
67
68
69
70
71
72
# File 'lib/mediawiki_selenium/helpers/headless_helper.rb', line 63

def browser
  @_headless_display = HeadlessHelper.create_or_reuse_display(self)

  if !@_headless_capture && headless_capture?
    @_headless_capture = true
    @_headless_display.video.start_capture
  end

  super
end

#headless_capture?true, false

Whether or not we should perform video capture of the headless display for each new browser session.

Returns:

  • (true, false)


79
80
81
# File 'lib/mediawiki_selenium/helpers/headless_helper.rb', line 79

def headless_capture?
  !headless_capture_path.nil?
end

#headless_capture_pathString?

Directory where screenshot/video files of headless sessions will be saved. Defaults to writing them to a log directory under the workspace directory.

Returns:

  • (String, nil)


89
90
91
# File 'lib/mediawiki_selenium/helpers/headless_helper.rb', line 89

def headless_capture_path
  lookup(:headless_capture_path, default: nil)
end

#teardown(info = {}) ⇒ Object

Performs teardown tasks for headless operation, saving any video captures to file.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mediawiki_selenium/helpers/headless_helper.rb', line 98

def teardown(info = {})
  artifacts = {}

  begin
    artifacts = super
  ensure
    if @_headless_capture
      if info[:status] == :failed
        dir = File.absolute_path(headless_capture_path)
        FileUtils.mkdir_p(dir)

        filename = "#{(info[:name] || 'scenario').tr("#{File::SEPARATOR}\000", '-')}.mp4"
        filename = File.join(dir, filename)

        @_headless_display.video.stop_and_save(filename)

        artifacts[filename] = 'video/mp4'
      else
        @_headless_display.video.stop_and_discard
      end
    end
  end

  artifacts
end