Skip to content

Commit

Permalink
fix: Corrected linter violations (and updated docs to match)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Collins committed Nov 27, 2024
1 parent a804f05 commit eb1094c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 45 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,7 @@ page.go_to("https://apple.com/ipad")

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

sleep 10
Expand Down
26 changes: 10 additions & 16 deletions lib/ferrum/page/screencast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Ferrum
class Page
module Screencast

# Starts yielding each frame to the given block.
#
# @param [Hash{Symbol => Object}] opts
Expand Down Expand Up @@ -64,44 +63,39 @@ module Screencast
#
# page.start_screencast(format: :jpeg, quality: 75) do |data, metadata|
# timestamp_ms = metadata['timestamp'] * 1000
# File.open("image_#{timestamp_ms.to_i}.jpg", 'wb') do
# _1.write(Base64.decode64 data)
# end
# File.binwrite("image_#{timestamp_ms.to_i}.jpg", Base64.decode64(data))
# end
#
# sleep 10
#
# page.stop_screencast
#
def start_screencast(**opts)

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

if error_text = response["errorText"] # https://cs.chromium.org/chromium/src/net/base/net_error_list.h
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, metadata, session_id = params.values_at('data', 'metadata', 'sessionId')
on("Page.screencastFrame") do |params|
data, metadata, session_id = params.values_at("data", "metadata", "sessionId")

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

yield data, metadata, session_id
end
end

# Stops sending each frame.
def stop_screencast
command('Page.stopScreencast')
command("Page.stopScreencast")
end

private

START_SCREENCAST_KEY_CONV = {
max_width: :maxWidth,
max_height: :maxHeight,
every_nth_frame: :everyNthFrame,
max_width: :maxWidth,
max_height: :maxHeight,
every_nth_frame: :everyNthFrame
}.freeze
end
end
Expand Down
49 changes: 23 additions & 26 deletions spec/page/screencast_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'base64'
require "base64"
require "image_size"
require "pdf/reader"
require "chunky_png"
Expand All @@ -13,18 +13,17 @@
Dir.glob("#{PROJECT_ROOT}/spec/tmp/screencast_frame*") { File.delete _1 }
end

describe '#start_screencast' do
context 'when the page has no changing content' do
it 'should continue screencasting frames' do
browser.go_to '/ferrum/long_page'
describe "#start_screencast" do
context "when the page has no changing content" do
it "should continue screencasting frames" do
browser.go_to "/ferrum/long_page"

format = :jpeg
count = 0
browser.start_screencast(format: format) do |data, metadata, session_id|
browser.start_screencast(format: format) do |data, _metadata, _session_id|
count += 1
File.open("#{PROJECT_ROOT}/spec/tmp/screencast_frame_#{'%05d' % count}.#{format}", 'wb') do
_1.write(Base64.decode64 data)
end
path = "#{PROJECT_ROOT}/spec/tmp/screencast_frame_#{format('%05d', count)}.#{format}"
File.binwrite(path, Base64.decode64(data))
end

sleep 5
Expand All @@ -35,17 +34,16 @@
end
end

context 'when the page content continually changes' do
it 'should stop screencasting frames when the page has finished rendering' do
browser.go_to '/ferrum/animation'
context "when the page content continually changes" do
it "should stop screencasting frames when the page has finished rendering" do
browser.go_to "/ferrum/animation"

format = :jpeg
count = 0
browser.start_screencast(format: format) do |data, metadata, session_id|
browser.start_screencast(format: format) do |data, _metadata, _session_id|
count += 1
File.open("#{PROJECT_ROOT}/spec/tmp/screencast_frame_#{'%05d' % count}.#{format}", 'wb') do
_1.write(Base64.decode64 data)
end
path = "#{PROJECT_ROOT}/spec/tmp/screencast_frame_#{format('%05d', count)}.#{format}"
File.binwrite(path, Base64.decode64(data))
end

sleep 5
Expand All @@ -57,18 +55,17 @@
end
end

describe '#stop_screencast' do
context 'when the page content continually changes' do
it 'should stop screencasting frames when the page has finished rendering' do
browser.go_to '/ferrum/animation'
describe "#stop_screencast" do
context "when the page content continually changes" do
it "should stop screencasting frames when the page has finished rendering" do
browser.go_to "/ferrum/animation"

format = :jpeg
count = 0
browser.start_screencast(format: format) do |data, metadata, session_id|
browser.start_screencast(format: format) do |data, _metadata, _session_id|
count += 1
File.open("#{PROJECT_ROOT}/spec/tmp/screencast_frame_#{'%05d' % count}.#{format}", 'wb') do
_1.write(Base64.decode64 data)
end
path = "#{PROJECT_ROOT}/spec/tmp/screencast_frame_#{format('%05d', count)}.#{format}"
File.binwrite(path, Base64.decode64(data))
end

sleep 5
Expand All @@ -79,9 +76,9 @@

sleep 2

no_more_frames_after_stop = number_of_frames_after_stop == Dir.glob("#{PROJECT_ROOT}/spec/tmp/screencast_frame_*").count
number_of_frames_after_delay = Dir.glob("#{PROJECT_ROOT}/spec/tmp/screencast_frame_*").count

expect(no_more_frames_after_stop).to be_truthy
expect(number_of_frames_after_stop).to eq number_of_frames_after_delay
end
end
end
Expand Down

0 comments on commit eb1094c

Please sign in to comment.