Path: blob/trunk/rb/lib/selenium/webdriver/common/takes_screenshot.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21#22# @api private23#24module TakesScreenshot25#26# Save a PNG screenshot of the viewport to the given path27#28# @api public29#3031def save_screenshot(png_path, full_page: false)32extension = File.extname(png_path).downcase33if extension != '.png'34WebDriver.logger.warn 'name used for saved screenshot does not match file type. ' \35'It should end with .png extension',36id: :screenshot37end38WebDriver.logger.debug("Saving screenshot to #{Dir.pwd}/#{png_path}")39File.open(png_path, 'wb') { |f| f << screenshot_as(:png, full_page: full_page) }40end4142#43# Return a PNG screenshot in the given format as a string44#45# @param [:base64, :png] format46# @param [Boolean] full_page allows taking full page screenshots if supported47# @return String screenshot48#49# @api public5051def screenshot_as(format, full_page: false)52if full_page && !respond_to?(:save_full_page_screenshot)53raise Error::UnsupportedOperationError, "Full Page Screenshots are not supported for #{inspect}"54end5556case format57when :base6458full_page ? full_screenshot : screenshot59when :png60screenshot_as(:base64, full_page: full_page).unpack1('m')61else62raise Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"63end64end65end # TakesScreenshot66end # WebDriver67end # Selenium686970