Class: Puppeteer::Page::ScreenshotOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/page/screenshot_options.rb

Overview

/**

  • @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;

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ScreenshotOptions

Returns a new instance of ScreenshotOptions.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
# File 'lib/puppeteer/page/screenshot_options.rb', line 50

def initialize(options)
  if options[:type]
    unless [:png, :jpeg, :webp].include?(options[:type].to_sym)
      raise ArgumentError.new("Unknown options.type value: #{options[:type]}")
    end
    @type = options[:type]
  elsif options[:path]
    mime_types = MIME::Types.type_for(options[:path])
    if mime_types.include?('image/png')
      @type = 'png'
    elsif mime_types.include?('image/jpeg')
      @type = 'jpeg'
    elsif mime_types.include?('image/webp')
      @type = 'webp'
    else
      raise ArgumentError.new("Unsupported screenshot mime type resolved: #{mime_types}, path: #{options[:path]}")
    end
  end
  @type ||= 'png'

  if options[:quality]
    if @type != 'jpeg' && @type != 'webp'
      raise ArgumentError.new("options.quality is unsupported for the #{@type} screenshots")
    end
    unless options[:quality].is_a?(Numeric)
      raise ArgumentError.new("Expected options.quality to be a number but found #{options[:quality].class}")
    end
    quality = options[:quality].to_i
    unless (0..100).include?(quality)
      raise ArgumentError.new("Expected options.quality to be between 0 and 100 (inclusive), got #{quality}")
    end
    @quality = quality
  end

  if options[:clip] && options[:full_page]
    raise ArgumentError.new('options.clip and options.fullPage are exclusive')
  end

  # if (options.clip) {
  #   assert(typeof options.clip.x === 'number', 'Expected options.clip.x to be a number but found ' + (typeof options.clip.x));
  #   assert(typeof options.clip.y === 'number', 'Expected options.clip.y to be a number but found ' + (typeof options.clip.y));
  #   assert(typeof options.clip.width === 'number', 'Expected options.clip.width to be a number but found ' + (typeof options.clip.width));
  #   assert(typeof options.clip.height === 'number', 'Expected options.clip.height to be a number but found ' + (typeof options.clip.height));
  #   assert(options.clip.width !== 0, 'Expected options.clip.width not to be 0.');
  #   assert(options.clip.height !== 0, 'Expected options.clip.height not to be 0.');
  # }

  @path = options[:path]
  @full_page = options[:full_page]
  @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 = options[:from_surface]
end

Instance Attribute Details

#clipObject (readonly)

Returns the value of attribute clip.



111
112
113
# File 'lib/puppeteer/page/screenshot_options.rb', line 111

def clip
  @clip
end

#encodingObject (readonly)

Returns the value of attribute encoding.



111
112
113
# File 'lib/puppeteer/page/screenshot_options.rb', line 111

def encoding
  @encoding
end

#from_surfaceObject (readonly)

Returns the value of attribute from_surface.



111
112
113
# File 'lib/puppeteer/page/screenshot_options.rb', line 111

def from_surface
  @from_surface
end

#pathObject (readonly)

Returns the value of attribute path.



111
112
113
# File 'lib/puppeteer/page/screenshot_options.rb', line 111

def path
  @path
end

#qualityObject (readonly)

Returns the value of attribute quality.



111
112
113
# File 'lib/puppeteer/page/screenshot_options.rb', line 111

def quality
  @quality
end

#typeObject (readonly)

Returns the value of attribute type.



111
112
113
# File 'lib/puppeteer/page/screenshot_options.rb', line 111

def type
  @type
end

Instance Method Details

#capture_beyond_viewport?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/puppeteer/page/screenshot_options.rb', line 121

def capture_beyond_viewport?
  @capture_beyond_viewport
end

#full_page?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/puppeteer/page/screenshot_options.rb', line 113

def full_page?
  @full_page
end

#omit_background?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/puppeteer/page/screenshot_options.rb', line 117

def omit_background?
  @omit_background
end