diff --git a/py/selenium/webdriver/chromium/webdriver.py b/py/selenium/webdriver/chromium/webdriver.py index 484fa132ad74d..9ab606019f50d 100644 --- a/py/selenium/webdriver/chromium/webdriver.py +++ b/py/selenium/webdriver/chromium/webdriver.py @@ -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 @@ -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 diff --git a/py/selenium/webdriver/firefox/webdriver.py b/py/selenium/webdriver/firefox/webdriver.py index fd953edd23e5b..f429ca5db94b0 100644 --- a/py/selenium/webdriver/firefox/webdriver.py +++ b/py/selenium/webdriver/firefox/webdriver.py @@ -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 diff --git a/py/test/selenium/webdriver/chrome/chrome_takes_full_page_screenshots_tests.py b/py/test/selenium/webdriver/chrome/chrome_takes_full_page_screenshots_tests.py new file mode 100644 index 0000000000000..e1274bf6e5116 --- /dev/null +++ b/py/test/selenium/webdriver/chrome/chrome_takes_full_page_screenshots_tests.py @@ -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" diff --git a/py/test/selenium/webdriver/edge/edge_takes_full_page_screenshots_tests.py b/py/test/selenium/webdriver/edge/edge_takes_full_page_screenshots_tests.py new file mode 100644 index 0000000000000..e1274bf6e5116 --- /dev/null +++ b/py/test/selenium/webdriver/edge/edge_takes_full_page_screenshots_tests.py @@ -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"