def testScreenshotTimeout(self): fake_platform = FakeScreenshotTimeoutPlatform() def SetTargetCallCount(target): fake_platform.take_screenshot_call_count = 0 fake_platform.target_screenshot_call_count = target # No timeout. SetTargetCallCount(None) try: fh = screenshot.TryCaptureScreenShot(fake_platform, timeout=None) self.assertEqual(fake_platform.take_screenshot_call_count, 1) finally: os.remove(fh.GetAbsPath()) # Timeout, never succeeds. SetTargetCallCount(None) try: fh = screenshot.TryCaptureScreenShot(fake_platform, timeout=1) self.assertTrue(fake_platform.take_screenshot_call_count >= 2) finally: os.remove(fh.GetAbsPath()) # Timeout, eventual success. SetTargetCallCount(3) try: fh = screenshot.TryCaptureScreenShot(fake_platform, timeout=10) self.assertEqual(fake_platform.take_screenshot_call_count, 3) finally: os.remove(fh.GetAbsPath())
def _CollectScreenshot(self, log_level, suffix, start_time=None): """Helper function to handle the screenshot portion of CollectDebugData. Attempts to take a screenshot at the OS level and save it as an artifact. Args: log_level: The logging level to use from the logging module, e.g. logging.ERROR. suffix: The suffix to prepend to the names of any created artifacts. start_time: If set, prepend elaped time to screenshot path. Should be time at which the test started, as a datetime. This is done here because it may take a nonzero amount of time to take a screenshot. """ screenshot_handle = screenshot.TryCaptureScreenShot( self.browser.platform, timeout=self.screenshot_timeout) if screenshot_handle: with open(screenshot_handle.GetAbsPath(), 'rb') as infile: if start_time: # Prepend time since test started to path test_time = datetime.now() - start_time suffix = str(test_time.total_seconds()).replace( '.', '_') + '-' + suffix artifact_name = posixpath.join('debug_screenshots', 'screenshot-%s' % suffix) logging.log(log_level, 'Saving screenshot as artifact %s', artifact_name) artifact_logger.CreateArtifact(artifact_name, infile.read()) else: logging.log(log_level, 'Failed to capture screenshot')
def DumpStateUponFailure(self, page, results): # Dump browser standard output and log. if self._browser: self._browser.DumpStateUponFailure() else: logging.warning('Cannot dump browser state: No browser.') # Capture a screenshot if self._finder_options.browser_options.take_screenshot_for_failed_page: fh = screenshot.TryCaptureScreenShot(self.platform, self._current_tab) if fh is not None: results.AddProfilingFile(page, fh) else: logging.warning('Taking screenshots upon failures disabled.')
def DumpStateUponStoryRunFailure(self, results): # Dump browser standard output and log. if self._browser: self._browser.DumpStateUponFailure() else: logging.warning('Cannot dump browser state: No browser.') # Capture a screenshot if self._finder_options.browser_options.take_screenshot_for_failed_page: fh = screenshot.TryCaptureScreenShot(self.platform, self._current_tab) if fh is not None: with results.CaptureArtifact('screenshot.png') as path: shutil.move(fh.GetAbsPath(), path) else: logging.warning('Taking screenshots upon failures disabled.')
def testScreenShotTakenSupportedPlatform(self): fake_platform = self.options.fake_possible_browser.returned_browser.platform expected_png_base64 = """ iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91 JpzAAAAFklEQVR4Xg3EAQ0AAABAMP1LY3YI7l8l6A T8tgwbJAAAAABJRU5ErkJggg== """ fake_platform.screenshot_png_data = expected_png_base64 fh = screenshot.TryCaptureScreenShot(fake_platform, None) screenshot_file_path = fh.GetAbsPath() try: actual_screenshot_img = image_util.FromPngFile(screenshot_file_path) self.assertTrue( image_util.AreEqual( image_util.FromBase64Png(expected_png_base64), actual_screenshot_img)) finally: # Must clean up screenshot file if exists. os.remove(screenshot_file_path)
def _CollectScreenshot(self, log_level, suffix): """Helper function to handle the screenshot portion of CollectDebugData. Attempts to take a screenshot at the OS level and save it as an artifact. Args: log_level: The logging level to use from the logging module, e.g. logging.ERROR. suffix: The suffix to append to the names of any created artifacts. """ screenshot_handle = screenshot.TryCaptureScreenShot(self.browser.platform) if screenshot_handle: with open(screenshot_handle.GetAbsPath(), 'rb') as infile: artifact_name = posixpath.join( 'debug_screenshots', 'screenshot-%s' % suffix) logging.log( log_level, 'Saving screenshot as artifact %s', artifact_name) artifact_logger.CreateArtifact(artifact_name, infile.read()) else: logging.log(log_level, 'Failed to capture screenshot')
def _CollectScreenshot(self, log_level): """Helper function to handle the screenshot portion of CollectDebugData. Attempts to take a screenshot at the OS level and save it as an artifact. Args: log_level: The logging level to use from the logging module, e.g. logging.ERROR. """ screenshot_handle = screenshot.TryCaptureScreenShot( self.browser.platform) if screenshot_handle: with open(screenshot_handle.GetAbsPath(), 'rb') as infile: now = datetime.datetime.now() suffix = now.strftime('%Y-%m-%d-%H-%M-%S') artifact_name = posixpath.join('debug_screenshots', 'screenshot-' + suffix) logging.log(log_level, 'Saving screenshot as artifact %s', artifact_name) artifact_logger.CreateArtifact(artifact_name, infile.read()) else: logging.log(log_level, 'Failed to capture screenshot')