|
| 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 | +require_relative 'spec_helper' |
| 21 | + |
| 22 | +module Selenium |
| 23 | + module WebDriver |
| 24 | + describe TakesScreenshot do |
| 25 | + before do |
| 26 | + driver.navigate.to url_for('xhtmlTest.html') |
| 27 | + end |
| 28 | + |
| 29 | + let(:element) { driver.find_element(css: '.content') } |
| 30 | + let(:path) { "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.png" } |
| 31 | + |
| 32 | + it 'should save' do |
| 33 | + save_screenshots_and_assert(path) |
| 34 | + end |
| 35 | + |
| 36 | + it 'should warn if extension of provided path is not png' do |
| 37 | + jpg_path = "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.jpg" |
| 38 | + message = "name used for saved screenshot does not match file type. "\ |
| 39 | + "It should end with .png extension" |
| 40 | + expect(WebDriver.logger).to receive(:warn).with(message, id: :screenshot).twice |
| 41 | + |
| 42 | + save_screenshots_and_assert(jpg_path) |
| 43 | + end |
| 44 | + |
| 45 | + it 'should not warn if extension of provided path is png' do |
| 46 | + expect(WebDriver.logger).not_to receive(:warn) |
| 47 | + |
| 48 | + save_screenshots_and_assert(path) |
| 49 | + end |
| 50 | + |
| 51 | + it 'should return in the specified format' do |
| 52 | + ss = element.screenshot_as(:png) |
| 53 | + expect(ss).to be_kind_of(String) |
| 54 | + expect(ss.size).to be_positive |
| 55 | + end |
| 56 | + |
| 57 | + it 'raises an error when given an unknown format' do |
| 58 | + expect { element.screenshot_as(:jpeg) }.to raise_error(WebDriver::Error::UnsupportedOperationError) |
| 59 | + end |
| 60 | + |
| 61 | + def save_screenshots_and_assert(path) |
| 62 | + save_screenshot_and_assert(driver, path) |
| 63 | + save_screenshot_and_assert(element, path) |
| 64 | + end |
| 65 | + |
| 66 | + def save_screenshot_and_assert(source, path) |
| 67 | + source.save_screenshot path |
| 68 | + expect(File.exist?(path)).to be true |
| 69 | + expect(File.size(path)).to be_positive |
| 70 | + ensure |
| 71 | + File.delete(path) if File.exist?(path) |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | +end |
0 commit comments