Skip to content

Commit

Permalink
Merge pull request #234 from YusukeIwaki/porting/8496
Browse files Browse the repository at this point in the history
feat: add fromSurface option to page.screenshot
  • Loading branch information
YusukeIwaki committed Jul 3, 2022
2 parents 3a45bea + 9a5499f commit 7162980
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 24 deletions.
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

0 comments on commit 7162980

Please sign in to comment.