Example #1
0
  def testDiscardFirstResult(self):
    ps = page_set.PageSet()
    expectations = test_expectations.TestExpectations()
    ps.pages.append(page_module.Page(
        'file://blank.html', ps, base_dir=util.GetUnittestDataDir()))
    ps.pages.append(page_module.Page(
        'file://blank.html', ps, base_dir=util.GetUnittestDataDir()))

    class Measurement(page_test.PageTest):
      @property
      def discard_first_result(self):
        return True

      def ValidateAndMeasurePage(self, page, _, results):
        results.AddValue(string.StringValue(page, 'test', 't', page.url))

    options = options_for_unittests.GetCopy()
    options.output_formats = ['none']
    options.suppress_gtest_report = True
    options.reset_results = None
    options.upload_results = None
    options.results_label = None

    options.page_repeat = 1
    options.pageset_repeat = 1
    SetUpPageRunnerArguments(options)
    results = results_options.CreateResults(EmptyMetadataForTest(), options)
    page_runner.Run(Measurement(), ps, expectations, options, results)
    self.assertEquals(0, len(GetSuccessfulPageRuns(results)))
    self.assertEquals(0, len(results.failures))
    self.assertEquals(0, len(results.all_page_specific_values))

    options.page_repeat = 1
    options.pageset_repeat = 2
    SetUpPageRunnerArguments(options)
    results = results_options.CreateResults(EmptyMetadataForTest(), options)
    page_runner.Run(Measurement(), ps, expectations, options, results)
    self.assertEquals(2, len(GetSuccessfulPageRuns(results)))
    self.assertEquals(0, len(results.failures))
    self.assertEquals(2, len(results.all_page_specific_values))

    options.page_repeat = 2
    options.pageset_repeat = 1
    SetUpPageRunnerArguments(options)
    results = results_options.CreateResults(EmptyMetadataForTest(), options)
    page_runner.Run(Measurement(), ps, expectations, options, results)
    self.assertEquals(2, len(GetSuccessfulPageRuns(results)))
    self.assertEquals(0, len(results.failures))
    self.assertEquals(2, len(results.all_page_specific_values))

    options.output_formats = ['html']
    options.suppress_gtest_report = True
    options.page_repeat = 1
    options.pageset_repeat = 1
    SetUpPageRunnerArguments(options)
    results = results_options.CreateResults(EmptyMetadataForTest(), options)
    page_runner.Run(Measurement(), ps, expectations, options, results)
    self.assertEquals(0, len(GetSuccessfulPageRuns(results)))
    self.assertEquals(0, len(results.failures))
    self.assertEquals(0, len(results.all_page_specific_values))
    def testDiscardFirstResult(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        ps.pages.append(
            page_module.Page('file://blank.html',
                             ps,
                             base_dir=util.GetUnittestDataDir()))
        ps.pages.append(
            page_module.Page('file://blank.html',
                             ps,
                             base_dir=util.GetUnittestDataDir()))

        class Measurement(page_measurement.PageMeasurement):
            @property
            def discard_first_result(self):
                return True

            def MeasurePage(self, *args):
                pass

        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        options.reset_results = None
        options.upload_results = None
        options.results_label = None

        options.page_repeat = 1
        options.pageset_repeat = 1
        SetUpPageRunnerArguments(options)
        results = page_runner.Run(Measurement(), ps, expectations, options)
        self.assertEquals(0, len(results.successes))
        self.assertEquals(0, len(results.failures))

        options.page_repeat = 1
        options.pageset_repeat = 2
        SetUpPageRunnerArguments(options)
        results = page_runner.Run(Measurement(), ps, expectations, options)
        self.assertEquals(2, len(results.successes))
        self.assertEquals(0, len(results.failures))

        options.page_repeat = 2
        options.pageset_repeat = 1
        SetUpPageRunnerArguments(options)
        results = page_runner.Run(Measurement(), ps, expectations, options)
        self.assertEquals(2, len(results.successes))
        self.assertEquals(0, len(results.failures))

        options.output_format = 'html'
        options.page_repeat = 1
        options.pageset_repeat = 1
        SetUpPageRunnerArguments(options)
        results = page_runner.Run(Measurement(), ps, expectations, options)
        self.assertEquals(0, len(results.successes))
        self.assertEquals(0, len(results.failures))
    def testDiscardFirstResult(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        ps.pages.append(
            page_module.Page(
                'file:///' +
                os.path.join(util.GetUnittestDataDir(), 'blank.html'),
                ps,
                base_dir=os.path.dirname(__file__)))
        ps.pages.append(
            page_module.Page(
                'file:///' +
                os.path.join(util.GetUnittestDataDir(), 'blank.html'),
                ps,
                base_dir=os.path.dirname(__file__)))

        class Measurement(page_measurement.PageMeasurement):
            @property
            def discard_first_result(self):
                return True

            def MeasurePage(self, *args):
                pass

        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        options.reset_html_results = None

        options.repeat_options.page_repeat_iters = 1
        options.repeat_options.pageset_repeat_iters = 1
        results = page_runner.Run(Measurement(), ps, expectations, options)
        self.assertEquals(0, len(results.successes))
        self.assertEquals(0, len(results.failures))

        options.repeat_options.page_repeat_iters = 1
        options.repeat_options.pageset_repeat_iters = 2
        results = page_runner.Run(Measurement(), ps, expectations, options)
        self.assertEquals(2, len(results.successes))
        self.assertEquals(0, len(results.failures))

        options.repeat_options.page_repeat_iters = 2
        options.repeat_options.pageset_repeat_iters = 1
        results = page_runner.Run(Measurement(), ps, expectations, options)
        self.assertEquals(2, len(results.successes))
        self.assertEquals(0, len(results.failures))

        options.output_format = 'html'
        options.repeat_options.page_repeat_iters = 1
        options.repeat_options.pageset_repeat_iters = 1
        results = page_runner.Run(Measurement(), ps, expectations, options)
        self.assertEquals(0, len(results.successes))
        self.assertEquals(0, len(results.failures))
    def RunMeasurement(self,
                       measurement,
                       ps,
                       expectations=test_expectations.TestExpectations(),
                       options=None):
        """Runs a measurement against a pageset, returning the rows its outputs."""
        if options is None:
            options = options_for_unittests.GetCopy()
        assert options
        temp_parser = options.CreateParser()
        page_runner.AddCommandLineArgs(temp_parser)
        measurement.AddCommandLineArgs(temp_parser)
        measurement.SetArgumentDefaults(temp_parser)
        defaults = temp_parser.get_default_values()
        for k, v in defaults.__dict__.items():
            if hasattr(options, k):
                continue
            setattr(options, k, v)

        measurement.CustomizeBrowserOptions(options)
        options.output_file = None
        options.output_format = 'none'
        options.output_trace_tag = None
        page_runner.ProcessCommandLineArgs(temp_parser, options)
        measurement.ProcessCommandLineArgs(temp_parser, options)
        return page_runner.Run(measurement, ps, expectations, options)
    def testHandlingOfTestThatRaisesWithNonFatalUnknownExceptions(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        ps.pages.append(
            page_module.Page('file://blank.html',
                             ps,
                             base_dir=util.GetUnittestDataDir()))
        ps.pages.append(
            page_module.Page('file://blank.html',
                             ps,
                             base_dir=util.GetUnittestDataDir()))

        class ExpectedException(Exception):
            pass

        class Test(page_test.PageTest):
            def __init__(self, *args):
                super(Test, self).__init__(*args)
                self.run_count = 0

            def ValidatePage(self, *_):
                old_run_count = self.run_count
                self.run_count += 1
                if old_run_count == 0:
                    raise ExpectedException()

        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        test = Test()
        SetUpPageRunnerArguments(options)
        results = page_runner.Run(test, ps, expectations, options)
        self.assertEquals(2, test.run_count)
        self.assertEquals(1, len(results.successes))
        self.assertEquals(1, len(results.failures))
  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
        if self._browser.supports_tab_control:
          self._browser.tabs.New()

      def RunTest(self, page, tab, results): # pylint: disable=W0613,R0201
        if not self._browser.supports_tab_control:
          logging.warning('Browser does not support tab control, skipping test')
          return
        assert len(self._browser.tabs) == 1

    test = TestOneTab('RunTest')
    options = options_for_unittests.GetCopy()
    options.output_format = 'none'
    page_runner.Run(test, ps, options)
  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')
    options = options_for_unittests.GetCopy()
    options.output_format = 'none'
    page_runner.Run(test, ps, options)

    self.assertTrue(hasattr(test, 'hasRun') and test.hasRun)
Example #8
0
  def testRetryOnBrowserCrash(self):
    ps = page_set.PageSet()
    expectations = test_expectations.TestExpectations()
    ps.pages.append(page_module.Page(
        'file://blank.html', ps, base_dir=util.GetUnittestDataDir()))

    class CrashyMeasurement(page_test.PageTest):
      has_crashed = False
      def ValidateAndMeasurePage(self, page, tab, results):
        # This value should be discarded on the first run when the
        # browser crashed.
        results.AddValue(
            string.StringValue(page, 'test', 't', str(self.has_crashed)))
        if not self.has_crashed:
          self.has_crashed = True
          raise exceptions.BrowserGoneException(tab.browser)

    options = options_for_unittests.GetCopy()
    options.output_formats = ['csv']
    options.suppress_gtest_report = True

    SetUpPageRunnerArguments(options)
    results = results_options.CreateResults(EmptyMetadataForTest(), options)
    page_runner.Run(CrashyMeasurement(), ps, expectations, options, results)

    self.assertEquals(1, len(GetSuccessfulPageRuns(results)))
    self.assertEquals(0, len(results.failures))
    self.assertEquals(1, len(results.all_page_specific_values))
    self.assertEquals(
        'True', results.all_page_specific_values[0].GetRepresentativeString())
  def runCredentialsTest(self, # pylint: disable=R0201
                         credentials_backend):
    ps = page_set.PageSet()
    expectations = test_expectations.TestExpectations()
    page = page_module.Page(
        'file://blank.html', ps, base_dir=util.GetUnittestDataDir())
    page.credentials = "test"
    ps.pages.append(page)

    did_run = [False]

    try:
      with tempfile.NamedTemporaryFile(delete=False) as f:
        f.write(SIMPLE_CREDENTIALS_STRING)
        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 DidStartBrowser(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)
      options = options_for_unittests.GetCopy()
      options.output_format = 'none'
      page_runner.Run(test, ps, expectations, options)
    finally:
      os.remove(f.name)

    return did_run[0]
    def testBrowserBeforeLaunch(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        page = page_module.Page('file://blank.html',
                                ps,
                                base_dir=util.GetUnittestDataDir())
        ps.pages.append(page)

        class TestBeforeLaunch(page_test.PageTest):
            def __init__(self):
                super(TestBeforeLaunch, self).__init__()
                self._did_call_will_start = False
                self._did_call_did_start = False

            def WillStartBrowser(self, browser):
                self._did_call_will_start = True
                # TODO(simonjam): Test that the profile is available.

            def DidStartBrowser(self, browser):
                assert self._did_call_will_start
                self._did_call_did_start = True

            def ValidatePage(self, *_):
                assert self._did_call_did_start

        test = TestBeforeLaunch()
        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        SetUpPageRunnerArguments(options)
        page_runner.Run(test, ps, expectations, options)
    def testUserAgent(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        page = page_module.Page('file://blank.html',
                                ps,
                                base_dir=util.GetUnittestDataDir())
        ps.pages.append(page)
        ps.user_agent_type = 'tablet'

        class TestUserAgent(page_test.PageTest):
            def ValidatePage(self, _1, tab, _2):
                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()
        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        SetUpPageRunnerArguments(options)
        page_runner.Run(test, ps, expectations, options)

        self.assertTrue(hasattr(test, 'hasRun') and test.hasRun)
Example #12
0
  def testCleanUpPage(self):
    ps = page_set.PageSet()
    expectations = test_expectations.TestExpectations()
    page = page_module.Page(
        'file://blank.html', ps, base_dir=util.GetUnittestDataDir())
    ps.pages.append(page)

    class Test(page_test.PageTest):
      def __init__(self):
        super(Test, self).__init__()
        self.did_call_clean_up = False

      def ValidatePage(self, *_):
        raise exceptions.IntentionalException

      def CleanUpAfterPage(self, page, tab):
        self.did_call_clean_up = True


    test = Test()
    options = options_for_unittests.GetCopy()
    options.output_formats = ['none']
    options.suppress_gtest_report = True
    SetUpPageRunnerArguments(options)
    results = results_options.CreateResults(EmptyMetadataForTest(), options)
    page_runner.Run(test, ps, expectations, options, results)
    assert test.did_call_clean_up
    def testRetryOnBrowserCrash(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        ps.pages.append(
            page_module.Page('file://blank.html',
                             ps,
                             base_dir=util.GetUnittestDataDir()))

        class CrashyMeasurement(page_measurement.PageMeasurement):
            has_crashed = False

            def MeasurePage(self, *_):
                if not self.has_crashed:
                    self.has_crashed = True
                    raise exceptions.BrowserGoneException()

        options = options_for_unittests.GetCopy()
        options.output_format = 'csv'

        SetUpPageRunnerArguments(options)
        results = page_runner.Run(CrashyMeasurement(), ps, expectations,
                                  options)

        self.assertEquals(1, len(results.successes))
        self.assertEquals(0, len(results.failures))
        self.assertEquals(0, len(results.errors))
    def testRunPageWithStartupUrl(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        expectations = test_expectations.TestExpectations()
        page = page_module.Page('file://blank.html',
                                ps,
                                base_dir=util.GetUnittestDataDir())
        page.startup_url = 'about:blank'
        ps.pages.append(page)

        class Measurement(page_measurement.PageMeasurement):
            def __init__(self):
                super(Measurement, self).__init__()
                self.browser_restarted = False

            def CustomizeBrowserOptionsForSinglePage(self, ps, options):
                self.browser_restarted = True
                super(Measurement,
                      self).CustomizeBrowserOptionsForSinglePage(ps, options)

            def MeasurePage(self, page, tab, results):
                pass

        options = options_for_unittests.GetCopy()
        options.page_repeat = 2
        options.output_format = 'none'
        if not browser_finder.FindBrowser(options):
            return
        test = Measurement()
        SetUpPageRunnerArguments(options)
        page_runner.Run(test, ps, expectations, options)
        self.assertEquals('about:blank', options.browser_options.startup_url)
        self.assertTrue(test.browser_restarted)
    def testOneTab(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        page = page_module.Page('file://blank.html',
                                ps,
                                base_dir=util.GetUnittestDataDir())
        ps.pages.append(page)

        class TestOneTab(page_test.PageTest):
            def __init__(self,
                         test_method_name,
                         action_name_to_run='',
                         needs_browser_restart_after_each_page=False):
                super(TestOneTab,
                      self).__init__(test_method_name, action_name_to_run,
                                     needs_browser_restart_after_each_page)
                self._browser = None

            def DidStartBrowser(self, browser):
                self._browser = browser
                if self._browser.supports_tab_control:
                    self._browser.tabs.New()

            def RunTest(self, page, tab, results):  # pylint: disable=W0613,R0201
                if not self._browser.supports_tab_control:
                    logging.warning(
                        'Browser does not support tab control, skipping test')
                    return
                assert len(self._browser.tabs) == 1

        test = TestOneTab('RunTest')
        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        SetUpPageRunnerArguments(options)
        page_runner.Run(test, ps, expectations, options)
Example #16
0
    def TestUseLiveSitesFlag(self, options, expect_from_archive):
        ps = page_set.PageSet(file_path=util.GetUnittestDataDir(),
                              archive_data_file='data/archive_blank.json')
        ps.pages.append(
            page_module.Page('file://blank.html', ps, base_dir=ps.base_dir))
        expectations = test_expectations.TestExpectations()

        class ArchiveTest(page_measurement.PageMeasurement):
            def __init__(self):
                super(ArchiveTest, self).__init__()
                self.is_page_from_archive = False
                self.archive_path_exist = True

            def WillNavigateToPage(self, page, tab):
                self.archive_path_exist = (page.archive_path and
                                           os.path.isfile(page.archive_path))
                self.is_page_from_archive = (tab.browser._wpr_server
                                             is not None)  # pylint: disable=W0212

            def MeasurePage(self, _, __, results):
                pass

        test = ArchiveTest()
        page_runner.Run(test, ps, expectations, options)
        if expect_from_archive and not test.archive_path_exist:
            logging.warning('archive path did not exist, asserting that page '
                            'is from archive is skipped.')
            return
        self.assertEquals(expect_from_archive, test.is_page_from_archive)
Example #17
0
    def Run(self, args):
        """Run this test with the given options."""
        self.CustomizeBrowserOptions(args)

        test = self.PageTestClass()()
        test.__name__ = self.__class__.__name__

        if hasattr(self, '_disabled_strings'):
            test._disabled_strings = self._disabled_strings
        if hasattr(self, '_enabled_strings'):
            test._enabled_strings = self._enabled_strings

        ps = self.CreatePageSet(args)
        expectations = self.CreateExpectations(ps)

        self._DownloadGeneratedProfileArchive(args)

        results = page_test_results.PageTestResults()
        try:
            results = page_runner.Run(test, ps, expectations, args)
        except page_test.TestNotSupportedOnPlatformFailure as failure:
            logging.warning(str(failure))

        results.PrintSummary()
        return len(results.failures) + len(results.errors)
def GenerateProfiles(profile_creator_class, profile_creator_name, options):
  """Generate a profile"""
  expectations = test_expectations.TestExpectations()
  test = profile_creator_class()

  temp_output_directory = tempfile.mkdtemp()
  options.output_profile_path = temp_output_directory

  results = results_options.CreateResults(
      benchmark.BenchmarkMetadata(test.__class__.__name__), options)
  page_runner.Run(test, test.page_set, expectations, options, results)

  if results.failures:
    logging.warning('Some pages failed.')
    logging.warning('Failed pages:\n%s',
                    '\n'.join(results.pages_that_failed))
    return 1

  # Everything is a-ok, move results to final destination.
  generated_profiles_dir = os.path.abspath(options.output_dir)
  if not os.path.exists(generated_profiles_dir):
    os.makedirs(generated_profiles_dir)
  out_path = os.path.join(generated_profiles_dir, profile_creator_name)
  if os.path.exists(out_path):
    shutil.rmtree(out_path)

  shutil.copytree(temp_output_directory, out_path, ignore=_IsPseudoFile)
  shutil.rmtree(temp_output_directory)
  sys.stderr.write("SUCCESS: Generated profile copied to: '%s'.\n" % out_path)

  return 0
Example #19
0
    def Run(self, options):
        """Run this test with the given options."""
        assert hasattr(self, 'test'), 'This test has no "test" attribute.'
        assert issubclass(self.test,
                          page_test.PageTest), ('"%s" is not a PageTest.' %
                                                self.test.__name__)

        for key, value in self.options.iteritems():
            setattr(options, key, value)

        if hasattr(self, '_disabled_strings'):
            self.test._disabled_strings = self._disabled_strings
        if hasattr(self, '_enabled_strings'):
            self.test._enabled_strings = self._enabled_strings

        options.repeat_options = self._CreateRepeatOptions(options)
        self.CustomizeBrowserOptions(options)

        test = self.test()
        test.__name__ = self.__class__.__name__
        ps = self.CreatePageSet(options)
        expectations = self.CreateExpectations(ps)

        # Ensure the test's default options are set if needed.
        parser = options.CreateParser()
        test.AddCommandLineOptions(parser)
        options.MergeDefaultValues(parser.get_default_values())

        self._DownloadGeneratedProfileArchive(options)

        results = page_runner.Run(test, ps, expectations, options)
        results.PrintSummary()
        return len(results.failures) + len(results.errors)
Example #20
0
    def Run(self, finder_options):
        """Run this test with the given options."""
        self.CustomizeBrowserOptions(finder_options.browser_options)

        pt = self.PageTestClass()()
        pt.__name__ = self.__class__.__name__

        if hasattr(self, '_disabled_strings'):
            pt._disabled_strings = self._disabled_strings
        if hasattr(self, '_enabled_strings'):
            pt._enabled_strings = self._enabled_strings

        ps = self.CreatePageSet(finder_options)
        expectations = self.CreateExpectations(ps)

        self._DownloadGeneratedProfileArchive(finder_options)

        results = results_options.CreateResults(self.GetMetadata(),
                                                finder_options)
        try:
            page_runner.Run(pt, ps, expectations, finder_options, results)
        except page_test.TestNotSupportedOnPlatformFailure as failure:
            logging.warning(str(failure))

        results.PrintSummary()
        return len(results.failures)
    def testCleanUpPage(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        page = page_module.Page('file://blank.html',
                                ps,
                                base_dir=util.GetUnittestDataDir())
        ps.pages.append(page)

        class Test(page_test.PageTest):
            def __init__(self):
                super(Test, self).__init__()
                self.did_call_clean_up = False

            def ValidatePage(self, *_):
                raise Exception('Intentional failure')

            def CleanUpAfterPage(self, page, tab):
                self.did_call_clean_up = True

        test = Test()
        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        SetUpPageRunnerArguments(options)
        page_runner.Run(test, ps, expectations, options)
        assert test.did_call_clean_up
    def testBrowserBeforeLaunch(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        page = page_module.Page('file://blank.html',
                                ps,
                                base_dir=util.GetUnittestDataDir())
        ps.pages.append(page)

        class TestBeforeLaunch(page_test.PageTest):
            def __init__(self, test_method_name, action_name_to_run=''):
                super(TestBeforeLaunch,
                      self).__init__(test_method_name, action_name_to_run,
                                     False)
                self._did_call_will_start = False
                self._did_call_did_start = False

            def WillStartBrowser(self, browser):
                self._did_call_will_start = True
                # TODO(simonjam): Test that the profile is available.

            def DidStartBrowser(self, browser):
                assert self._did_call_will_start
                self._did_call_did_start = True

            def RunTest(self, page, tab, results):  # pylint: disable=W0613,R0201
                assert self._did_call_did_start

        test = TestBeforeLaunch('RunTest')
        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        SetUpPageRunnerArguments(options)
        page_runner.Run(test, ps, expectations, options)
    def testOneTab(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        page = page_module.Page('file://blank.html',
                                ps,
                                base_dir=util.GetUnittestDataDir())
        ps.pages.append(page)

        class TestOneTab(page_test.PageTest):
            def __init__(self):
                super(TestOneTab, self).__init__()
                self._browser = None

            def DidStartBrowser(self, browser):
                self._browser = browser
                if self._browser.supports_tab_control:
                    self._browser.tabs.New()

            def ValidatePage(self, *_):
                if not self._browser.supports_tab_control:
                    logging.warning(
                        'Browser does not support tab control, skipping test')
                    return
                assert len(self._browser.tabs) == 1

        test = TestOneTab()
        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        SetUpPageRunnerArguments(options)
        page_runner.Run(test, ps, expectations, options)
Example #24
0
  def testOneTab(self):
    ps = page_set.PageSet()
    expectations = test_expectations.TestExpectations()
    page = page_module.Page(
        'file://blank.html', ps, base_dir=util.GetUnittestDataDir())
    ps.pages.append(page)

    class TestOneTab(page_test.PageTest):
      def __init__(self):
        super(TestOneTab, self).__init__()
        self._browser = None

      def DidStartBrowser(self, browser):
        self._browser = browser
        self._browser.tabs.New()

      def ValidatePage(self, *_):
        assert len(self._browser.tabs) == 1

    test = TestOneTab()
    options = options_for_unittests.GetCopy()
    options.output_formats = ['none']
    options.suppress_gtest_report = True
    SetUpPageRunnerArguments(options)
    results = results_options.CreateResults(EmptyMetadataForTest(), options)
    page_runner.Run(test, ps, expectations, options, results)
    def testMaxFailuresOptionIsRespected(self):
        class TestPage(page_module.Page):
            def __init__(self, *args, **kwargs):
                super(TestPage, self).__init__(*args, **kwargs)
                self.was_run = False

            def RunNavigateSteps(self, action_runner):
                self.was_run = True
                raise Exception('Test exception')

        class Test(page_test.PageTest):
            def ValidatePage(self, *args):
                pass

        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        page1 = TestPage('file://blank.html',
                         ps,
                         base_dir=util.GetUnittestDataDir())
        ps.pages.append(page1)
        page2 = TestPage('file://blank.html',
                         ps,
                         base_dir=util.GetUnittestDataDir())
        ps.pages.append(page2)
        page3 = TestPage('file://blank.html',
                         ps,
                         base_dir=util.GetUnittestDataDir())
        ps.pages.append(page3)
        page4 = TestPage('file://blank.html',
                         ps,
                         base_dir=util.GetUnittestDataDir())
        ps.pages.append(page4)
        page5 = TestPage('file://blank.html',
                         ps,
                         base_dir=util.GetUnittestDataDir())
        ps.pages.append(page5)

        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        options.suppress_gtest_report = True
        SetUpPageRunnerArguments(options)
        results = results_options.CreateResults(EmptyMetadataForTest(),
                                                options)
        page_runner.Run(Test(max_failures=2), ps, expectations, options,
                        results)
        self.assertEquals(0, len(GetSuccessfulPageRuns(results)))
        # Runs up to max_failures+1 failing tests before stopping, since
        # every tests after max_failures failures have been encountered
        # may all be passing.
        self.assertEquals(3, len(results.failures))
        self.assertTrue(page1.was_run)
        self.assertTrue(page2.was_run)
        self.assertTrue(page3.was_run)
        self.assertFalse(page4.was_run)
        self.assertFalse(page5.was_run)
    def testPagesetRepeat(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        ps.pages.append(
            page_module.Page('file://blank.html',
                             ps,
                             base_dir=util.GetUnittestDataDir()))
        ps.pages.append(
            page_module.Page('file://green_rect.html',
                             ps,
                             base_dir=util.GetUnittestDataDir()))

        class Measurement(page_test.PageTest):
            i = 0

            def ValidateAndMeasurePage(self, page, _, results):
                self.i += 1
                results.AddValue(
                    scalar.ScalarValue(page, 'metric', 'unit', self.i))

        output_file = tempfile.NamedTemporaryFile(delete=False).name
        try:
            options = options_for_unittests.GetCopy()
            options.output_format = 'buildbot'
            options.output_file = output_file
            options.suppress_gtest_report = True
            options.reset_results = None
            options.upload_results = None
            options.results_label = None

            options.page_repeat = 1
            options.pageset_repeat = 2
            SetUpPageRunnerArguments(options)
            results = results_options.CreateResults(EmptyMetadataForTest(),
                                                    options)
            page_runner.Run(Measurement(), ps, expectations, options, results)
            results.PrintSummary()
            self.assertEquals(4, len(GetSuccessfulPageRuns(results)))
            self.assertEquals(0, len(results.failures))
            with open(output_file) as f:
                stdout = f.read()
            self.assertIn('RESULT metric: blank.html= [1,3] unit', stdout)
            self.assertIn('RESULT metric: green_rect.html= [2,4] unit', stdout)
            self.assertIn('*RESULT metric: metric= [1,2,3,4] unit', stdout)
        finally:
            # TODO(chrishenry): This is a HACK!!1 Really, the right way to
            # do this is for page_runner (or output formatter) to close any
            # files it has opened.
            for formatter in results._output_formatters:  # pylint: disable=W0212
                formatter.output_stream.close()
            os.remove(output_file)
  def testHandlingOfCrashedTab(self):
    ps = page_set.PageSet()
    page1 = page_module.Page('chrome://crash', ps)
    ps.pages.append(page1)

    class Test(page_test.PageTest):
      def RunTest(self, *args):
        pass

    options = options_for_unittests.GetCopy()
    options.output_format = 'none'
    results = page_runner.Run(Test('RunTest'), ps, options)
    self.assertEquals(0, len(results.successes))
    self.assertEquals(1, len(results.errors))
Example #28
0
  def Run(self, options):
    """Run this test with the given options."""
    assert hasattr(self, 'test'), 'This test has no "test" attribute.'
    assert issubclass(self.test, page_test.PageTest), (
            '"%s" is not a PageTest.' % self.test.__name__)

    for key, value in self.options.iteritems():
      setattr(options, key, value)

    test = self.test()
    ps = self.CreatePageSet(options)
    results = page_runner.Run(test, ps, options)
    results.PrintSummary()
    return len(results.failures) + len(results.errors)
Example #29
0
def Main(base_dir):
    measurements = discover.DiscoverClasses(base_dir, base_dir,
                                            page_measurement.PageMeasurement)
    options = browser_options.BrowserOptions()
    parser = options.CreateParser('%prog <page_set>')
    page_runner.AddCommandLineOptions(parser)

    recorder = RecordPage(measurements)
    recorder.AddCommandLineOptions(parser)
    recorder.AddOutputOptions(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
    options.no_proxy_server = True
    recorder.CustomizeBrowserOptions(options)
    results = page_runner.Run(recorder, ps, options)

    if results.errors or results.failures:
        logging.warning(
            'Some pages failed. The recording has not been updated for '
            'these pages.')
        logging.warning('Failed pages:\n%s',
                        '\n'.join(zip(*results.errors + results.failures)[0]))

    if results.skipped:
        logging.warning('Some pages were skipped. The recording has not been '
                        'updated for these pages.')
        logging.warning('Skipped pages:\n%s',
                        '\n'.join(zip(*results.skipped)[0]))

    if results.successes:
        # Update the metadata for the pages which were recorded.
        ps.wpr_archive_info.AddRecordedPages(results.successes)
    else:
        os.remove(temp_target_wpr_file_path)

    return min(255, len(results.failures))
    def testHandlingOfCrashedTab(self):
        ps = page_set.PageSet()
        expectations = test_expectations.TestExpectations()
        page1 = page_module.Page('chrome://crash', ps)
        ps.pages.append(page1)

        class Test(page_test.PageTest):
            def ValidatePage(self, *args):
                pass

        options = options_for_unittests.GetCopy()
        options.output_format = 'none'
        SetUpPageRunnerArguments(options)
        results = page_runner.Run(Test(), ps, expectations, options)
        self.assertEquals(0, len(results.successes))
        self.assertEquals(0, len(results.failures))
        self.assertEquals(1, len(results.errors))