-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rb] add support for element screenshots (#8533)
Extracts TakesScreenshot from DriverExtensions to apply it to Element too
- Loading branch information
1 parent
55db83f
commit 8a66e12
Showing
14 changed files
with
153 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 0 additions & 65 deletions
65
rb/lib/selenium/webdriver/common/driver_extensions/takes_screenshot.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
module Selenium | ||
module WebDriver | ||
# | ||
# @api private | ||
# | ||
module TakesScreenshot | ||
# | ||
# Save a PNG screenshot to the given path | ||
# | ||
# @api public | ||
# | ||
|
||
def save_screenshot(png_path) | ||
extension = File.extname(png_path).downcase | ||
if extension != '.png' | ||
WebDriver.logger.warn "name used for saved screenshot does not match file type. "\ | ||
"It should end with .png extension", | ||
id: :screenshot | ||
end | ||
File.open(png_path, 'wb') { |f| f << screenshot_as(:png) } | ||
end | ||
|
||
# | ||
# Return a PNG screenshot in the given format as a string | ||
# | ||
# @param [:base64, :png] format | ||
# @return String screenshot | ||
# | ||
# @api public | ||
|
||
def screenshot_as(format) | ||
case format | ||
when :base64 | ||
screenshot | ||
when :png | ||
screenshot.unpack1('m') | ||
else | ||
raise Error::UnsupportedOperationError, "unsupported format: #{format.inspect}" | ||
end | ||
end | ||
|
||
end # TakesScreenshot | ||
end # WebDriver | ||
end # Selenium |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
rb/spec/integration/selenium/webdriver/takes_screenshot_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
require_relative 'spec_helper' | ||
|
||
module Selenium | ||
module WebDriver | ||
describe TakesScreenshot do | ||
before do | ||
driver.navigate.to url_for('xhtmlTest.html') | ||
end | ||
|
||
let(:element) { driver.find_element(css: '.content') } | ||
let(:path) { "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.png" } | ||
|
||
it 'should save' do | ||
save_screenshots_and_assert(path) | ||
end | ||
|
||
it 'should warn if extension of provided path is not png' do | ||
jpg_path = "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.jpg" | ||
message = "name used for saved screenshot does not match file type. "\ | ||
"It should end with .png extension" | ||
expect(WebDriver.logger).to receive(:warn).with(message, id: :screenshot).twice | ||
|
||
save_screenshots_and_assert(jpg_path) | ||
end | ||
|
||
it 'should not warn if extension of provided path is png' do | ||
expect(WebDriver.logger).not_to receive(:warn) | ||
|
||
save_screenshots_and_assert(path) | ||
end | ||
|
||
it 'should return in the specified format' do | ||
ss = element.screenshot_as(:png) | ||
expect(ss).to be_kind_of(String) | ||
expect(ss.size).to be_positive | ||
end | ||
|
||
it 'raises an error when given an unknown format' do | ||
expect { element.screenshot_as(:jpeg) }.to raise_error(WebDriver::Error::UnsupportedOperationError) | ||
end | ||
|
||
def save_screenshots_and_assert(path) | ||
save_screenshot_and_assert(driver, path) | ||
save_screenshot_and_assert(element, path) | ||
end | ||
|
||
def save_screenshot_and_assert(source, path) | ||
source.save_screenshot path | ||
expect(File.exist?(path)).to be true | ||
expect(File.size(path)).to be_positive | ||
ensure | ||
File.delete(path) if File.exist?(path) | ||
end | ||
end | ||
end | ||
end |