Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/integration/selenium/webdriver/takes_screenshot_spec.rb
1864 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
require_relative 'spec_helper'
21
22
module Selenium
23
module WebDriver
24
describe TakesScreenshot, exclusive: {bidi: false, reason: 'Not yet implemented with BiDi'} 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 'saves' do
33
save_screenshots_and_assert(path)
34
end
35
36
it 'warns 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
allow(WebDriver.logger).to receive(:warn)
41
42
save_screenshots_and_assert(jpg_path)
43
44
expect(WebDriver.logger).to have_received(:warn).with(message, id: :screenshot).twice
45
end
46
47
it 'does not warn if extension of provided path is png' do
48
allow(WebDriver.logger).to receive(:warn)
49
50
save_screenshots_and_assert(path)
51
52
expect(WebDriver.logger).not_to have_received(:warn)
53
end
54
55
it 'returns in the specified format' do
56
ss = element.screenshot_as(:png)
57
expect(ss).to be_a(String)
58
expect(ss.size).to be_positive
59
end
60
61
it 'raises an error when given an unknown format' do
62
expect { element.screenshot_as(:jpeg) }.to raise_error(WebDriver::Error::UnsupportedOperationError)
63
end
64
65
def save_screenshots_and_assert(path)
66
save_screenshot_and_assert(driver, path)
67
save_screenshot_and_assert(element, path)
68
end
69
70
def save_screenshot_and_assert(source, path)
71
source.save_screenshot path
72
expect(File.exist?(path)).to be true
73
expect(File.size(path)).to be_positive
74
ensure
75
FileUtils.rm_rf(path)
76
end
77
78
describe 'page size' do
79
before do
80
driver.navigate.to url_for('printPage.html')
81
end
82
83
after { FileUtils.rm_rf(path) }
84
85
it 'takes viewport screenshot by default' do
86
viewport_width = driver.execute_script('return window.innerWidth;')
87
viewport_height = driver.execute_script('return window.innerHeight;')
88
89
screenshot = driver.save_screenshot path
90
width, height = png_size(screenshot)
91
92
expect(width).to be <= viewport_width
93
expect(height).to be <= viewport_height
94
end
95
96
it 'takes full page screenshot', except: [{platform: :windows,
97
reason: 'Some issues with resolution?'},
98
{platform: :macosx,
99
reason: 'showing half resolution of what expected'}],
100
exclusive: {browser: :firefox} do
101
viewport_width = driver.execute_script('return window.innerWidth;')
102
viewport_height = driver.execute_script('return window.innerHeight;')
103
104
screenshot = driver.save_screenshot path, full_page: true
105
width, height = png_size(screenshot)
106
107
expect(width).to be >= viewport_width
108
expect(height).to be > viewport_height
109
end
110
111
it 'does not take full page screenshot', only: {browser: %i[chrome edge safari safari_preview],
112
reason: 'these browsers do not implement this feature'} do
113
expect {
114
driver.save_screenshot path, full_page: true
115
}.to raise_exception(Error::UnsupportedOperationError, /Full Page Screenshots are not supported/)
116
end
117
end
118
end
119
end
120
end
121
122