def runCredentialsTest(self, # pylint: disable=R0201 credentials_backend, results): page = page_module.Page('http://www.google.com') page.credentials = "test" ps = page_set.PageSet() ps.pages.append(page) did_run = [False] with tempfile.NamedTemporaryFile() as f: f.write(SIMPLE_CREDENTIALS_STRING) f.flush() ps.credentials_path = f.name class TestThatInstallsCredentialsBackend(page_test.PageTest): def __init__(self, credentials_backend): super(TestThatInstallsCredentialsBackend, self).__init__('RunTest') self._credentials_backend = credentials_backend def SetUpBrowser(self, browser): browser.credentials.AddBackend(self._credentials_backend) def RunTest(self, page, tab, results): # pylint: disable=W0613,R0201 did_run[0] = True test = TestThatInstallsCredentialsBackend(credentials_backend) with page_runner.PageRunner(ps) as runner: options = options_for_unittests.Get() possible_browser = browser_finder.FindBrowser(options) runner.Run(options, possible_browser, test, results) return did_run[0]
def testBrowserCreation(self): options = options_for_unittests.Get() browser_to_create = browser_finder.FindBrowser(options) if not browser_to_create: raise Exception('No browser found, cannot continue test.') with browser_to_create.Create() as b: self.assertEquals(1, b.num_tabs) # Different browsers boot up to different things assert b.GetNthTabUrl(0)
def Main(): """Turns a MultiPageBenchmark into a command-line program. If args is not specified, sys.argv[1:] is used. """ # Naively find the benchmark. If we use the browser options parser, we run # the risk of failing to parse if we use a benchmark-specific parameter. benchmark_name = None for arg in sys.argv: if arg in _BENCHMARKS: benchmark_name = arg options = browser_options.BrowserOptions() parser = options.CreateParser('%prog [options] <benchmark> <page_set>') benchmark = None if benchmark_name is not None: benchmark = _BENCHMARKS[benchmark_name]() benchmark.AddOptions(parser) _, args = parser.parse_args() if benchmark is None or len(args) != 2: parser.print_usage() import page_sets # pylint: disable=F0401 print >> sys.stderr, 'Available benchmarks:\n%s\n' % ',\n'.join( _BENCHMARKS.keys()) print >> sys.stderr, 'Available page_sets:\n%s\n' % ',\n'.join( [os.path.relpath(f) for f in page_sets.GetAllPageSetFilenames()]) sys.exit(1) ps = page_set.PageSet.FromFile(args[1]) benchmark.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 = multi_page_benchmark.CsvBenchmarkResults(csv.writer(sys.stdout)) with page_runner.PageRunner(ps) as runner: runner.Run(options, possible_browser, benchmark, results) # When using an exact executable, assume it is a reference build for the # purpose of outputting the perf results. results.PrintSummary(options.browser_executable and '_ref' or '') if len(results.page_failures): logging.warning( 'Failed pages: %s', '\n'.join( [failure['page'].url for failure in results.page_failures])) return min(255, len(results.page_failures))
def setUp(self): self._browser = None self._tab = None options = options_for_unittests.Get() browser_to_create = browser_finder.FindBrowser(options) if not browser_to_create: raise Exception('No browser found, cannot continue test.') try: self._browser = browser_to_create.Create() self._tab = self._browser.ConnectToNthTab(0) except: self.tearDown() raise
def testRealLoginIfPossible(self): credentials_path = os.path.join(os.path.dirname(__file__), '..', '..', 'perf', 'data', 'credentials.json') if not os.path.exists(credentials_path): return options = options_for_unittests.Get() with browser_finder.FindBrowser(options).Create() as b: b.credentials.credentials_path = credentials_path if not b.credentials.CanLogin('google'): return with b.ConnectToNthTab(0) as tab: ret = b.credentials.LoginNeeded(tab, 'google') self.assertTrue(ret)
def testBasicHosting(self): unittest_data_dir = os.path.join(os.path.dirname(__file__), '..', 'unittest_data') options = options_for_unittests.Get() browser_to_create = browser_finder.FindBrowser(options) with browser_to_create.Create() as b: b.SetHTTPServerDirectory(unittest_data_dir) with b.ConnectToNthTab(0) as t: t.page.Navigate(b.http_server.UrlOf('/blank.html')) t.WaitForDocumentReadyStateToBeComplete() x = t.runtime.Evaluate('document.body.innerHTML') x = x.strip() self.assertEquals(x, 'Hello world')
def testNewCloseTab(self): options = options_for_unittests.Get() browser_to_create = browser_finder.FindBrowser(options) with browser_to_create.Create() as b: self.assertEquals(1, b.num_tabs) existing_tab_url = b.GetNthTabUrl(0) b.NewTab() self.assertEquals(2, b.num_tabs) self.assertEquals(b.GetNthTabUrl(0), existing_tab_url) self.assertEquals(b.GetNthTabUrl(1), 'about:blank') b.CloseTab(1) self.assertEquals(1, b.num_tabs) self.assertEquals(b.GetNthTabUrl(0), existing_tab_url) self.assertRaises(AssertionError, b.CloseTab, 0)
def testCommandLineOverriding(self): # This test starts the browser with --enable-benchmarking, which should # create a chrome.Interval namespace. This tests whether the command line is # being set. options = options_for_unittests.Get() flag1 = '--user-agent=chrome_remote_control' options.extra_browser_args.append(flag1) browser_to_create = browser_finder.FindBrowser(options) with browser_to_create.Create() as b: with b.ConnectToNthTab(0) as t: t.page.Navigate('http://www.google.com/') t.WaitForDocumentReadyStateToBeInteractiveOrBetter() self.assertEquals(t.runtime.Evaluate('navigator.userAgent'), 'chrome_remote_control')
def RunBenchmark(self, benchmark, ps): """Runs a benchmark against a pageset, returning the rows its outputs.""" options = options_for_unittests.Get() assert options temp_parser = options.CreateParser() benchmark.AddOptions(temp_parser) defaults = temp_parser.get_default_values() for k, v in defaults.__dict__.items(): if hasattr(options, k): continue setattr(options, k, v) benchmark.CustomizeBrowserOptions(options) self.CustomizeOptionsForTest(options) possible_browser = browser_finder.FindBrowser(options) results = multi_page_benchmark.BenchmarkResults() with page_runner.PageRunner(ps) as runner: runner.Run(options, possible_browser, benchmark, results) return results
def Main(args, start_dir, top_level_dir): """Unit test suite that collects all test cases for chrome_remote_control.""" default_options = browser_options.BrowserOptions() default_options.browser_type = 'any' parser = default_options.CreateParser('run_tests [options] [test names]') parser.add_option('--repeat-count', dest='run_test_repeat_count', type='int', default=1, help='Repeats each a provided number of times.') _, args = parser.parse_args(args) if default_options.verbosity == 0: logging.getLogger().setLevel(logging.ERROR) from chrome_remote_control import browser_finder browser_to_create = browser_finder.FindBrowser(default_options) if browser_to_create == None: logging.error('No browser found of type %s. Cannot run tests.', default_options.browser_type) logging.error('Re-run with --browser=list to see available browser types.') return 1 options_for_unittests.Set(default_options, browser_to_create.browser_type) olddir = os.getcwd() num_errors = 0 try: os.chdir(top_level_dir) for _ in range( default_options.run_test_repeat_count): # pylint: disable=E1101 num_errors += DiscoverAndRunTests(start_dir, args, top_level_dir) finally: os.chdir(olddir) options_for_unittests.Set(None, None) return max(num_errors, 255)