Path: blob/trunk/rb/spec/integration/selenium/webdriver/takes_screenshot_spec.rb
1864 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.1819require_relative 'spec_helper'2021module Selenium22module WebDriver23describe TakesScreenshot, exclusive: {bidi: false, reason: 'Not yet implemented with BiDi'} do24before do25driver.navigate.to url_for('xhtmlTest.html')26end2728let(:element) { driver.find_element(css: '.content') }29let(:path) { "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.png" }3031it 'saves' do32save_screenshots_and_assert(path)33end3435it 'warns if extension of provided path is not png' do36jpg_path = "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.jpg"37message = 'name used for saved screenshot does not match file type. ' \38'It should end with .png extension'39allow(WebDriver.logger).to receive(:warn)4041save_screenshots_and_assert(jpg_path)4243expect(WebDriver.logger).to have_received(:warn).with(message, id: :screenshot).twice44end4546it 'does not warn if extension of provided path is png' do47allow(WebDriver.logger).to receive(:warn)4849save_screenshots_and_assert(path)5051expect(WebDriver.logger).not_to have_received(:warn)52end5354it 'returns in the specified format' do55ss = element.screenshot_as(:png)56expect(ss).to be_a(String)57expect(ss.size).to be_positive58end5960it 'raises an error when given an unknown format' do61expect { element.screenshot_as(:jpeg) }.to raise_error(WebDriver::Error::UnsupportedOperationError)62end6364def save_screenshots_and_assert(path)65save_screenshot_and_assert(driver, path)66save_screenshot_and_assert(element, path)67end6869def save_screenshot_and_assert(source, path)70source.save_screenshot path71expect(File.exist?(path)).to be true72expect(File.size(path)).to be_positive73ensure74FileUtils.rm_rf(path)75end7677describe 'page size' do78before do79driver.navigate.to url_for('printPage.html')80end8182after { FileUtils.rm_rf(path) }8384it 'takes viewport screenshot by default' do85viewport_width = driver.execute_script('return window.innerWidth;')86viewport_height = driver.execute_script('return window.innerHeight;')8788screenshot = driver.save_screenshot path89width, height = png_size(screenshot)9091expect(width).to be <= viewport_width92expect(height).to be <= viewport_height93end9495it 'takes full page screenshot', except: [{platform: :windows,96reason: 'Some issues with resolution?'},97{platform: :macosx,98reason: 'showing half resolution of what expected'}],99exclusive: {browser: :firefox} do100viewport_width = driver.execute_script('return window.innerWidth;')101viewport_height = driver.execute_script('return window.innerHeight;')102103screenshot = driver.save_screenshot path, full_page: true104width, height = png_size(screenshot)105106expect(width).to be >= viewport_width107expect(height).to be > viewport_height108end109110it 'does not take full page screenshot', only: {browser: %i[chrome edge safari safari_preview],111reason: 'these browsers do not implement this feature'} do112expect {113driver.save_screenshot path, full_page: true114}.to raise_exception(Error::UnsupportedOperationError, /Full Page Screenshots are not supported/)115end116end117end118end119end120121122