Exemplo n.º 1
0
  def CreateAndNavigateToPageFromUnittestDataDir(
    self, filename, page_attributes):
    self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir())
    page = page_module.Page(
      self._browser.http_server.UrlOf(filename),
      None, # In this test, we don't need a page set.
      attributes=page_attributes)

    self._tab.Navigate(page.url)
    self._tab.WaitForDocumentReadyStateToBeComplete()

    return page
Exemplo n.º 2
0
    def testGetMemoryStatsNoHWM(self):
        if not proc_supporting_platform_backend.resource:
            logging.warning('Test not supported')
            return

        backend = TestBackend()
        backend.SetMockFile(
            '/proc/1/stat',
            open(os.path.join(util.GetUnittestDataDir(), 'stat')).read())
        backend.SetMockFile(
            '/proc/1/status',
            open(os.path.join(util.GetUnittestDataDir(),
                              'status_nohwm')).read())
        result = backend.GetMemoryStats(1)
        self.assertEquals(
            result, {
                'VM': 1025978368,
                'VMPeak': 1025978368,
                'WorkingSetSize': 84000768,
                'WorkingSetSizePeak': 84000768
            })
Exemplo n.º 3
0
 def testArchiveWithMissingStory(self):
     # This test file has a recording for a 'http://www.testurl.com' story only.
     archive_data_file = os.path.join(util.GetUnittestDataDir(),
                                      'archive_files', 'test.json')
     story_set = test_stories.DummyStorySet(
         ['http://www.testurl.com', 'http://www.google.com'],
         archive_data_file=archive_data_file)
     with self.assertRaises(story_runner.ArchiveError):
         story_runner._UpdateAndCheckArchives(story_set.archive_data_file,
                                              story_set.wpr_archive_info,
                                              story_set.stories,
                                              self._mock_story_filter)
Exemplo n.º 4
0
 def testExtensionNotLoaded(self):
     """Querying an extension that was not loaded will return None"""
     extension_path = os.path.join(util.GetUnittestDataDir(),
                                   'simple_extension')
     options = options_for_unittests.GetCopy()
     load_extension = extension_to_load.ExtensionToLoad(
         extension_path, options.browser_type)
     browser_to_create = browser_finder.FindBrowser(options)
     with browser_to_create.Create(options) as b:
         if b.supports_extensions:
             self.assertRaises(KeyError,
                               lambda: b.extensions[load_extension])
Exemplo n.º 5
0
    def Run(self, args):
        possible_browser = browser_finder.FindBrowser(args)

        runner = typ.Runner()
        if self.stream:
            runner.host.stdout = self.stream

        # Telemetry seems to overload the system if we run one test per core,
        # so we scale things back a fair amount. Many of the telemetry tests
        # are long-running, so there's a limit to how much parallelism we
        # can effectively use for now anyway.
        #
        # It should be possible to handle multiple devices if we adjust the
        # browser_finder code properly, but for now we only handle one on ChromeOS.
        if possible_browser.platform.GetOSName() == 'chromeos':
            runner.args.jobs = 1
        elif possible_browser.platform.GetOSName() == 'android':
            runner.args.jobs = len(
                device_finder.GetDevicesMatchingOptions(args))
            print 'Running tests with %d Android device(s).' % runner.args.jobs
        elif possible_browser.platform.GetOSVersionName() == 'xp':
            # For an undiagnosed reason, XP falls over with more parallelism.
            # See crbug.com/388256
            runner.args.jobs = max(int(args.jobs) // 4, 1)
        else:
            runner.args.jobs = max(int(args.jobs) // 2, 1)

        runner.args.metadata = args.metadata
        runner.args.passthrough = args.passthrough
        runner.args.path = args.path
        runner.args.retry_limit = args.retry_limit
        runner.args.test_results_server = args.test_results_server
        runner.args.test_type = args.test_type
        # Always print out test's timing info.
        runner.args.timing = True
        runner.args.top_level_dir = args.top_level_dir
        runner.args.verbose = args.verbosity
        runner.args.write_full_results_to = args.write_full_results_to
        runner.args.write_trace_to = args.write_trace_to

        runner.args.path.append(util.GetUnittestDataDir())

        runner.classifier = GetClassifier(args, possible_browser)
        runner.context = args
        runner.setup_fn = _SetUpProcess
        runner.teardown_fn = _TearDownProcess
        runner.win_multiprocessing = typ.WinMultiprocessing.importable
        try:
            ret, _, _ = runner.run()
        except KeyboardInterrupt:
            print >> sys.stderr, "interrupted, exiting"
            ret = 130
        return ret
    def testGetMemoryStatsBasic(self):
        if not proc_supporting_platform_backend.resource:
            logging.warning('Test not supported')
            return

        backend = TestBackend()
        with open(os.path.join(util.GetUnittestDataDir(), 'stat')) as f:
            backend.SetMockFile('/proc/1/stat', f.read())
        with open(os.path.join(util.GetUnittestDataDir(), 'status')) as f:
            backend.SetMockFile('/proc/1/status', f.read())
        with open(os.path.join(util.GetUnittestDataDir(), 'smaps')) as f:
            backend.SetMockFile('/proc/1/smaps', f.read())
        result = backend.GetMemoryStats(1)
        self.assertEquals(
            result, {
                'PrivateDirty': 5324800,
                'VM': 1025978368,
                'VMPeak': 1050099712,
                'WorkingSetSize': 84000768,
                'WorkingSetSizePeak': 144547840
            })
  def testTrafficSettings(self):
    story_set = story.StorySet()
    slow_page = page_module.Page(
        'file://green_rect.html', story_set, base_dir=util.GetUnittestDataDir(),
        name='slow',
        traffic_setting=traffic_setting_module.REGULAR_2G)
    fast_page = page_module.Page(
        'file://green_rect.html', story_set, base_dir=util.GetUnittestDataDir(),
        name='fast',
        traffic_setting=traffic_setting_module.WIFI)
    story_set.AddStory(slow_page)
    story_set.AddStory(fast_page)

    latencies_by_page_in_ms = {}

    class MeasureLatency(legacy_page_test.LegacyPageTest):
      def __init__(self):
        super(MeasureLatency, self).__init__()
        self._will_navigate_time = None

      def WillNavigateToPage(self, page, tab):
        del page, tab # unused
        self._will_navigate_time = time.time() * 1000

      def ValidateAndMeasurePage(self, page, tab, results):
        del results  # unused
        latencies_by_page_in_ms[page.name] = (
            time.time() * 1000 - self._will_navigate_time)

    test = MeasureLatency()
    options = options_for_unittests.GetCopy()
    options.output_formats = ['none']
    options.suppress_gtest_report = True
    SetUpStoryRunnerArguments(options)
    results = results_options.CreateResults(EmptyMetadataForTest(), options)
    story_runner.Run(test, story_set, options, results)
    # Slow page should be slower than fast page by at least 300 ms (roundtrip
    # time of 2G) - 2 ms (roundtrip time of Wifi)
    self.assertGreater(latencies_by_page_in_ms['slow'],
                       latencies_by_page_in_ms['fast'] + 300 - 2)
Exemplo n.º 8
0
    def testRaiseBrowserGoneExceptionFromRestartBrowserBeforeEachPage(self):
        self.CaptureFormattedException()
        story_set = story.StorySet()
        story_set.AddStory(
            page_module.Page('file://blank.html',
                             story_set,
                             base_dir=util.GetUnittestDataDir()))
        story_set.AddStory(
            page_module.Page('file://blank.html',
                             story_set,
                             base_dir=util.GetUnittestDataDir()))

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

            def RestartBrowserBeforeEachPage(self):
                old_run_count = self.run_count
                self.run_count += 1
                if old_run_count == 0:
                    raise exceptions.BrowserGoneException(None)
                return self._needs_browser_restart_after_each_page

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

        options = options_for_unittests.GetCopy()
        options.output_formats = ['none']
        options.suppress_gtest_report = True
        test = Test()
        SetUpStoryRunnerArguments(options)
        results = results_options.CreateResults(EmptyMetadataForTest(),
                                                options)
        story_runner.Run(test, story_set, options, results)
        self.assertEquals(2, test.run_count)
        self.assertEquals(1, len(GetSuccessfulPageRuns(results)))
        self.assertEquals(1, len(results.failures))
        self.assertFormattedExceptionIsEmpty()
Exemplo n.º 9
0
    def runCredentialsTest(self, credentials_backend):
        story_set = story.StorySet()
        did_run = [False]

        try:

            class TestSharedState(shared_page_state.SharedPageState):
                def ShouldStopBrowserAfterStoryRun(self, _):
                    # Do not close browser for LoginNoLongerNeeded to get called.
                    return False

            with tempfile.NamedTemporaryFile(delete=False) as f:
                page = page_module.Page(
                    'file://blank.html',
                    story_set,
                    base_dir=util.GetUnittestDataDir(),
                    shared_page_state_class=TestSharedState,
                    credentials_path=f.name,
                    name='blank.html')
                page.credentials = "test"
                story_set.AddStory(page)

                f.write(SIMPLE_CREDENTIALS_STRING)

            class TestThatInstallsCredentialsBackend(
                    legacy_page_test.LegacyPageTest):
                def __init__(self, credentials_backend):
                    super(TestThatInstallsCredentialsBackend, self).__init__()
                    self._credentials_backend = credentials_backend

                def DidStartBrowser(self, browser):
                    browser.credentials.AddBackend(self._credentials_backend)

                def ValidateAndMeasurePage(self, *_):
                    did_run[0] = True

            test = TestThatInstallsCredentialsBackend(credentials_backend)
            options = options_for_unittests.GetCopy()
            options.output_formats = ['none']
            options.suppress_gtest_report = True
            SetUpStoryRunnerArguments(options)
            results = results_options.CreateResults(EmptyMetadataForTest(),
                                                    options)
            story_runner.Run(test,
                             story_set,
                             options,
                             results,
                             metadata=EmptyMetadataForTest())
        finally:
            os.remove(f.name)

        return did_run[0]
Exemplo n.º 10
0
    def run(self, test, progress_reporters, repeat_count, args):
        util.AddDirToPythonPath(util.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 testGetOSVersionNameArch(self):
    path = os.path.join(util.GetUnittestDataDir(), 'arch-lsb-release')
    with open(path) as f:
      arch_lsb_release_content = f.read()

    with mock.patch.object(
        linux_platform_backend.LinuxPlatformBackend, 'GetFileContents',
        return_value=arch_lsb_release_content) as mock_method:
      backend = linux_platform_backend.LinuxPlatformBackend()
      # a distribution may not have a codename or a release number. We just
      # check that GetOSVersionName doesn't raise an exception
      backend.GetOSVersionName()
      mock_method.assert_called_once_with('/etc/lsb-release')
Exemplo n.º 12
0
    def testGetOSVersionNameSaucy(self):
        path = os.path.join(util.GetUnittestDataDir(),
                            'ubuntu-saucy-lsb-release')
        with open(path) as f:
            unbuntu_saucy_lsb_release_content = f.read()

        with mock.patch.object(
                linux_platform_backend.LinuxPlatformBackend,
                'GetFileContents',
                return_value=unbuntu_saucy_lsb_release_content) as mock_method:
            backend = linux_platform_backend.LinuxPlatformBackend()
            self.assertEqual(backend.GetOSVersionName(), 'saucy')
            mock_method.assert_called_once_with('/etc/lsb-release')
Exemplo n.º 13
0
    def testWaitAction(self):
        self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir())
        self._tab.Navigate(self._browser.http_server.UrlOf('blank.html'))
        self._tab.WaitForDocumentReadyStateToBeComplete()
        self.assertEquals(
            self._tab.EvaluateJavaScript('document.location.pathname;'),
            '/blank.html')

        i = wait.WaitAction({'condition': 'duration', 'seconds': 1})

        start_time = time.time()
        i.RunAction(None, self._tab, None)
        self.assertTrue(time.time() - start_time >= 1.0)
    def testBrowserRestartsAfterEachPage(self):
        self.CaptureFormattedException()
        story_set = story.StorySet()
        story_set.AddStory(
            page_module.Page('file://blank.html',
                             story_set,
                             base_dir=util.GetUnittestDataDir(),
                             name='foo'))
        story_set.AddStory(
            page_module.Page('file://blank.html',
                             story_set,
                             base_dir=util.GetUnittestDataDir(),
                             name='bar'))

        class Test(legacy_page_test.LegacyPageTest):
            def __init__(self):
                super(Test, self).__init__()
                self.browser_starts = 0
                self.platform_name = None

            def DidStartBrowser(self, browser):
                super(Test, self).DidStartBrowser(browser)
                self.browser_starts += 1
                self.platform_name = browser.platform.GetOSName()

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

        test = Test()
        results = RunStorySet(test, story_set, self.options)

        self.assertEquals(len(story_set), results.num_successful)
        # Browser is started once per story run, except in ChromeOS where a single
        # instance is reused for all stories.
        if test.platform_name == 'chromeos':
            self.assertEquals(1, test.browser_starts)
        else:
            self.assertEquals(len(story_set), test.browser_starts)
        self.assertFormattedExceptionIsEmpty()
  def testSharedPageStateCannotRunOnBrowser(self):
    story_set = story.StorySet()

    class UnrunnableSharedState(shared_page_state.SharedPageState):
      def CanRunOnBrowser(self, browser_info, page):
        del browser_info, page  # unused
        return False

      def ValidateAndMeasurePage(self, _):
        pass

    story_set.AddStory(page_module.Page(
        url='file://blank.html', page_set=story_set,
        base_dir=util.GetUnittestDataDir(),
        shared_page_state_class=UnrunnableSharedState,
        name='blank.html'))

    class Test(legacy_page_test.LegacyPageTest):

      def __init__(self, *args, **kwargs):
        super(Test, self).__init__(*args, **kwargs)
        self.will_navigate_to_page_called = False

      def ValidateAndMeasurePage(self, *args):
        del args  # unused
        raise Exception('Exception should not be thrown')

      def WillNavigateToPage(self, page, tab):
        del page, tab  # unused
        self.will_navigate_to_page_called = True

    test = Test()
    options = options_for_unittests.GetCopy()
    options.output_formats = ['none']
    options.suppress_gtest_report = True
    SetUpStoryRunnerArguments(options)
    results = results_options.CreateResults(EmptyMetadataForTest(), options)
    possible_browser = browser_finder.FindBrowser(options)
    story_runner.RunStorySet(
        test=test,
        story_set=story_set,
        possible_browser=possible_browser,
        expectations=None,
        browser_options=options.browser_options,
        finder_options=options,
        results=results,
    )
    self.assertFalse(test.will_navigate_to_page_called)
    self.assertEquals(1, len(GetSuccessfulPageRuns(results)))
    self.assertEquals(1, len(results.skipped_values))
    self.assertFalse(results.had_failures)
 def testVideoFileFrameGeneratorSkipFrames(self):
     vid = os.path.join(util.GetUnittestDataDir(),
                        'screen_3_frames.mov')
     fg = self.VideoFileFrameGenerator(vid, 2)
     self.assertEqual(fg.CurrentFrameNumber, 1)
     self.assertAlmostEqual(fg.CurrentTimestamp, 33.367, 3)
     self.assertEqual(fg.Dimensions, (432, 320))
     next(fg.Generator)
     try:
         next(fg.Generator)
         stopped = False
     except StopIteration:
         stopped = True
     self.assertTrue(stopped)
Exemplo n.º 17
0
 def testSuccesfulPythonPageSetLoading(self):
   test_pps_dir = os.path.join(util.GetUnittestDataDir(), 'test_page_set.py')
   pps = page_set.PageSet.FromFile(test_pps_dir)
   self.assertEqual('TestPageSet', pps.__class__.__name__)
   self.assertEqual('A pageset for testing purpose', pps.description)
   self.assertEqual('data/test.json', pps.archive_data_file)
   self.assertEqual('data/credential', pps.credentials_path)
   self.assertEqual('desktop', pps.user_agent_type)
   self.assertEqual(test_pps_dir, pps.file_path)
   self.assertEqual(3, len(pps.pages))
   google_page = pps.pages[0]
   self.assertEqual('https://www.google.com', google_page.url)
   self.assertIs(pps, google_page.page_set)
   self.assertTrue(5, google_page.RunGetActionRunner(action_runner=5))
Exemplo n.º 18
0
 def testArchiveWithMissingWprFile(self):
   # This test file claims to have recordings for both
   # 'http://www.testurl.com' and 'http://www.google.com'; but the file with
   # the wpr recording for the later story is actually missing.
   archive_data_file = os.path.join(
       util.GetUnittestDataDir(), 'archive_files',
       'test_missing_wpr_file.json')
   story_set = test_stories.DummyStorySet(
       ['http://www.testurl.com', 'http://www.google.com'],
       archive_data_file=archive_data_file)
   with self.assertRaises(story_runner.ArchiveError):
     story_runner._UpdateAndCheckArchives(
         story_set.archive_data_file, story_set.wpr_archive_info,
         story_set.stories, self._mock_story_filter)
Exemplo n.º 19
0
 def testFindPythonDependenciesWithNestedImport(self):
     moose_module_path = os.path.join(util.GetUnittestDataDir(),
                                      'dependency_test_dir',
                                      'other_animals', 'moose', 'moose')
     moose_object_path = os.path.join(moose_module_path, 'moose_object.py')
     horn_module_path = os.path.join(moose_module_path, 'horn')
     horn_module_init_path = os.path.join(horn_module_path, '__init__.py')
     horn_object_path = os.path.join(horn_module_path, 'horn_object.py')
     self.assertEquals(
         set(p for p in find_dependencies.FindPythonDependencies(
             moose_object_path)), {
                 moose_object_path, horn_module_path, horn_module_init_path,
                 horn_object_path
             })
    def testNeedsBrowserRestartAfterEachPage(self):
        self.CaptureFormattedException()
        story_set = story.StorySet()
        story_set.AddStory(
            page_module.Page('file://blank.html',
                             story_set,
                             base_dir=util.GetUnittestDataDir(),
                             name='foo'))
        story_set.AddStory(
            page_module.Page('file://blank.html',
                             story_set,
                             base_dir=util.GetUnittestDataDir(),
                             name='bar'))

        class Test(legacy_page_test.LegacyPageTest):
            def __init__(self, *args, **kwargs):
                super(Test, self).__init__(*args, **kwargs)
                self.browser_starts = 0

            def DidStartBrowser(self, *args):
                super(Test, self).DidStartBrowser(*args)
                self.browser_starts += 1

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

        options = options_for_unittests.GetCopy()
        options.output_formats = ['none']
        options.suppress_gtest_report = True
        test = Test(needs_browser_restart_after_each_page=True)
        SetUpStoryRunnerArguments(options)
        results = results_options.CreateResults(EmptyMetadataForTest(),
                                                options)
        story_runner.Run(test, story_set, options, results)
        self.assertEquals(2, len(GetSuccessfulPageRuns(results)))
        self.assertEquals(2, test.browser_starts)
        self.assertFormattedExceptionIsEmpty()
    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'
        options.suppress_gtest_report = True
        test = Test()
        SetUpPageRunnerArguments(options)
        results = results_options.CreateResults(EmptyMetadataForTest(),
                                                options)
        page_runner.Run(test, ps, expectations, options, results)
        self.assertEquals(2, test.run_count)
        self.assertEquals(1, len(GetSuccessfulPageRuns(results)))
        self.assertEquals(1, len(results.failures))
    def testRunPageWithStartupUrl(self):
        num_times_browser_closed = [0]

        class TestSharedState(shared_page_state.SharedPageState):
            def _StopBrowser(self):
                if self._browser:
                    num_times_browser_closed[0] += 1
                super(TestSharedState, self)._StopBrowser()

        story_set = story.StorySet()
        page = page_module.Page('file://blank.html',
                                story_set,
                                base_dir=util.GetUnittestDataDir(),
                                startup_url='about:blank',
                                shared_page_state_class=TestSharedState,
                                name='blank.html')
        story_set.AddStory(page)

        class Measurement(legacy_page_test.LegacyPageTest):
            def __init__(self):
                super(Measurement, self).__init__()

            def ValidateAndMeasurePage(self, page, tab, results):
                del page, tab, results  # not used

        options = options_for_unittests.GetCopy()
        options.pageset_repeat = 2
        options.output_formats = ['none']
        options.suppress_gtest_report = True
        if not browser_finder.FindBrowser(options):
            return

        with tempfile_ext.NamedTemporaryDirectory('page_E2E_tests') as tempdir:
            options.output_dir = tempdir
            test = Measurement()
            SetUpStoryRunnerArguments(options)
            results = results_options.CreateResults(EmptyMetadataForTest(),
                                                    options)
            story_runner.Run(test,
                             story_set,
                             options,
                             results,
                             metadata=EmptyMetadataForTest())
            self.assertEquals('about:blank',
                              options.browser_options.startup_url)
            # _StopBrowser should be called 2 times:
            # 1. browser restarts after page 1 run
            # 2. in the TearDownState after all the pages have run.
            self.assertEquals(num_times_browser_closed[0], 2)
    def testRaiseBrowserGoneExceptionFromRestartBrowserBeforeEachPage(self):
        self.SuppressExceptionFormatting()
        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 Test(page_test.PageTest):
            def __init__(self, *args):
                super(Test, self).__init__(*args)
                self.run_count = 0

            def RestartBrowserBeforeEachPage(self):
                old_run_count = self.run_count
                self.run_count += 1
                if old_run_count == 0:
                    raise exceptions.BrowserGoneException(None)
                return self._needs_browser_restart_after_each_page

        options = options_for_unittests.GetCopy()
        options.output_formats = ['none']
        options.suppress_gtest_report = True
        test = Test()
        SetUpUserStoryRunnerArguments(options)
        results = results_options.CreateResults(EmptyMetadataForTest(),
                                                options)
        user_story_runner.Run(test, ps, expectations, options, results)
        self.assertEquals(2, test.run_count)
        self.assertEquals(1, len(GetSuccessfulPageRuns(results)))
        self.assertEquals(1, len(results.failures))
    def testGetOSVersionNameFedora(self):
        path = os.path.join(util.GetUnittestDataDir(), 'fedora-os-release')
        with open(path) as f:
            fedora_os_release_content = f.read()

        with mock.patch.object(os.path,
                               'exists',
                               side_effect=_PathMatches('/etc/os-release')):
            with mock.patch.object(
                    linux_platform_backend.LinuxPlatformBackend,
                    'GetFileContents',
                    return_value=fedora_os_release_content) as mock_method:
                backend = linux_platform_backend.LinuxPlatformBackend()
                self.assertEqual(backend.GetOSVersionName(), 'fedora')
                mock_method.assert_called_once_with('/etc/os-release')
Exemplo n.º 25
0
  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_measurement.PageMeasurement):
      i = 0
      def MeasurePage(self, _, __, results):
        self.i += 1
        results.Add('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.reset_results = None
      options.upload_results = None
      options.results_label = None

      options.repeat_options.page_repeat_iters = 1
      options.repeat_options.pageset_repeat_iters = 2
      results = page_runner.Run(Measurement(), ps, expectations, options)
      results.PrintSummary()
      self.assertEquals(4, len(results.successes))
      self.assertEquals(0, len(results.failures))
      stdout = open(output_file).read()
      self.assertIn('RESULT metric_by_url: blank.html= [1,3] unit', stdout)
      self.assertIn('RESULT metric_by_url: green_rect.html= [2,4] unit', stdout)
      self.assertIn('*RESULT metric: metric= [1,2,3,4] unit', stdout)
    finally:
      results._output_stream.close()  # pylint: disable=W0212
      os.remove(output_file)
Exemplo n.º 26
0
    def testGetDOMStats(self):
        self._browser.SetHTTPServerDirectories(util.GetUnittestDataDir())

        # Due to an issue with CrOS, we create a new tab here rather than
        # using self._tab to get a consistent starting page on all platforms
        tab = self._browser.tabs.New()

        tab.Navigate(
            self._browser.http_server.UrlOf('dom_counter_sample.html'))
        tab.WaitForDocumentReadyStateToBeComplete()

        counts = tab.dom_stats
        self.assertEqual(counts['document_count'], 2)
        self.assertEqual(counts['node_count'], 18)
        self.assertEqual(counts['event_listener_count'], 2)
Exemplo n.º 27
0
    def CreateBrowserWithExtension(self, ext_path):
        extension_path = os.path.join(util.GetUnittestDataDir(), ext_path)
        options = options_for_unittests.GetCopy()
        load_extension = extension_to_load.ExtensionToLoad(
            extension_path, options.browser_type)
        options.extensions_to_load = [load_extension]
        browser_to_create = browser_finder.FindBrowser(options)

        if not browser_to_create:
            # May not find a browser that supports extensions.
            return False
        self._browser = browser_to_create.Create(options)
        self._extension = self._browser.extensions[load_extension]
        self._extension_id = load_extension.extension_id
        self.assertTrue(self._extension)
        return True
        def testVideoFileFrameGeneratorFailure(self):
            vid = os.path.join(util.GetUnittestDataDir(),
                               'screen_3_frames.mov')
            try:
                self.VideoFileFrameGenerator(vid, 4)
                fail = False
            except frame_generator.FrameReadError:
                fail = True
            self.assertTrue(fail)

            try:
                self.VideoFileFrameGenerator('not_a_file', 0)
                fail = False
            except frame_generator.FrameReadError:
                fail = True
            self.assertTrue(fail)
    def _testMaxFailuresOptionIsRespectedAndOverridable(
            self, max_failures=None):
        self.SuppressExceptionFormatting()

        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):  # pylint: disable=W0613
                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()
        for ii in range(5):
            ps.pages.append(
                TestPage('file://blank.html',
                         ps,
                         base_dir=util.GetUnittestDataDir()))

        options = options_for_unittests.GetCopy()
        options.output_formats = ['none']
        options.suppress_gtest_report = True
        expected_max_failures = 2
        if not max_failures is None:
            options.max_failures = max_failures
            expected_max_failures = max_failures
        SetUpUserStoryRunnerArguments(options)
        results = results_options.CreateResults(EmptyMetadataForTest(),
                                                options)
        user_story_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(expected_max_failures + 1, len(results.failures))
        for ii in range(len(ps.pages)):
            if ii <= expected_max_failures:
                self.assertTrue(ps.pages[ii].was_run)
            else:
                self.assertFalse(ps.pages[ii].was_run)
  def setUp(self):
    extension_path = os.path.join(util.GetUnittestDataDir(), 'simple_extension')
    options = options_for_unittests.GetCopy()
    load_extension = extension_to_load.ExtensionToLoad(
        extension_path, options.browser_type)
    options.extensions_to_load = [load_extension]
    browser_to_create = browser_finder.FindBrowser(options)

    self._browser = None
    self._extension = None
    if not browser_to_create:
      # May not find a browser that supports extensions.
      return
    self._browser = browser_to_create.Create()
    self._browser.Start()
    self._extension = self._browser.extensions[load_extension]
    self.assertTrue(self._extension)