diff options
Diffstat (limited to 'testing/mozbase/mozscreenshot')
-rw-r--r-- | testing/mozbase/mozscreenshot/mozscreenshot/__init__.py | 111 | ||||
-rw-r--r-- | testing/mozbase/mozscreenshot/setup.cfg | 2 | ||||
-rw-r--r-- | testing/mozbase/mozscreenshot/setup.py | 32 |
3 files changed, 145 insertions, 0 deletions
diff --git a/testing/mozbase/mozscreenshot/mozscreenshot/__init__.py b/testing/mozbase/mozscreenshot/mozscreenshot/__init__.py new file mode 100644 index 0000000000..0e87f685c6 --- /dev/null +++ b/testing/mozbase/mozscreenshot/mozscreenshot/__init__.py @@ -0,0 +1,111 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at http://mozilla.org/MPL/2.0/. + +from __future__ import absolute_import, print_function + +import os +import mozinfo +import tempfile +import subprocess +from mozlog.formatters.process import strstatus + + +def printstatus(name, returncode): + """ + print the status of a command exit code, formatted for tbpl. + + Note that mozlog structured action "process_exit" should be used + instead of that in new code. + """ + print("TEST-INFO | %s: %s" % (name, strstatus(returncode))) + + +def dump_screen(utilityPath, log, prefix="mozilla-test-fail-screenshot_"): + """dumps a screenshot of the entire screen to a directory specified by + the MOZ_UPLOAD_DIR environment variable. + + :param utilityPath: Path of utility programs. This is typically a path + to either the objdir's bin directory or a path to the host utilities. + :param log: Reference to logger. + """ + + is_structured_log = hasattr(log, "process_exit") + + # Need to figure out which OS-dependent tool to use + if mozinfo.isUnix: + utility = [os.path.join(utilityPath, "screentopng")] + utilityname = "screentopng" + elif mozinfo.isMac: + utility = ["/usr/sbin/screencapture", "-C", "-x", "-t", "png"] + utilityname = "screencapture" + elif mozinfo.isWin: + utility = [os.path.join(utilityPath, "screenshot.exe")] + utilityname = "screenshot" + + # Get dir where to write the screenshot file + parent_dir = os.environ.get("MOZ_UPLOAD_DIR", None) + if not parent_dir: + log.info("Failed to retrieve MOZ_UPLOAD_DIR env var") + return + + # Run the capture + try: + tmpfd, imgfilename = tempfile.mkstemp( + prefix=prefix, suffix=".png", dir=parent_dir + ) + os.close(tmpfd) + if is_structured_log: + log.process_start(utilityname) + returncode = subprocess.call(utility + [imgfilename]) + if is_structured_log: + log.process_exit(utilityname, returncode) + else: + printstatus(utilityname, returncode) + except OSError as err: + log.info("Failed to start %s for screenshot: %s" % (utility[0], err.strerror)) + + +def dump_device_screen(device, log, prefix="mozilla-test-fail-screenshot_"): + """dumps a screenshot of a real device's entire screen to a directory + specified by the MOZ_UPLOAD_DIR environment variable. Cloned from + mozscreenshot.dump_screen. + + :param device: Reference to an ADBDevice object which provides the + interface to interact with Android devices. + :param log: Reference to logger. + """ + + utilityname = "screencap" + is_structured_log = hasattr(log, "process_exit") + + # Get dir where to write the screenshot file + parent_dir = os.environ.get("MOZ_UPLOAD_DIR", None) + if not parent_dir: + log.info("Failed to retrieve MOZ_UPLOAD_DIR env var") + return + + # Run the capture + try: + # Android 6.0 and later support mktemp. See + # https://android.googlesource.com/platform/system/core/ + # +/master/shell_and_utilities/README.md#android-6_0-marshmallow + # We can use mktemp on real devices since we do not test on + # real devices older than Android 6.0. Note we must create the + # file without an extension due to limitations in mktemp. + filename = device.shell_output( + "mktemp -p %s %sXXXXXX" % (device.test_root, prefix) + ) + pngfilename = filename + ".png" + device.mv(filename, pngfilename) + if is_structured_log: + log.process_start(utilityname) + device.shell_output("%s -p %s" % (utilityname, pngfilename)) + if is_structured_log: + log.process_exit(utilityname, 0) + else: + printstatus(utilityname, 0) + device.pull(pngfilename, parent_dir) + device.rm(pngfilename) + except Exception as err: + log.info("Failed to start %s for screenshot: %s" % (utilityname, str(err))) diff --git a/testing/mozbase/mozscreenshot/setup.cfg b/testing/mozbase/mozscreenshot/setup.cfg new file mode 100644 index 0000000000..3c6e79cf31 --- /dev/null +++ b/testing/mozbase/mozscreenshot/setup.cfg @@ -0,0 +1,2 @@ +[bdist_wheel] +universal=1 diff --git a/testing/mozbase/mozscreenshot/setup.py b/testing/mozbase/mozscreenshot/setup.py new file mode 100644 index 0000000000..7131ae61fd --- /dev/null +++ b/testing/mozbase/mozscreenshot/setup.py @@ -0,0 +1,32 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at http://mozilla.org/MPL/2.0/. + +from __future__ import absolute_import + +from setuptools import setup + + +PACKAGE_NAME = "mozscreenshot" +PACKAGE_VERSION = "1.0.0" + + +setup( + name=PACKAGE_NAME, + version=PACKAGE_VERSION, + description="Library for taking screenshots in tests harness", + long_description="see https://firefox-source-docs.mozilla.org/mozbase/index.html", + classifiers=[ + "Programming Language :: Python :: 2.7", + "Programming Language :: Python :: 3.5", + ], + # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers + keywords="mozilla", + author="Mozilla Automation and Tools team", + author_email="tools@lists.mozilla.org", + url="https://wiki.mozilla.org/Auto-tools/Projects/Mozbase", + license="MPL", + packages=["mozscreenshot"], + zip_safe=False, + install_requires=["mozlog", "mozinfo"], +) |