Module: Ferrum::Page::Screencast

Included in:
Ferrum::Page
Defined in:
lib/ferrum/page/screencast.rb

Constant Summary collapse

START_SCREENCAST_KEY_CONV =
{
  max_width: :maxWidth,
  max_height: :maxHeight,
  every_nth_frame: :everyNthFrame
}.freeze

Instance Method Summary collapse

Instance Method Details

#start_screencast(**opts) {|data, metadata, session_id| ... } ⇒ Object

Starts sending frames to record screencast to the given block.

Examples:

require "base64"

page.go_to("https://apple.com/ipad")

page.start_screencast(format: :jpeg, quality: 75) do |data, |
  timestamp = (['timestamp'] * 1000).to_i
  File.binwrite("image_#{timestamp}.jpg", Base64.decode64(data))
end

sleep 10

page.stop_screencast

Parameters:

  • opts (Hash{Symbol => Object})
  • metadata (Hash)

    a customizable set of options

Options Hash (**opts):

  • :format (:jpeg, :png)

    The format the image should be returned in.

  • :quality (Integer)

    The image quality. Note: 0-100 works for JPEG only.

  • :max_width (Integer)

    Maximum screencast frame width.

  • :max_height (Integer)

    Maximum screencast frame height.

  • :every_nth_frame (Integer)

    Send every n-th frame.

Yields:

  • (data, metadata, session_id)

    The given block receives the screencast frame along with metadata about the frame and the screencast session ID.

Yield Parameters:

  • data (String)

    Base64-encoded compressed image.

  • metadata (Hash{String => Object})

    Screencast frame metadata.

  • session_id (Integer)

    Frame number.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ferrum/page/screencast.rb', line 73

def start_screencast(**opts)
  options = opts.transform_keys { START_SCREENCAST_KEY_CONV.fetch(_1, _1) }
  response = command("Page.startScreencast", **options)

  if (error_text = response["errorText"]) # https://cs.chromium.org/chromium/src/net/base/net_error_list.h
    raise "Starting screencast failed (#{error_text})"
  end

  on("Page.screencastFrame") do |params|
    data, , session_id = params.values_at("data", "metadata", "sessionId")

    command("Page.screencastFrameAck", sessionId: session_id)

    yield data, , session_id
  end
end

#stop_screencastObject

Stops sending frames.



91
92
93
# File 'lib/ferrum/page/screencast.rb', line 91

def stop_screencast
  command("Page.stopScreencast")
end