Bug 1213008 - Add Marionette client option to save screenshot to a file. r=ato
authorVangelis Katsikaros <vkatsikaros@gmail.com>
Sat, 11 Aug 2018 11:10:39 +0000
changeset 492072 cae8cdc1e24a7f5cc440010b74aee9b6ab28435a
parent 492071 b87c8b35c101787bf52f2128c61965aefd955937
child 492073 c3a6cf6cf350922c0f0308ee08a512007349b59d
push id1815
push userffxbld-merge
push dateMon, 15 Oct 2018 10:40:45 +0000
treeherdermozilla-release@18d4c09e9378 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato
bugs1213008
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1213008 - Add Marionette client option to save screenshot to a file. r=ato
testing/marionette/client/marionette_driver/marionette.py
testing/marionette/harness/marionette_harness/tests/unit/test_screenshot.py
--- a/testing/marionette/client/marionette_driver/marionette.py
+++ b/testing/marionette/client/marionette_driver/marionette.py
@@ -1884,16 +1884,29 @@ class Marionette(object):
 
         This is the equivalent of calling `document.cookie` and
         parsing the result.
 
         :returns: A list of cookies for the current domain.
         """
         return self._send_message("WebDriver:GetCookies")
 
+    def save_screenshot(self, fh, element=None, highlights=None,
+                        full=True, scroll=True):
+        """Takes a screenhot of a web element or the current frame and
+        saves it in the filehandle.
+
+        It is a wrapper around screenshot()
+        :param fh: The filehandle to save the screenshot at.
+
+        The rest of the parameters are defined like in screenshot()
+        """
+        data = self.screenshot(element, highlights, "binary", full, scroll)
+        fh.write(data)
+
     def screenshot(self, element=None, highlights=None, format="base64",
                    full=True, scroll=True):
         """Takes a screenshot of a web element or the current frame.
 
         The screen capture is returned as a lossless PNG image encoded
         as a base 64 string by default. If the `element` argument is defined the
         capture area will be limited to the bounding box of that
         element.  Otherwise, the capture area will be the bounding box
--- a/testing/marionette/harness/marionette_harness/tests/unit/test_screenshot.py
+++ b/testing/marionette/harness/marionette_harness/tests/unit/test_screenshot.py
@@ -3,16 +3,17 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import absolute_import
 
 import base64
 import hashlib
 import imghdr
 import struct
+import tempfile
 import urllib
 
 from marionette_driver import By
 from marionette_driver.errors import NoSuchElementException, NoSuchWindowException
 from marionette_harness import (
     MarionetteTestCase,
     skip,
     skip_if_mobile,
@@ -390,16 +391,25 @@ class TestScreenCaptureContent(WindowMan
 
         # Highlighting a sub element
         paragraph = self.marionette.find_element(By.ID, "green")
         screenshot_highlight_paragraph = self.marionette.screenshot(element=element,
                                                                     highlights=[paragraph])
         self.assertNotEqual(screenshot_element, screenshot_highlight_paragraph)
         self.assertNotEqual(screenshot_highlight, screenshot_highlight_paragraph)
 
+    def test_save_screenshot(self):
+        expected = self.marionette.screenshot(format="binary")
+        with tempfile.TemporaryFile('w+b') as fh:
+            self.marionette.save_screenshot(fh)
+            fh.flush()
+            fh.seek(0)
+            content = fh.read()
+            self.assertEqual(expected, content)
+
     def test_scroll_default(self):
         self.marionette.navigate(long)
         before = self.page_y_offset
         el = self.marionette.find_element(By.TAG_NAME, "p")
         self.marionette.screenshot(element=el, format="hash")
         self.assertNotEqual(before, self.page_y_offset)
 
     def test_scroll(self):