def _CaptureScreenshotImpl(self, request, timeout): # "Google API are missing..." infobar might cause a viewport resize # which invalidates screenshot request. See crbug.com/459820. for _ in range(2): res = self._inspector_websocket.SyncRequest(request, timeout) if res and ('result' in res) and ('data' in res['result']): return image_util.FromBase64Png(res['result']['data']) return None
def testReadFromBase64Png(self): bmp = image_util.FromBase64Png(test_png) self.assertEquals(2, image_util.Width(bmp)) self.assertEquals(2, image_util.Height(bmp)) image_util.GetPixelColor(bmp, 0, 0).AssertIsRGB(255, 0, 0) image_util.GetPixelColor(bmp, 1, 1).AssertIsRGB(0, 255, 0) image_util.GetPixelColor(bmp, 0, 1).AssertIsRGB(0, 0, 255) image_util.GetPixelColor(bmp, 1, 0).AssertIsRGB(255, 255, 0)
def testScreenShotTakenForFailedPageOnSupportedPlatform(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 self.options.browser_options.take_screenshot_for_failed_page = True class FailingTestPage(page_module.Page): def RunNavigateSteps(self, action_runner): raise exceptions.AppCrashException story_set = story.StorySet() story_set.AddStory(page_module.Page('file://blank.html', story_set, name='blank.html')) failing_page = FailingTestPage('chrome://version', story_set, name='failing') story_set.AddStory(failing_page) self.options.output_formats = ['json-test-results'] with tempfile_ext.NamedTemporaryDirectory() as tempdir: self.options.output_dir = tempdir results = results_options.CreateResults( EmptyMetadataForTest(), self.options) # This ensures the output stream used by the json test results object is # closed. On windows, we can't delete the temp directory if a file in that # directory is still in use. with results: possible_browser = browser_finder.FindBrowser(self.options) story_runner.RunStorySet( test=DummyTest(), story_set=story_set, possible_browser=possible_browser, expectations=None, browser_options=self.options.browser_options, finder_options=self.options, results=results, max_failures=2, ) self.assertTrue(results.had_failures) artifacts = results._artifact_results.GetTestArtifacts( failing_page.name) self.assertIsNotNone(artifacts) self.assertIn('screenshot', artifacts) screenshot_file_path = os.path.join(tempdir, artifacts['screenshot'][0]) actual_screenshot_img = image_util.FromPngFile(screenshot_file_path) self.assertTrue( image_util.AreEqual( image_util.FromBase64Png(expected_png_base64), actual_screenshot_img))
def CaptureScreenshot(self, timeout=60): request = { 'method': 'Page.captureScreenshot', # TODO(rmistry): when Chrome is running in headless mode, this # will need to pass True. Telemetry needs to understand # whether the browser is in headless mode, and pass that # knowledge down to this method. 'fromSurface': False } # "Google API are missing..." infobar might cause a viewport resize # which invalidates screenshot request. See crbug.com/459820. for _ in range(2): res = self._inspector_websocket.SyncRequest(request, timeout) if res and ('result' in res) and ('data' in res['result']): return image_util.FromBase64Png(res['result']['data']) return None
def testScreenShotTakenForFailedPageOnSupportedPlatform(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 self.options.browser_options.take_screenshot_for_failed_page = True class FailingTestPage(page_module.Page): def RunNavigateSteps(self, action_runner): raise exceptions.AppCrashException story_set = story.StorySet() story_set.AddStory( page_module.Page('file://blank.html', story_set, name='blank.html')) failing_page = FailingTestPage('chrome://version', story_set, name='failing') story_set.AddStory(failing_page) results = results_options.CreateResults(EmptyMetadataForTest(), self.options) story_runner.Run(DummyTest(), story_set, self.options, results, max_failures=2, metadata=EmptyMetadataForTest()) self.assertEquals(1, len(results.failures)) self.assertEquals(1, len(results.pages_to_profiling_files)) self.assertIn(failing_page, results.pages_to_profiling_files) screenshot_file_path = ( results.pages_to_profiling_files[failing_page][0].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 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 testScreenShotTakenForFailedPageOnSupportedPlatform(self): expected_png_base64 = ('iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91' 'JpzAAAAFklEQVR4Xg3EAQ0AAABAMP1LY3YI7l8l6A' 'T8tgwbJAAAAABJRU5ErkJggg==') self.fake_platform.screenshot_png_data = expected_png_base64 # After setting up some fake data, now the platform supports screenshots. self.assertTrue(self.fake_platform.CanTakeScreenshot()) self.options.browser_options.take_screenshot_for_failed_page = True story_set = test_stories.SinglePageStorySet( story_run_side_effect=exceptions.AppCrashException(msg='fake crash')) results = self.RunStorySetAndGetResults(story_set) self.assertEqual(results['status'], 'FAIL') self.assertIn('screenshot.png', results['outputArtifacts']) actual_screenshot_img = image_util.FromPngFile( results['outputArtifacts']['screenshot.png']['filePath']) self.assertTrue( image_util.AreEqual( image_util.FromBase64Png(expected_png_base64), actual_screenshot_img))
def testScreenShotTakenForFailedPageOnSupportedPlatform(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 class FailingTestPage(page_module.Page): def RunNavigateSteps(self, action_runner): raise exceptions.AppCrashException story_set = story.StorySet() story_set.AddStory( page_module.Page('file://blank.html', story_set, name='blank.html')) failing_page = FailingTestPage('chrome://version', story_set, name='failing') story_set.AddStory(failing_page) self.options.browser_options.take_screenshot_for_failed_page = True self.options.output_formats = ['json-test-results'] results = RunStorySet(DummyTest(), story_set, self.options, max_failures=2) self.assertTrue(results.had_failures) failed_run = next(run for run in results.IterStoryRuns() if run.story.name == failing_page.name) screenshot_file_path = failed_run.GetArtifact( 'screenshot.png').local_path actual_screenshot_img = image_util.FromPngFile(screenshot_file_path) self.assertTrue( image_util.AreEqual(image_util.FromBase64Png(expected_png_base64), actual_screenshot_img))
def Screenshot(self): assert self.screenshot_supported, 'Screenshot is not supported' return image_util.FromBase64Png(self.test_png)
def TakeScreenshot(self, file_path): if not self.CanTakeScreenshot(): raise NotImplementedError img = image_util.FromBase64Png(self.screenshot_png_data) image_util.WritePngFile(img, file_path) return True
def testIsEqual(self): bmp = image_util.FromBase64Png(test_png) file_bmp = image_util.FromPngFile(test_png_path) self.assertTrue(image_util.AreEqual(bmp, file_bmp, likely_equal=True))