Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions py/selenium/webdriver/chromium/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

import base64
import warnings

from selenium.webdriver.chromium.options import ChromiumOptions
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
Expand Down Expand Up @@ -217,6 +219,71 @@ def quit(self) -> None:
finally:
self.service.stop()

def get_full_page_screenshot_as_file(self, filename) -> bool:
"""Save a full document screenshot of the current window to a PNG image file.

Args:
filename: The full path you wish to save your screenshot to. This
should end with a `.png` extension.

Returns:
False if there is any IOError, else returns True. Use full paths in your filename.

Example:
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 OSError:
return False
finally:
del png
return True

def save_full_page_screenshot(self, filename) -> bool:
"""Save a full document screenshot of the current window to a PNG image file.

Args:
filename: The full path you wish to save your screenshot to. This
should end with a `.png` extension.

Returns:
False if there is any IOError, else returns True. Use full paths in your filename.

Example:
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) -> bytes:
"""Get the full document screenshot of the current window as binary data.

Returns:
Binary data of the screenshot.

Example:
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) -> str:
"""Get the full document screenshot of the current window as a base64-encoded string.

Returns:
Base64 encoded string of the screenshot.

Example:
driver.get_full_page_screenshot_as_base64()
"""
return self.execute("FULL_PAGE_SCREENSHOT")["value"]

def download_file(self, *args, **kwargs):
"""Download file functionality is not implemented for Chromium driver."""
raise NotImplementedError
Expand Down
1 change: 1 addition & 0 deletions py/selenium/webdriver/firefox/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import base64
import os
import warnings
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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 filetype


def test_get_full_page_screenshot_as_base64(driver, pages):
pages.load("simpleTest.html")
result = base64.b64decode(driver.get_full_page_screenshot_as_base64())
kind = filetype.guess(result)
assert kind is not None and kind.mime == "image/png"


def test_get_full_page_screenshot_as_png(driver, pages):
pages.load("simpleTest.html")
result = driver.get_full_page_screenshot_as_png()
kind = filetype.guess(result)
assert kind is not None and kind.mime == "image/png"
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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 filetype


def test_get_full_page_screenshot_as_base64(driver, pages):
pages.load("simpleTest.html")
result = base64.b64decode(driver.get_full_page_screenshot_as_base64())
kind = filetype.guess(result)
assert kind is not None and kind.mime == "image/png"


def test_get_full_page_screenshot_as_png(driver, pages):
pages.load("simpleTest.html")
result = driver.get_full_page_screenshot_as_png()
kind = filetype.guess(result)
assert kind is not None and kind.mime == "image/png"
Loading