def _GenerateBrowserProfile(number_of_tabs): """ Generate a browser profile which browser had |number_of_tabs| number of tabs opened before it was closed. Returns: profile_dir: the directory of profile. """ profile_dir = tempfile.mkdtemp() options = options_for_unittests.GetCopy() options.browser_options.output_profile_path = profile_dir browser_to_create = browser_finder.FindBrowser(options) browser_to_create.platform.network_controller.Open() try: with browser_to_create.Create(options) as browser: browser.platform.SetHTTPServerDirectories(path.GetUnittestDataDir()) blank_file_path = os.path.join(path.GetUnittestDataDir(), 'blank.html') blank_url = browser.platform.http_server.UrlOf(blank_file_path) browser.foreground_tab.Navigate(blank_url) browser.foreground_tab.WaitForDocumentReadyStateToBeComplete() for _ in xrange(number_of_tabs - 1): tab = browser.tabs.New() tab.Navigate(blank_url) tab.WaitForDocumentReadyStateToBeComplete() return profile_dir finally: browser_to_create.platform.network_controller.Close()
def testSnappingSimplePage(self): self.platform.SetHTTPServerDirectories(path.GetUnittestDataDir()) html_file_path = os.path.join(path.GetUnittestDataDir(), 'green_rect.html') url = self.platform.http_server.UrlOf(html_file_path) outfile = StringIO.StringIO() snap_page_util.SnapPage(self.finder_options, url, interactive=False, snapshot_file=outfile) self.assertIn('id="green"', outfile.getvalue())
def testSnappingSimplePage(self): self.platform.SetHTTPServerDirectories(path.GetUnittestDataDir()) html_file_path = os.path.join(path.GetUnittestDataDir(), 'green_rect.html') url = self.platform.http_server.UrlOf(html_file_path) outfile = StringIO.StringIO() test_dir = tempfile.mkdtemp() try: snap_page_util._SnapPageToFile( self.finder_options, url, interactive=False, snapshot_path=os.path.join(test_dir, 'page.html'), snapshot_file=outfile, enable_browser_log=False) self.assertIn('id="green"', outfile.getvalue()) finally: shutil.rmtree(test_dir)
def testSnappingPageWithImage(self, mock_urlopen): test_dir = tempfile.mkdtemp() try: src_html_filename = 'image.html' dest_html_path = os.path.join(test_dir, src_html_filename) shutil.copyfile( os.path.join(path.GetUnittestDataDir(), src_html_filename), dest_html_path) self.platform.SetHTTPServerDirectories(path.GetUnittestDataDir()) url = self.platform.http_server.UrlOf( os.path.join(path.GetUnittestDataDir(), src_html_filename)) outfile = StringIO.StringIO() # Load the test image file's content so that we can return it # from the mocked url request as if we'd actually fetched the # image from an external source. request_response = FakeResponse() expected_image_path = os.path.join(path.GetUnittestDataDir(), 'image.png') with open(expected_image_path, 'rb') as image_file: request_response.content = image_file.read() # Mock out the external image url fetch to return the loaded # test image content. mock_urlopen.return_value = request_response snap_page_util._SnapPageToFile(self.finder_options, url, interactive=False, snapshot_path=os.path.join( test_dir, src_html_filename), snapshot_file=outfile, enable_browser_log=False) self.assertIn('id="target"', outfile.getvalue()) # Validate that the 'fetched' image was written to the # destination local image path. expected_fetched_md5 = hashlib.md5( request_response.content).hexdigest() actual_fetched_md5 = None with open(os.path.join(test_dir, 'image', '0-target.png'), 'rb') as f: actual_fetched_md5 = hashlib.md5(f.read()).hexdigest() self.assertEqual(expected_fetched_md5, actual_fetched_md5) finally: shutil.rmtree(test_dir)
def run(self, test, progress_reporters, repeat_count, args): sys.path.append(path.GetUnittestDataDir()) result = TestResult(progress_reporters) result.startTestRun() try: options_for_unittests.Push(args) for _ in xrange(repeat_count): test(result) finally: options_for_unittests.Pop() result.stopTestRun() return result
def UrlOfUnittestFile(cls, filename, handler_class=None): cls._platform.SetHTTPServerDirectories(path.GetUnittestDataDir(), handler_class) file_path = os.path.join(path.GetUnittestDataDir(), filename) return cls._platform.http_server.UrlOf(file_path)
def UrlOfUnittestFile(cls, filename): cls._browser.SetHTTPServerDirectories(path.GetUnittestDataDir()) file_path = os.path.join(path.GetUnittestDataDir(), filename) return cls._browser.http_server.UrlOf(file_path)