Esempio n. 1
0
    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)
Esempio n. 2
0
    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)
Esempio n. 3
0
    def runCredentialsTest(
            self,  # pylint: disable=R0201
            credentials_backend,
            results):
        ps = page_set.PageSet()
        page = page_module.Page('http://www.google.com', ps)
        page.credentials = "test"
        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.GetCopy()
                possible_browser = browser_finder.FindBrowser(options)
                runner.Run(options, possible_browser, test, results)

        return did_run[0]
Esempio n. 4
0
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))
Esempio n. 5
0
    def RunTestOnPageSet(self, test, ps, results):
        test.CustomizeBrowserOptions(self._options)
        possible_browser = browser_finder.FindBrowser(self._options)
        if not possible_browser:
            self.PrintParseError(
                'No browser found.\n'
                'Use --browser=list to figure out which are available.')

        with page_runner.PageRunner(ps) as runner:
            runner.Run(self._options, possible_browser, test, results)
Esempio n. 6
0
    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 RunBenchmark(self, benchmark, ps, options=None):
        """Runs a benchmark against a pageset, returning the rows its outputs."""
        if options is None:
            options = options_for_unittests.GetCopy()
        assert options
        temp_parser = options.CreateParser()
        benchmark.AddCommandLineOptions(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)
        possible_browser = browser_finder.FindBrowser(options)

        results = page_benchmark_results.PageBenchmarkResults()
        with page_runner.PageRunner(ps) as runner:
            runner.Run(options, possible_browser, benchmark, results)
        return results
Esempio n. 8
0
def RunTestOnPageSet(options, ps, test, results):
    test.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)

    with page_runner.PageRunner(ps) as runner:
        runner.Run(options, possible_browser, test, results)

    print '%i pages succeed\n' % len(results.page_successes)
    if len(results.page_failures):
        logging.warning(
            'Failed pages: %s', '\n'.join(
                [failure['page'].url for failure in results.page_failures]))

    if len(results.skipped_pages):
        logging.warning(
            'Skipped pages: %s', '\n'.join(
                [skipped['page'].url for skipped in results.skipped_pages]))
    return min(255, len(results.page_failures))
Esempio n. 9
0
    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))
Esempio n. 10
0
def Main(benchmark_dir):
    """Turns a PageBenchmark into a command-line program.

  Args:
    benchmark_dir: Path to directory containing PageBenchmarks.
  """
    benchmarks = discover.DiscoverClasses(benchmark_dir,
                                          os.path.join(benchmark_dir, '..'),
                                          page_benchmark.PageBenchmark)

    # 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>')

    page_runner.PageRunner.AddCommandLineOptions(parser)
    parser.add_option('--output-format',
                      dest='output_format',
                      default='csv',
                      help='Output format. Can be "csv" or "block". '
                      'Defaults to "%default".')
    parser.add_option('-o',
                      '--output',
                      dest='output_file',
                      help='Redirects output to a file. Defaults to stdout.')
    parser.add_option('--output-trace-tag',
                      dest='output_trace_tag',
                      help='Append a tag to the key of each result trace.')

    benchmark = None
    if benchmark_name is not None:
        benchmark = benchmarks[benchmark_name]()
        benchmark.AddCommandLineOptions(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(
            sorted(benchmarks.keys()))
        print >> sys.stderr, 'Available page_sets:\n%s\n' % ',\n'.join(
            sorted([
                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)

    if not options.output_file:
        output_file = sys.stdout
    elif options.output_file == '-':
        output_file = sys.stdout
    else:
        output_file = open(os.path.expanduser(options.output_file), 'w')

    if options.output_format == 'csv':
        results = csv_page_benchmark_results.CsvPageBenchmarkResults(
            csv.writer(output_file),
            benchmark.results_are_the_same_on_every_page)
    elif options.output_format in ('block', 'terminal-block'):
        results = block_page_benchmark_results.BlockPageBenchmarkResults(
            output_file)
    else:
        raise Exception(
            'Invalid --output-format value: "%s". Valid values are '
            '"csv" and "block".' % options.output_format)

    with page_runner.PageRunner(ps) as runner:
        runner.Run(options, possible_browser, benchmark, results)

    output_trace_tag = ''
    if options.output_trace_tag:
        output_trace_tag = options.output_trace_tag
    elif options.browser_executable:
        # When using an exact executable, assume it is a reference build for the
        # purpose of outputting the perf results.
        # TODO(tonyg): Remove this branch once the perfbots use --output-trace-tag.
        output_trace_tag = '_ref'
    results.PrintSummary(output_trace_tag)

    if len(results.page_failures):
        logging.warning(
            'Failed pages: %s', '\n'.join(
                [failure['page'].url for failure in results.page_failures]))

    if len(results.skipped_pages):
        logging.warning(
            'Skipped pages: %s', '\n'.join(
                [skipped['page'].url for skipped in results.skipped_pages]))
    return min(255, len(results.page_failures))