Class: TestRecorder::CdpRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/test_recorder/cdp_recorder.rb

Defined Under Namespace

Classes: Record

Constant Summary collapse

FFMPEG_ENCODE_OPTIONS =
%w[-y -an -r 25 -c:v vp8 -qmin 0 -qmax 50 -crf 8 -deadline realtime -speed 8 -b:v 1M -threads 1].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled:) ⇒ CdpRecorder

Returns a new instance of CdpRecorder.



44
45
46
47
# File 'lib/test_recorder/cdp_recorder.rb', line 44

def initialize(enabled:)
  @enabled = enabled
  @started = nil
end

Class Method Details

.record(devtools) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/test_recorder/cdp_recorder.rb', line 12

def record(devtools)
  records[devtools] ||= begin
    page = devtools.page
    page.enable

    record = Record.new(page, nil, 0)
    page.on(:screencast_frame) do |event|
      write_frame(record, event["data"])
      record.page.screencast_frame_ack(session_id: event["sessionId"])
    end
    record
  end
end

Instance Method Details

#start(page:, enabled: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/test_recorder/cdp_recorder.rb', line 49

def start(page:, enabled: nil)
  enabled = @enabled if enabled.nil?
  @started = enabled
  return unless @started

  @tmp_video = Tempfile.new(["testrecorder", ".mjpeg"])
  @tmp_video.binmode

  @record = self.class.record(page.driver.browser.devtools)
  @record.io = @tmp_video
  @record.frame_count = 0

  @every_nth_frame = TestRecorder.every_nth_frame

  @record.page.start_screencast(format: "jpeg", quality: TestRecorder.jpeg_quality, max_width: TestRecorder.max_dimension, max_height: TestRecorder.max_dimension, every_nth_frame: @every_nth_frame)
end

#stop_and_discardObject



66
67
68
69
70
71
72
# File 'lib/test_recorder/cdp_recorder.rb', line 66

def stop_and_discard
  return unless @started

  @record.io = nil
  @record.page.stop_screencast
  @tmp_video.close!
end

#stop_and_save(filename) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/test_recorder/cdp_recorder.rb', line 74

def stop_and_save(filename)
  return "" unless @started

  @record.io = nil
  @record.page.stop_screencast
  @tmp_video.flush

  # Chrome sends a frame as soon as the screencast starts, so a test that fails
  # before it draws anything still leaves behind the one frame of the blank page
  # it started on.
  if @record.frame_count < 2
    @tmp_video.close!
    return ""
  end

  video_dir = ::Rails.root.join("tmp", "videos")
  FileUtils.mkdir_p(video_dir)
  video_path = video_dir.join(filename).to_s

  # Chrome captures at about 25 fps, but `every_nth_frame` makes it deliver only
  # one out of every N frames. So the captured file holds 25 / N frames per second.
  # Tell ffmpeg that input rate, otherwise it assumes 25 fps and the video plays
  # N times faster than the actual test.
  result = system("ffmpeg", "-loglevel", "error", "-f", "image2pipe", "-c:v", "mjpeg", "-framerate", "25/#{@every_nth_frame}", "-i", @tmp_video.path, *FFMPEG_ENCODE_OPTIONS, video_path)

  @tmp_video.close!

  if result.nil?
    warn "[TestRecorder] Failed to execute ffmpeg. Please make sure that FFmpeg is installed."
    return ""
  elsif !result
    warn "[TestRecorder] ffmpeg failed to encode #{video_path}."
    return ""
  end

  video_path
end