Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/takes_screenshot.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
#
23
# @api private
24
#
25
module TakesScreenshot
26
#
27
# Save a PNG screenshot of the viewport to the given path
28
#
29
# @api public
30
#
31
32
def save_screenshot(png_path, full_page: false)
33
extension = File.extname(png_path).downcase
34
if extension != '.png'
35
WebDriver.logger.warn 'name used for saved screenshot does not match file type. ' \
36
'It should end with .png extension',
37
id: :screenshot
38
end
39
WebDriver.logger.debug("Saving screenshot to #{Dir.pwd}/#{png_path}")
40
File.open(png_path, 'wb') { |f| f << screenshot_as(:png, full_page: full_page) }
41
end
42
43
#
44
# Return a PNG screenshot in the given format as a string
45
#
46
# @param [:base64, :png] format
47
# @param [Boolean] full_page allows taking full page screenshots if supported
48
# @return String screenshot
49
#
50
# @api public
51
52
def screenshot_as(format, full_page: false)
53
if full_page && !respond_to?(:save_full_page_screenshot)
54
raise Error::UnsupportedOperationError, "Full Page Screenshots are not supported for #{inspect}"
55
end
56
57
case format
58
when :base64
59
full_page ? full_screenshot : screenshot
60
when :png
61
screenshot_as(:base64, full_page: full_page).unpack1('m')
62
else
63
raise Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
64
end
65
end
66
end # TakesScreenshot
67
end # WebDriver
68
end # Selenium
69
70