def testOneTab(self): ps = page_set.PageSet() page = page_module.Page( 'file:///' + os.path.join('..', '..', 'unittest_data', 'blank.html'), ps, base_dir=os.path.dirname(__file__)) ps.pages.append(page) class TestOneTab(page_test.PageTest): def __init__(self, test_method_name, action_name_to_run='', needs_browser_restart_after_each_run=False): super(TestOneTab, self).__init__(test_method_name, action_name_to_run, needs_browser_restart_after_each_run) self._browser = None def SetUpBrowser(self, browser): self._browser = browser self._browser.tabs.New() def RunTest(self, page, tab, results): # pylint: disable=W0613,R0201 assert len(self._browser.tabs) == 1 test = TestOneTab('RunTest') with page_runner.PageRunner(ps) as runner: options = options_for_unittests.GetCopy() possible_browser = browser_finder.FindBrowser(options) results = page_test.PageTestResults() runner.Run(options, possible_browser, test, results)
def testUserAgent(self): ps = page_set.PageSet() page = page_module.Page( 'file:///' + os.path.join('..', '..', 'unittest_data', 'blank.html'), ps, base_dir=os.path.dirname(__file__)) ps.pages.append(page) ps.user_agent_type = 'tablet' class TestUserAgent(page_test.PageTest): def RunTest(self, page, tab, results): # pylint: disable=W0613,R0201 actual_user_agent = tab.EvaluateJavaScript( 'window.navigator.userAgent') expected_user_agent = user_agent.UA_TYPE_MAPPING['tablet'] assert actual_user_agent.strip() == expected_user_agent # This is so we can check later that the test actually made it into this # function. Previously it was timing out before even getting here, which # should fail, but since it skipped all the asserts, it slipped by. self.hasRun = True # pylint: disable=W0201 test = TestUserAgent('RunTest') with page_runner.PageRunner(ps) as runner: options = options_for_unittests.GetCopy() possible_browser = browser_finder.FindBrowser(options) results = page_test.PageTestResults() runner.Run(options, possible_browser, test, results) self.assertTrue(hasattr(test, 'hasRun') and test.hasRun)
def Main(benchmark_dir): benchmarks = discover.DiscoverClasses(benchmark_dir, os.path.join(benchmark_dir, '..'), page_benchmark.PageBenchmark) options = browser_options.BrowserOptions() parser = options.CreateParser('%prog <page_set>') page_runner.PageRunner.AddCommandLineOptions(parser) recorder = RecordPage(benchmarks) recorder.AddCommandLineOptions(parser) _, args = parser.parse_args() if len(args) != 1: parser.print_usage() sys.exit(1) ps = page_set.PageSet.FromFile(args[0]) # Set the archive path to something temporary. temp_target_wpr_file_path = tempfile.mkstemp()[1] ps.wpr_archive_info.AddNewTemporaryRecording(temp_target_wpr_file_path) # Do the actual recording. options.wpr_mode = wpr_modes.WPR_RECORD recorder.CustomizeBrowserOptions(options) possible_browser = browser_finder.FindBrowser(options) if not possible_browser: print >> sys.stderr, """No browser found.\n Use --browser=list to figure out which are available.\n""" sys.exit(1) results = page_test.PageTestResults() with page_runner.PageRunner(ps) as runner: runner.Run(options, possible_browser, recorder, results) if results.page_failures: logging.warning( 'Some pages failed. The recording has not been updated for ' 'these pages.') logging.warning( 'Failed pages: %s', '\n'.join( [failure['page'].url for failure in results.page_failures])) if results.skipped_pages: logging.warning('Some pages were skipped. The recording has not been ' 'updated for these pages.') logging.warning( 'Skipped pages: %s', '\n'.join( [skipped['page'].url for skipped in results.skipped_pages])) if results.page_successes: # Update the metadata for the pages which were recorded. ps.wpr_archive_info.AddRecordedPages( [page['page'] for page in results.page_successes]) else: os.remove(temp_target_wpr_file_path) return min(255, len(results.page_failures))
def disabled_testCredentialsWhenLoginSucceeds(self): # This test is disabled because it runs against live sites, and needs to be # fixed. crbug.com/179038 results = page_test.PageTestResults() credentials_backend = StubCredentialsBackend(login_return_value=True) did_run = self.runCredentialsTest(credentials_backend, results) assert credentials_backend.did_get_login == True assert credentials_backend.did_get_login_no_longer_needed == True assert did_run
def testHandlingOfCrashedTab(self): ps = page_set.PageSet() page1 = page_module.Page('chrome://crash', ps) ps.pages.append(page1) results = page_test.PageTestResults() class Test(page_test.PageTest): def RunTest(self, *args): pass with page_runner.PageRunner(ps) as runner: options = options_for_unittests.GetCopy() possible_browser = browser_finder.FindBrowser(options) runner.Run(options, possible_browser, Test('RunTest'), results) self.assertEquals(0, len(results.page_successes)) self.assertEquals(1, len(results.page_failures))
def Main(test_dir, page_set_filenames): """Turns a PageTest into a command-line program. Args: test_dir: Path to directory containing PageTests. """ tests = discover.DiscoverClasses(test_dir, os.path.join(test_dir, '..'), page_test.PageTest) # Naively find the test. If we use the browser options parser, we run # the risk of failing to parse if we use a test-specific parameter. test_name = None for arg in sys.argv: if arg in tests: test_name = arg options = browser_options.BrowserOptions() parser = options.CreateParser('%prog [options] <test> <page_set>') page_runner.PageRunner.AddCommandLineOptions(parser) test = None if test_name is not None: if test_name not in tests: sys.stderr.write('No test name %s found' % test_name) sys.exit(1) test = tests[test_name]() test.AddCommandLineOptions(parser) _, args = parser.parse_args() if test is None or len(args) != 2: parser.print_usage() print >> sys.stderr, 'Available tests:\n%s\n' % ',\n'.join( sorted(tests.keys())) print >> sys.stderr, 'Available page_sets:\n%s\n' % ',\n'.join( sorted([os.path.relpath(f) for f in page_set_filenames])) sys.exit(1) ps = page_set.PageSet.FromFile(args[1]) results = page_test.PageTestResults() return RunTestOnPageSet(options, ps, test, results)
def testDiscardFirstResult(self): ps = page_set.PageSet() page = page_module.Page( 'file:///' + os.path.join('..', '..', 'unittest_data', 'blank.html'), ps, base_dir=os.path.dirname(__file__)) ps.pages.append(page) results = page_test.PageTestResults() class Test(page_test.PageTest): @property def discard_first_result(self): return True def RunTest(self, *args): pass with page_runner.PageRunner(ps) as runner: options = options_for_unittests.GetCopy() possible_browser = browser_finder.FindBrowser(options) runner.Run(options, possible_browser, Test('RunTest'), results) self.assertEquals(0, len(results.page_successes)) self.assertEquals(0, len(results.page_failures))
def PrepareResults(self, test): #pylint: disable=W0613 return page_test.PageTestResults()