Skip to content

Commit

Permalink
[py] Adding full page screenshot feature for Firefox (#7182)
Browse files Browse the repository at this point in the history
Geckodriver allows to make full page screenshots since version 0.24.0.
This commit makes use of that feature.
  • Loading branch information
gentoo90 authored and lmtierney committed Jul 18, 2019
1 parent ac42a3e commit 8fa569e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions py/selenium/webdriver/firefox/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ def __init__(self, remote_server_addr, keep_alive=True):
("POST", "/session/$sessionId/moz/addon/install")
self._commands["UNINSTALL_ADDON"] = \
("POST", "/session/$sessionId/moz/addon/uninstall")
self._commands["FULL_PAGE_SCREENSHOT"] = \
("GET", "/session/$sessionId/moz/screenshot/full")
69 changes: 69 additions & 0 deletions py/selenium/webdriver/firefox/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
except NameError: # Python 3.x
basestring = str

import base64
import shutil
import warnings
from contextlib import contextmanager
Expand Down Expand Up @@ -257,3 +258,71 @@ def uninstall_addon(self, identifier):
driver.uninstall_addon('addon@foo.com')
"""
self.execute("UNINSTALL_ADDON", {"id": identifier})

def get_full_page_screenshot_as_file(self, filename):
"""
Saves a full document screenshot of the current window to a PNG image file. Returns
False if there is any IOError, else returns True. Use full paths in
your filename.
:Args:
- filename: The full path you wish to save your screenshot to. This
should end with a `.png` extension.
:Usage:
::
driver.get_full_page_screenshot_as_file('/Screenshots/foo.png')
"""
if not filename.lower().endswith('.png'):
warnings.warn("name used for saved screenshot does not match file "
"type. It should end with a `.png` extension", UserWarning)
png = self.get_full_page_screenshot_as_png()
try:
with open(filename, 'wb') as f:
f.write(png)
except IOError:
return False
finally:
del png
return True

def save_full_page_screenshot(self, filename):
"""
Saves a full document screenshot of the current window to a PNG image file. Returns
False if there is any IOError, else returns True. Use full paths in
your filename.
:Args:
- filename: The full path you wish to save your screenshot to. This
should end with a `.png` extension.
:Usage:
::
driver.save_full_page_screenshot('/Screenshots/foo.png')
"""
return self.get_full_page_screenshot_as_file(filename)

def get_full_page_screenshot_as_png(self):
"""
Gets the full document screenshot of the current window as a binary data.
:Usage:
::
driver.get_full_page_screenshot_as_png()
"""
return base64.b64decode(self.get_full_page_screenshot_as_base64().encode('ascii'))

def get_full_page_screenshot_as_base64(self):
"""
Gets the full document screenshot of the current window as a base64 encoded string
which is useful in embedded images in HTML.
:Usage:
::
driver.get_full_page_screenshot_as_base64()
"""
return self.execute("FULL_PAGE_SCREENSHOT")['value']
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.

import base64
import imghdr


def test_get_full_page_screenshot_as_base64(driver, pages):
pages.load("simpleTest.html")
result = base64.b64decode(driver.get_full_page_screenshot_as_base64())
assert imghdr.what('', result) == 'png'


def test_get_full_page_screenshot_as_png(driver, pages):
pages.load("simpleTest.html")
result = driver.get_full_page_screenshot_as_png()
assert imghdr.what('', result) == 'png'

0 comments on commit 8fa569e

Please sign in to comment.