def IsScreenshotWithinDynamicContentThreshold(base_screenshot, next_screenshot, content_pixels, num_total_pixels, dynamic_content_threshold): """Compares two screenshot images and updates the bytearray of RGB values that will be used to create the final output image if it finds an inconsistency. Returns False if the percentage of pixels with dynamic content is greater than the given threshold, and True otherwise.""" base_pixels = image_util.Pixels(base_screenshot) next_pixels = image_util.Pixels(next_screenshot) num_dynamic_pixels = 0.0 i = 0 while i < len(base_pixels): # If the RGB values of the two screenshots do not match, make the # corresponding pixel in the accumulated bytearray cyan and increment the # count of dynamic pixels. if (base_pixels[i] != next_pixels[i] or base_pixels[i+1] != next_pixels[i+1] or base_pixels[i+2] != next_pixels[i+2]): content_pixels[i] = 0 content_pixels[i+1] = 255 content_pixels[i+2] = 255 num_dynamic_pixels += 1 i += 3 if (num_dynamic_pixels / num_total_pixels) >= dynamic_content_threshold: logging.error("Percentage of pixels with dynamic content: %f", num_dynamic_pixels / num_total_pixels) return False return True
def testScreenShotTakenForFailedPage(self): self.CaptureFormattedException() platform_screenshot_supported = [False] tab_screenshot_supported = [False] chrome_version_screen_shot = [None] class FailingTestPage(page_module.Page): def RunNavigateSteps(self, action_runner): action_runner.Navigate(self._url) platform_screenshot_supported[0] = ( action_runner.tab.browser.platform.CanTakeScreenshot) tab_screenshot_supported[ 0] = action_runner.tab.screenshot_supported if not platform_screenshot_supported[ 0] and tab_screenshot_supported[0]: chrome_version_screen_shot[ 0] = action_runner.tab.Screenshot() 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) with tempfile_ext.NamedTemporaryDirectory() as tempdir: options = options_for_unittests.GetCopy() options.output_dir = tempdir options.output_formats = ['none'] options.browser_options.take_screenshot_for_failed_page = True options.suppress_gtest_report = True SetUpStoryRunnerArguments(options) results = results_options.CreateResults(EmptyMetadataForTest(), options) story_runner.Run(DummyTest(), story_set, options, results, max_failures=2, metadata=EmptyMetadataForTest()) self.assertTrue(results.had_failures) if not platform_screenshot_supported[ 0] and tab_screenshot_supported[0]: 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 = image_util.FromPngFile( screenshot_file_path) self.assertEquals( image_util.Pixels(chrome_version_screen_shot[0]), image_util.Pixels(actual_screenshot))
def testScreenShotTakenForFailedPage(self): self.CaptureFormattedException() platform_screenshot_supported = [False] tab_screenshot_supported = [False] chrome_version_screen_shot = [None] class FailingTestPage(page_module.Page): def RunNavigateSteps(self, action_runner): action_runner.Navigate(self._url) platform_screenshot_supported[0] = ( action_runner.tab.browser.platform.CanTakeScreenshot) tab_screenshot_supported[ 0] = action_runner.tab.screenshot_supported if not platform_screenshot_supported[ 0] and tab_screenshot_supported[0]: chrome_version_screen_shot[ 0] = action_runner.tab.Screenshot() 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) options = options_for_unittests.GetCopy() options.output_formats = ['none'] options.browser_options.take_screenshot_for_failed_page = True options.suppress_gtest_report = True SetUpStoryRunnerArguments(options) results = results_options.CreateResults(EmptyMetadataForTest(), options) story_runner.Run(DummyTest(), story_set, options, results, max_failures=2, metadata=EmptyMetadataForTest()) self.assertEquals(1, len(results.failures)) if not platform_screenshot_supported[0] and tab_screenshot_supported[0]: 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 = image_util.FromPngFile( screenshot_file_path) self.assertEquals( image_util.Pixels(chrome_version_screen_shot[0]), image_util.Pixels(actual_screenshot)) finally: # Must clean up screenshot file if exists. os.remove(screenshot_file_path)
def CheckScreenshot(): canvasRGB = []; for i in range(0, 3): canvasRGB.append(random.randint(0, 255)) tab.EvaluateJavaScript("window.draw(%d, %d, %d);" % tuple(canvasRGB)) screenshot = tab.Screenshot(5) CheckColorMatch(canvasRGB, image_util.Pixels(screenshot))
def testScreenshot(self): if not self._platform.CanTakeScreenshot(): self.skipTest( 'Platform does not support screenshots, skipping test.') # Skip the test on Mac 10.5, 10.6, 10.7 because png format isn't # recognizable on Mac < 10.8 (crbug.com/369490#c13) if (self._platform.GetOSName() == 'mac' and self._platform.GetOSVersionName() in (os_version.LEOPARD, os_version.SNOWLEOPARD, os_version.LION)): self.skipTest('OS X version %s too old' % self._platform.GetOSName()) tf = tempfile.NamedTemporaryFile(delete=False, suffix='.png') tf.close() try: self.Navigate('screenshot_test.html') self._platform.TakeScreenshot(tf.name) # Assert that screenshot image contains the color of the triangle defined # in screenshot_test.html. img = image_util.FromPngFile(tf.name) screenshot_pixels = image_util.Pixels(img) special_colored_pixel = bytearray([217, 115, 43]) self.assertTrue(special_colored_pixel in screenshot_pixels) finally: os.remove(tf.name)
def ValidateAndMeasurePage(self, page, tab, results): try: tab.WaitForDocumentReadyStateToBeComplete() except py_utils.TimeoutException: logging.warning("WaitForDocumentReadyStateToBeComplete() timeout, " "page: %s", page.name) return time.sleep(self._wait_time) if not os.path.exists(self._png_outdir): logging.info("Creating directory %s", self._png_outdir) try: os.makedirs(self._png_outdir) except OSError: logging.warning("Directory %s could not be created", self._png_outdir) raise outpath = os.path.abspath( os.path.join(self._png_outdir, page.file_safe_name)) + '.png' # Replace win32 path separator char '\' with '\\'. outpath = outpath.replace('\\', '\\\\') screenshot = tab.Screenshot() image_width = image_util.Width(screenshot) image_height = image_util.Height(screenshot) num_total_pixels = image_width * image_height content_pixels = image_util.Pixels(screenshot) # Dynamic content flag. if self._dc_detect: for i in range(self._dc_extra_screenshots): logging.info("Sleeping for %f seconds.", self._dc_wait_time) time.sleep(self._dc_wait_time) # After the specified wait time, take another screenshot of the page. logging.info("Taking extra screenshot %d of %d.", i+1, self._dc_extra_screenshots) next_screenshot = tab.Screenshot() # Compare this screenshot to the original to mark inconsistent pixels, # and check the percentage of dynamic content against the threshold. if not IsScreenshotWithinDynamicContentThreshold( screenshot, next_screenshot, content_pixels, num_total_pixels, self._dc_threshold): raise legacy_page_test.MeasurementFailure("Percentage of pixels " "with dynamic content is greater than threshold.") # Convert the pixel bytearray back into an image. image = image_util.FromRGBPixels(image_width, image_height, content_pixels) # TODO(lchoi): Add logging to image_util.py and/or augment error handling of # image_util.WritePngFile logging.info("Writing PNG file to %s. This may take awhile.", outpath) start = time.time() image_util.WritePngFile(image, outpath) logging.info("PNG file written successfully. (Took %f seconds)", time.time()-start)
def testCaptureScreenshot(self): if not self._tab.screenshot_supported: return self.Navigate('green_rect.html') res = image_util.Pixels(self._tab.Screenshot()) self.assertEquals(0x00, res[0]) self.assertEquals(0xFF, res[1]) self.assertEquals(0x00, res[2])
def testScreenShotTakenForFailedPage(self): self.CaptureFormattedException() platform_screenshot_supported = [False] tab_screenshot_supported = [False] chrome_version_screen_shot = [None] class FailingTestPage(page_module.Page): def RunNavigateSteps(self, action_runner): action_runner.Navigate(self._url) platform_screenshot_supported[0] = ( action_runner.tab.browser.platform.CanTakeScreenshot) tab_screenshot_supported[ 0] = action_runner.tab.screenshot_supported if not platform_screenshot_supported[ 0] and tab_screenshot_supported[0]: chrome_version_screen_shot[ 0] = action_runner.tab.Screenshot() 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 results = RunStorySet(DummyTest(), story_set, self.options, max_failures=2) self.assertTrue(results.had_failures) if not platform_screenshot_supported[0] and tab_screenshot_supported[0]: failed_run = next(run for run in results.IterStoryRuns() if run.story.name == failing_page.name) screenshot_file_path = failed_run.GetArtifact( 'screenshot').local_path actual_screenshot = image_util.FromPngFile(screenshot_file_path) self.assertEquals(image_util.Pixels(chrome_version_screen_shot[0]), image_util.Pixels(actual_screenshot))
def testCrop(self): pixels = [0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 0, 2, 0, 1, 2, 0, 2, 2, 0, 3, 2, 0] bmp = image_util.FromRGBPixels(4, 3, pixels) bmp = image_util.Crop(bmp, 1, 2, 2, 1) self.assertEquals(2, image_util.Width(bmp)) self.assertEquals(1, image_util.Height(bmp)) image_util.GetPixelColor(bmp, 0, 0).AssertIsRGB(1, 2, 0) image_util.GetPixelColor(bmp, 1, 0).AssertIsRGB(2, 2, 0) self.assertEquals(image_util.Pixels(bmp), bytearray([1, 2, 0, 2, 2, 0]))
def testScreenshot(self): # Screenshots for Cluster Telemetry purposes currently only supported on # Linux platform. screenshot_test = screenshot.Screenshot(self.options.output_dir) self.RunPageTest(screenshot_test, 'file://screenshot_test.html') filepath = os.path.join(self.options.output_dir, 'screenshot_test.png') self.assertTrue(os.path.exists(filepath)) self.assertTrue(os.path.isfile(filepath)) self.assertTrue(os.access(filepath, os.R_OK)) image = image_util.FromPngFile(filepath) screenshot_pixels = image_util.Pixels(image) special_colored_pixel = bytearray([217, 115, 43]) self.assertTrue(special_colored_pixel in screenshot_pixels)
def testScreenshot(self): # Screenshots for Cluster Telemetry purposes currently only supported on # Linux platform. page_set = self.CreateStorySetFromFileInUnittestDataDir( 'screenshot_test.html') measurement = screenshot.Screenshot(self._png_outdir) self.RunMeasurement(measurement, page_set, options=self._options) path = self._png_outdir + '/' + page_set.stories[ 0].file_safe_name + '.png' self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.isfile(path)) self.assertTrue(os.access(path, os.R_OK)) image = image_util.FromPngFile(path) screenshot_pixels = image_util.Pixels(image) special_colored_pixel = bytearray([217, 115, 43]) self.assertTrue(special_colored_pixel in screenshot_pixels)