Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fromSurface option to page.screenshot #234

Merged
merged 1 commit into from Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/puppeteer/element_handle.rb
Expand Up @@ -505,7 +505,15 @@ def box_model
end
end

def screenshot(type: nil, path: nil, full_page: nil, clip: nil, quality: nil, omit_background: nil, encoding: nil)
def screenshot(type: nil,
path: nil,
full_page: nil,
clip: nil,
quality: nil,
omit_background: nil,
encoding: nil,
capture_beyond_viewport: nil,
from_surface: nil)
needs_viewport_reset = false

box = bounding_box
Expand Down Expand Up @@ -549,7 +557,17 @@ def screenshot(type: nil, path: nil, full_page: nil, clip: nil, quality: nil, om
}
end

@page.screenshot(type: type, path: path, full_page: full_page, clip: clip, quality: quality, omit_background: omit_background, encoding: encoding)
@page.screenshot(
type: type,
path: path,
full_page:
full_page,
clip: clip,
quality: quality,
omit_background: omit_background,
encoding: encoding,
capture_beyond_viewport: capture_beyond_viewport,
from_surface: from_surface)
ensure
if needs_viewport_reset
@page.viewport = viewport
Expand Down
40 changes: 27 additions & 13 deletions lib/puppeteer/page.rb
Expand Up @@ -1025,7 +1025,15 @@ def title
# @param quality [Integer]
# @param omit_background [Boolean]
# @param encoding [String]
def screenshot(type: nil, path: nil, full_page: nil, clip: nil, quality: nil, omit_background: nil, encoding: nil)
def screenshot(type: nil,
path: nil,
full_page: nil,
clip: nil,
quality: nil,
omit_background: nil,
encoding: nil,
capture_beyond_viewport: nil,
from_surface: nil)
options = {
type: type,
path: path,
Expand All @@ -1034,6 +1042,8 @@ def screenshot(type: nil, path: nil, full_page: nil, clip: nil, quality: nil, om
quality: quality,
omit_background: omit_background,
encoding: encoding,
capture_beyond_viewport: capture_beyond_viewport,
from_surface: from_surface,
}.compact
screenshot_options = ScreenshotOptions.new(options)

Expand Down Expand Up @@ -1062,18 +1072,20 @@ def screenshot(type: nil, path: nil, full_page: nil, clip: nil, quality: nil, om
# Overwrite clip for full page at all times.
clip = { x: 0, y: 0, width: width, height: height, scale: 1 }

screen_orientation =
if @viewport&.landscape?
{ angle: 90, type: 'landscapePrimary' }
else
{ angle: 0, type: 'portraitPrimary' }
end
@client.send_message('Emulation.setDeviceMetricsOverride',
mobile: @viewport&.mobile? || false,
width: width,
height: height,
deviceScaleFactor: @viewport&.device_scale_factor || 1,
screenOrientation: screen_orientation)
unless screenshot_options.capture_beyond_viewport?
screen_orientation =
if @viewport&.landscape?
{ angle: 90, type: 'landscapePrimary' }
else
{ angle: 0, type: 'portraitPrimary' }
end
@client.send_message('Emulation.setDeviceMetricsOverride',
mobile: @viewport&.mobile? || false,
width: width,
height: height,
deviceScaleFactor: @viewport&.device_scale_factor || 1,
screenOrientation: screen_orientation)
end
end

should_set_default_background = screenshot_options.omit_background? && format == 'png'
Expand All @@ -1082,6 +1094,8 @@ def screenshot(type: nil, path: nil, full_page: nil, clip: nil, quality: nil, om
format: format,
quality: screenshot_options.quality,
clip: clip,
captureBeyondViewport: screenshot_options.capture_beyond_viewport?,
fromSurface: screenshot_options.from_surface?,
}.compact
result = @client.send_message('Page.captureScreenshot', screenshot_params)
reset_default_background_color if should_set_default_background
Expand Down
72 changes: 63 additions & 9 deletions lib/puppeteer/page/screenshot_options.rb
Expand Up @@ -2,15 +2,49 @@

class Puppeteer::Page
# /**
# * @typedef {Object} ScreenshotOptions
# * @property {string=} type
# * @property {string=} path
# * @property {boolean=} fullPage
# * @property {{x: number, y: number, width: number, height: number}=} clip
# * @property {number=} quality
# * @property {boolean=} omitBackground
# * @property {string=} encoding
# */
# * @defaultValue 'png'
# */
# type?: 'png' | 'jpeg' | 'webp';
# /**
# * The file path to save the image to. The screenshot type will be inferred
# * from file extension. If path is a relative path, then it is resolved
# * relative to current working directory. If no path is provided, the image
# * won't be saved to the disk.
# */
# path?: string;
# /**
# * When true, takes a screenshot of the full page.
# * @defaultValue false
# */
# fullPage?: boolean;
# /**
# * An object which specifies the clipping region of the page.
# */
# clip?: ScreenshotClip;
# /**
# * Quality of the image, between 0-100. Not applicable to `png` images.
# */
# quality?: number;
# /**
# * Hides default white background and allows capturing screenshots with transparency.
# * @defaultValue false
# */
# omitBackground?: boolean;
# /**
# * Encoding of the image.
# * @defaultValue 'binary'
# */
# encoding?: 'base64' | 'binary';
# /**
# * Capture the screenshot beyond the viewport.
# * @defaultValue true
# */
# captureBeyondViewport?: boolean;
# /**
# * Capture the screenshot from the surface, rather than the view.
# * @defaultValue true
# */
# fromSurface?: boolean;
class ScreenshotOptions
# @params options [Hash]
def initialize(options)
Expand Down Expand Up @@ -65,6 +99,18 @@ def initialize(options)
@clip = options[:clip]
@omit_background = options[:omit_background]
@encoding = options[:encoding]
@capture_beyond_viewport =
if options[:capture_beyond_viewport].nil?
true
else
options[:capture_beyond_viewport]
end
@from_surface =
if options[:from_surface].nil?
true
else
options[:from_surface]
end
end

attr_reader :type, :quality, :path, :clip, :encoding
Expand All @@ -76,5 +122,13 @@ def full_page?
def omit_background?
@omit_background
end

def capture_beyond_viewport?
@capture_beyond_viewport
end

def from_surface?
@from_surface
end
end
end
10 changes: 10 additions & 0 deletions spec/integration/screenshot_spec.rb
Expand Up @@ -142,6 +142,16 @@
# 'screenshot-sanity.png'
# );
# });

it 'should work in "fromSurface: false" mode' do
skip if headless?

page.viewport = Puppeteer::Viewport.new(width: 500, height: 500)
page.goto("#{server_prefix}/grid.html")

screenshot = page.screenshot(from_surface: false)
expect(screenshot.length).to be >= 1000
end
end

# Regression spec for # https://github.com/YusukeIwaki/puppeteer-ruby/issues/96
Expand Down