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: story_runner.Run(DummyTest(), story_set, self.options, 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 _DownloadFromCloudStorage(self, img_name, page, tab): """Downloads the reference image for the given test from cloud storage, returning it as a Telemetry Bitmap object.""" # TODO(kbr): there's a race condition between the deletion of the # temporary file and gsutil's overwriting it. if not self.options.refimg_cloud_storage_bucket: raise Exception('--refimg-cloud-storage-bucket argument is required') temp_file = tempfile.NamedTemporaryFile(suffix='.png').name cloud_storage.Get(self.options.refimg_cloud_storage_bucket, self._FormatReferenceImageName(img_name, page, tab), temp_file) return image_util.FromPngFile(temp_file)
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(options) story_runner.Run(DummyTest(), story_set, options, results, 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 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)) failing_page = FailingTestPage('chrome://version', story_set) 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) 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 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 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 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)
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 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 _GetReferenceImage(self, img_dir, img_name, cur_revision, screenshot): if not cur_revision: cur_revision = 0 image_path = os.path.join(img_dir, img_name) self._DeleteOldReferenceImages(image_path, cur_revision) image_path = image_path + '_' + str(cur_revision) + '.png' try: ref_png = image_util.FromPngFile(image_path) except: ref_png = None if ref_png is not None: return ref_png print 'Reference image not found. Writing tab contents as reference.' self._WriteImage(image_path, screenshot) return screenshot
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 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 _GetReferenceImage(self, img_dir, img_name, cur_revision, screenshot): if not cur_revision: cur_revision = 0 image_path = os.path.join(img_dir, img_name) self._DeleteOldReferenceImages(image_path, cur_revision) image_path = image_path + '_v' + str(cur_revision) + '.png' try: ref_png = image_util.FromPngFile(image_path) # This can raise a couple of exceptions including IOError and ValueError. except Exception: ref_png = None if ref_png is not None: return ref_png print ('Reference image not found. Writing tab contents as reference to: ' + image_path) self._WriteImage(image_path, screenshot) return screenshot
def testWritePngWithoutPngSuffixThrows(self): orig = image_util.FromPngFile(test_png_path) temp_file = tempfile.NamedTemporaryFile().name self.assertRaises(AssertionError, image_util.WritePngFile, orig, temp_file)
def testWritePngToPngFile(self): orig = image_util.FromPngFile(test_png_path) temp_file = tempfile.NamedTemporaryFile(suffix='.png').name image_util.WritePngFile(orig, temp_file) new_file = image_util.FromPngFile(temp_file) self.assertTrue(image_util.AreEqual(orig, new_file, likely_equal=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))