| OLD | NEW |
| 1 # Copyright 2012 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import logging | 5 import logging |
| 6 import os | 6 import os |
| 7 import tempfile | 7 import tempfile |
| 8 import unittest | 8 import unittest |
| 9 | 9 |
| 10 from telemetry import benchmark |
| 10 from telemetry import decorators | 11 from telemetry import decorators |
| 11 from telemetry.core import browser_finder | 12 from telemetry.core import browser_finder |
| 12 from telemetry.core import exceptions | 13 from telemetry.core import exceptions |
| 13 from telemetry.core import user_agent | 14 from telemetry.core import user_agent |
| 14 from telemetry.core import util | 15 from telemetry.core import util |
| 15 from telemetry.page import page as page_module | 16 from telemetry.page import page as page_module |
| 16 from telemetry.page import page_measurement | 17 from telemetry.page import page_measurement |
| 17 from telemetry.page import page_set | 18 from telemetry.page import page_set |
| 18 from telemetry.page import page_test | 19 from telemetry.page import page_test |
| 19 from telemetry.page import page_runner | 20 from telemetry.page import page_runner |
| 20 from telemetry.page import test_expectations | 21 from telemetry.page import test_expectations |
| 22 from telemetry.results import results_options |
| 21 from telemetry.unittest import options_for_unittests | 23 from telemetry.unittest import options_for_unittests |
| 22 from telemetry.value import scalar | 24 from telemetry.value import scalar |
| 23 from telemetry.value import string | 25 from telemetry.value import string |
| 24 | 26 |
| 25 | 27 |
| 26 SIMPLE_CREDENTIALS_STRING = """ | 28 SIMPLE_CREDENTIALS_STRING = """ |
| 27 { | 29 { |
| 28 "test": { | 30 "test": { |
| 29 "username": "example", | 31 "username": "example", |
| 30 "password": "asdf" | 32 "password": "asdf" |
| 31 } | 33 } |
| 32 } | 34 } |
| 33 """ | 35 """ |
| 34 | 36 |
| 35 | 37 |
| 36 def SetUpPageRunnerArguments(options): | 38 def SetUpPageRunnerArguments(options): |
| 37 parser = options.CreateParser() | 39 parser = options.CreateParser() |
| 38 page_runner.AddCommandLineArgs(parser) | 40 page_runner.AddCommandLineArgs(parser) |
| 39 options.MergeDefaultValues(parser.get_default_values()) | 41 options.MergeDefaultValues(parser.get_default_values()) |
| 40 page_runner.ProcessCommandLineArgs(parser, options) | 42 page_runner.ProcessCommandLineArgs(parser, options) |
| 41 | 43 |
| 44 class EmptyMetadataForTest(benchmark.BenchmarkMetadata): |
| 45 def __init__(self): |
| 46 super(EmptyMetadataForTest, self).__init__('') |
| 42 | 47 |
| 43 class StubCredentialsBackend(object): | 48 class StubCredentialsBackend(object): |
| 44 def __init__(self, login_return_value): | 49 def __init__(self, login_return_value): |
| 45 self.did_get_login = False | 50 self.did_get_login = False |
| 46 self.did_get_login_no_longer_needed = False | 51 self.did_get_login_no_longer_needed = False |
| 47 self.login_return_value = login_return_value | 52 self.login_return_value = login_return_value |
| 48 | 53 |
| 49 @property | 54 @property |
| 50 def credentials_type(self): | 55 def credentials_type(self): |
| 51 return 'test' | 56 return 'test' |
| (...skipping 20 matching lines...) Expand all Loading... |
| 72 page1 = page_module.Page('chrome://crash', ps) | 77 page1 = page_module.Page('chrome://crash', ps) |
| 73 ps.pages.append(page1) | 78 ps.pages.append(page1) |
| 74 | 79 |
| 75 class Test(page_test.PageTest): | 80 class Test(page_test.PageTest): |
| 76 def ValidatePage(self, *args): | 81 def ValidatePage(self, *args): |
| 77 pass | 82 pass |
| 78 | 83 |
| 79 options = options_for_unittests.GetCopy() | 84 options = options_for_unittests.GetCopy() |
| 80 options.output_format = 'none' | 85 options.output_format = 'none' |
| 81 SetUpPageRunnerArguments(options) | 86 SetUpPageRunnerArguments(options) |
| 82 results = page_runner.Run(Test(), ps, expectations, options) | 87 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 88 page_runner.Run(Test(), ps, expectations, options, results) |
| 83 self.assertEquals(0, len(GetSuccessfulPageRuns(results))) | 89 self.assertEquals(0, len(GetSuccessfulPageRuns(results))) |
| 84 self.assertEquals(1, len(results.failures)) | 90 self.assertEquals(1, len(results.failures)) |
| 85 | 91 |
| 86 def testHandlingOfTestThatRaisesWithNonFatalUnknownExceptions(self): | 92 def testHandlingOfTestThatRaisesWithNonFatalUnknownExceptions(self): |
| 87 ps = page_set.PageSet() | 93 ps = page_set.PageSet() |
| 88 expectations = test_expectations.TestExpectations() | 94 expectations = test_expectations.TestExpectations() |
| 89 ps.pages.append(page_module.Page( | 95 ps.pages.append(page_module.Page( |
| 90 'file://blank.html', ps, base_dir=util.GetUnittestDataDir())) | 96 'file://blank.html', ps, base_dir=util.GetUnittestDataDir())) |
| 91 ps.pages.append(page_module.Page( | 97 ps.pages.append(page_module.Page( |
| 92 'file://blank.html', ps, base_dir=util.GetUnittestDataDir())) | 98 'file://blank.html', ps, base_dir=util.GetUnittestDataDir())) |
| 93 | 99 |
| 94 class ExpectedException(Exception): | 100 class ExpectedException(Exception): |
| 95 pass | 101 pass |
| 96 | 102 |
| 97 class Test(page_test.PageTest): | 103 class Test(page_test.PageTest): |
| 98 def __init__(self, *args): | 104 def __init__(self, *args): |
| 99 super(Test, self).__init__(*args) | 105 super(Test, self).__init__(*args) |
| 100 self.run_count = 0 | 106 self.run_count = 0 |
| 101 def ValidatePage(self, *_): | 107 def ValidatePage(self, *_): |
| 102 old_run_count = self.run_count | 108 old_run_count = self.run_count |
| 103 self.run_count += 1 | 109 self.run_count += 1 |
| 104 if old_run_count == 0: | 110 if old_run_count == 0: |
| 105 raise ExpectedException() | 111 raise ExpectedException() |
| 106 | 112 |
| 107 options = options_for_unittests.GetCopy() | 113 options = options_for_unittests.GetCopy() |
| 108 options.output_format = 'none' | 114 options.output_format = 'none' |
| 109 test = Test() | 115 test = Test() |
| 110 SetUpPageRunnerArguments(options) | 116 SetUpPageRunnerArguments(options) |
| 111 results = page_runner.Run(test, ps, expectations, options) | 117 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 118 page_runner.Run(test, ps, expectations, options, results) |
| 112 self.assertEquals(2, test.run_count) | 119 self.assertEquals(2, test.run_count) |
| 113 self.assertEquals(1, len(GetSuccessfulPageRuns(results))) | 120 self.assertEquals(1, len(GetSuccessfulPageRuns(results))) |
| 114 self.assertEquals(1, len(results.failures)) | 121 self.assertEquals(1, len(results.failures)) |
| 115 | 122 |
| 116 def testHandlingOfCrashedTabWithExpectedFailure(self): | 123 def testHandlingOfCrashedTabWithExpectedFailure(self): |
| 117 ps = page_set.PageSet() | 124 ps = page_set.PageSet() |
| 118 expectations = test_expectations.TestExpectations() | 125 expectations = test_expectations.TestExpectations() |
| 119 expectations.Fail('chrome://crash') | 126 expectations.Fail('chrome://crash') |
| 120 page1 = page_module.Page('chrome://crash', ps) | 127 page1 = page_module.Page('chrome://crash', ps) |
| 121 ps.pages.append(page1) | 128 ps.pages.append(page1) |
| 122 | 129 |
| 123 class Test(page_test.PageTest): | 130 class Test(page_test.PageTest): |
| 124 def ValidatePage(self, *_): | 131 def ValidatePage(self, *_): |
| 125 pass | 132 pass |
| 126 | 133 |
| 127 options = options_for_unittests.GetCopy() | 134 options = options_for_unittests.GetCopy() |
| 128 options.output_format = 'none' | 135 options.output_format = 'none' |
| 129 SetUpPageRunnerArguments(options) | 136 SetUpPageRunnerArguments(options) |
| 130 results = page_runner.Run( | 137 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 131 Test(), ps, expectations, options) | 138 page_runner.Run(Test(), ps, expectations, options, results) |
| 132 self.assertEquals(1, len(GetSuccessfulPageRuns(results))) | 139 self.assertEquals(1, len(GetSuccessfulPageRuns(results))) |
| 133 self.assertEquals(0, len(results.failures)) | 140 self.assertEquals(0, len(results.failures)) |
| 134 | 141 |
| 135 def testRetryOnBrowserCrash(self): | 142 def testRetryOnBrowserCrash(self): |
| 136 ps = page_set.PageSet() | 143 ps = page_set.PageSet() |
| 137 expectations = test_expectations.TestExpectations() | 144 expectations = test_expectations.TestExpectations() |
| 138 ps.pages.append(page_module.Page( | 145 ps.pages.append(page_module.Page( |
| 139 'file://blank.html', ps, base_dir=util.GetUnittestDataDir())) | 146 'file://blank.html', ps, base_dir=util.GetUnittestDataDir())) |
| 140 | 147 |
| 141 class CrashyMeasurement(page_measurement.PageMeasurement): | 148 class CrashyMeasurement(page_measurement.PageMeasurement): |
| 142 has_crashed = False | 149 has_crashed = False |
| 143 def MeasurePage(self, page, tab, results): | 150 def MeasurePage(self, page, tab, results): |
| 144 # This value should be discarded on the first run when the | 151 # This value should be discarded on the first run when the |
| 145 # browser crashed. | 152 # browser crashed. |
| 146 results.AddValue( | 153 results.AddValue( |
| 147 string.StringValue(page, 'test', 't', str(self.has_crashed))) | 154 string.StringValue(page, 'test', 't', str(self.has_crashed))) |
| 148 if not self.has_crashed: | 155 if not self.has_crashed: |
| 149 self.has_crashed = True | 156 self.has_crashed = True |
| 150 raise exceptions.BrowserGoneException(tab.browser) | 157 raise exceptions.BrowserGoneException(tab.browser) |
| 151 | 158 |
| 152 options = options_for_unittests.GetCopy() | 159 options = options_for_unittests.GetCopy() |
| 153 options.output_format = 'csv' | 160 options.output_format = 'csv' |
| 154 | 161 |
| 155 SetUpPageRunnerArguments(options) | 162 SetUpPageRunnerArguments(options) |
| 156 results = page_runner.Run(CrashyMeasurement(), ps, expectations, options) | 163 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 164 page_runner.Run(CrashyMeasurement(), ps, expectations, options, results) |
| 157 | 165 |
| 158 self.assertEquals(1, len(GetSuccessfulPageRuns(results))) | 166 self.assertEquals(1, len(GetSuccessfulPageRuns(results))) |
| 159 self.assertEquals(0, len(results.failures)) | 167 self.assertEquals(0, len(results.failures)) |
| 160 self.assertEquals(1, len(results.all_page_specific_values)) | 168 self.assertEquals(1, len(results.all_page_specific_values)) |
| 161 self.assertEquals( | 169 self.assertEquals( |
| 162 'True', results.all_page_specific_values[0].GetRepresentativeString()) | 170 'True', results.all_page_specific_values[0].GetRepresentativeString()) |
| 163 | 171 |
| 164 @decorators.Disabled('xp') # Flaky, http://crbug.com/390079. | 172 @decorators.Disabled('xp') # Flaky, http://crbug.com/390079. |
| 165 def testDiscardFirstResult(self): | 173 def testDiscardFirstResult(self): |
| 166 ps = page_set.PageSet() | 174 ps = page_set.PageSet() |
| (...skipping 13 matching lines...) Expand all Loading... |
| 180 | 188 |
| 181 options = options_for_unittests.GetCopy() | 189 options = options_for_unittests.GetCopy() |
| 182 options.output_format = 'none' | 190 options.output_format = 'none' |
| 183 options.reset_results = None | 191 options.reset_results = None |
| 184 options.upload_results = None | 192 options.upload_results = None |
| 185 options.results_label = None | 193 options.results_label = None |
| 186 | 194 |
| 187 options.page_repeat = 1 | 195 options.page_repeat = 1 |
| 188 options.pageset_repeat = 1 | 196 options.pageset_repeat = 1 |
| 189 SetUpPageRunnerArguments(options) | 197 SetUpPageRunnerArguments(options) |
| 190 results = page_runner.Run(Measurement(), ps, expectations, options) | 198 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 199 page_runner.Run(Measurement(), ps, expectations, options, results) |
| 191 self.assertEquals(0, len(GetSuccessfulPageRuns(results))) | 200 self.assertEquals(0, len(GetSuccessfulPageRuns(results))) |
| 192 self.assertEquals(0, len(results.failures)) | 201 self.assertEquals(0, len(results.failures)) |
| 193 self.assertEquals(0, len(results.all_page_specific_values)) | 202 self.assertEquals(0, len(results.all_page_specific_values)) |
| 194 | 203 |
| 195 options.page_repeat = 1 | 204 options.page_repeat = 1 |
| 196 options.pageset_repeat = 2 | 205 options.pageset_repeat = 2 |
| 197 SetUpPageRunnerArguments(options) | 206 SetUpPageRunnerArguments(options) |
| 198 results = page_runner.Run(Measurement(), ps, expectations, options) | 207 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 208 page_runner.Run(Measurement(), ps, expectations, options, results) |
| 199 self.assertEquals(2, len(GetSuccessfulPageRuns(results))) | 209 self.assertEquals(2, len(GetSuccessfulPageRuns(results))) |
| 200 self.assertEquals(0, len(results.failures)) | 210 self.assertEquals(0, len(results.failures)) |
| 201 self.assertEquals(2, len(results.all_page_specific_values)) | 211 self.assertEquals(2, len(results.all_page_specific_values)) |
| 202 | 212 |
| 203 options.page_repeat = 2 | 213 options.page_repeat = 2 |
| 204 options.pageset_repeat = 1 | 214 options.pageset_repeat = 1 |
| 205 SetUpPageRunnerArguments(options) | 215 SetUpPageRunnerArguments(options) |
| 206 results = page_runner.Run(Measurement(), ps, expectations, options) | 216 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 217 page_runner.Run(Measurement(), ps, expectations, options, results) |
| 207 self.assertEquals(2, len(GetSuccessfulPageRuns(results))) | 218 self.assertEquals(2, len(GetSuccessfulPageRuns(results))) |
| 208 self.assertEquals(0, len(results.failures)) | 219 self.assertEquals(0, len(results.failures)) |
| 209 self.assertEquals(2, len(results.all_page_specific_values)) | 220 self.assertEquals(2, len(results.all_page_specific_values)) |
| 210 | 221 |
| 211 options.output_format = 'html' | 222 options.output_format = 'html' |
| 212 options.page_repeat = 1 | 223 options.page_repeat = 1 |
| 213 options.pageset_repeat = 1 | 224 options.pageset_repeat = 1 |
| 214 SetUpPageRunnerArguments(options) | 225 SetUpPageRunnerArguments(options) |
| 215 results = page_runner.Run(Measurement(), ps, expectations, options) | 226 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 227 page_runner.Run(Measurement(), ps, expectations, options, results) |
| 216 self.assertEquals(0, len(GetSuccessfulPageRuns(results))) | 228 self.assertEquals(0, len(GetSuccessfulPageRuns(results))) |
| 217 self.assertEquals(0, len(results.failures)) | 229 self.assertEquals(0, len(results.failures)) |
| 218 self.assertEquals(0, len(results.all_page_specific_values)) | 230 self.assertEquals(0, len(results.all_page_specific_values)) |
| 219 | 231 |
| 220 @decorators.Disabled('win') | 232 @decorators.Disabled('win') |
| 221 def testPagesetRepeat(self): | 233 def testPagesetRepeat(self): |
| 222 ps = page_set.PageSet() | 234 ps = page_set.PageSet() |
| 223 expectations = test_expectations.TestExpectations() | 235 expectations = test_expectations.TestExpectations() |
| 224 ps.pages.append(page_module.Page( | 236 ps.pages.append(page_module.Page( |
| 225 'file://blank.html', ps, base_dir=util.GetUnittestDataDir())) | 237 'file://blank.html', ps, base_dir=util.GetUnittestDataDir())) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 238 options = options_for_unittests.GetCopy() | 250 options = options_for_unittests.GetCopy() |
| 239 options.output_format = 'buildbot' | 251 options.output_format = 'buildbot' |
| 240 options.output_file = output_file | 252 options.output_file = output_file |
| 241 options.reset_results = None | 253 options.reset_results = None |
| 242 options.upload_results = None | 254 options.upload_results = None |
| 243 options.results_label = None | 255 options.results_label = None |
| 244 | 256 |
| 245 options.page_repeat = 1 | 257 options.page_repeat = 1 |
| 246 options.pageset_repeat = 2 | 258 options.pageset_repeat = 2 |
| 247 SetUpPageRunnerArguments(options) | 259 SetUpPageRunnerArguments(options) |
| 248 results = page_runner.Run(Measurement(), ps, expectations, options) | 260 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 261 page_runner.Run(Measurement(), ps, expectations, options, results) |
| 249 results.PrintSummary() | 262 results.PrintSummary() |
| 250 self.assertEquals(4, len(GetSuccessfulPageRuns(results))) | 263 self.assertEquals(4, len(GetSuccessfulPageRuns(results))) |
| 251 self.assertEquals(0, len(results.failures)) | 264 self.assertEquals(0, len(results.failures)) |
| 252 with open(output_file) as f: | 265 with open(output_file) as f: |
| 253 stdout = f.read() | 266 stdout = f.read() |
| 254 self.assertIn('RESULT metric: blank.html= [1,3] unit', stdout) | 267 self.assertIn('RESULT metric: blank.html= [1,3] unit', stdout) |
| 255 self.assertIn('RESULT metric: green_rect.html= [2,4] unit', stdout) | 268 self.assertIn('RESULT metric: green_rect.html= [2,4] unit', stdout) |
| 256 self.assertIn('*RESULT metric: metric= [1,2,3,4] unit', stdout) | 269 self.assertIn('*RESULT metric: metric= [1,2,3,4] unit', stdout) |
| 257 finally: | 270 finally: |
| 258 # TODO(chrishenry): This is a HACK!!1 Really, the right way to | 271 # TODO(chrishenry): This is a HACK!!1 Really, the right way to |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 def DidStartBrowser(self, browser): | 312 def DidStartBrowser(self, browser): |
| 300 browser.credentials.AddBackend(self._credentials_backend) | 313 browser.credentials.AddBackend(self._credentials_backend) |
| 301 | 314 |
| 302 def ValidatePage(self, *_): | 315 def ValidatePage(self, *_): |
| 303 did_run[0] = True | 316 did_run[0] = True |
| 304 | 317 |
| 305 test = TestThatInstallsCredentialsBackend(credentials_backend) | 318 test = TestThatInstallsCredentialsBackend(credentials_backend) |
| 306 options = options_for_unittests.GetCopy() | 319 options = options_for_unittests.GetCopy() |
| 307 options.output_format = 'none' | 320 options.output_format = 'none' |
| 308 SetUpPageRunnerArguments(options) | 321 SetUpPageRunnerArguments(options) |
| 309 page_runner.Run(test, ps, expectations, options) | 322 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 323 page_runner.Run(test, ps, expectations, options, results) |
| 310 finally: | 324 finally: |
| 311 os.remove(f.name) | 325 os.remove(f.name) |
| 312 | 326 |
| 313 return did_run[0] | 327 return did_run[0] |
| 314 | 328 |
| 315 def testUserAgent(self): | 329 def testUserAgent(self): |
| 316 ps = page_set.PageSet() | 330 ps = page_set.PageSet() |
| 317 expectations = test_expectations.TestExpectations() | 331 expectations = test_expectations.TestExpectations() |
| 318 page = page_module.Page( | 332 page = page_module.Page( |
| 319 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) | 333 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) |
| 320 ps.pages.append(page) | 334 ps.pages.append(page) |
| 321 ps.user_agent_type = 'tablet' | 335 ps.user_agent_type = 'tablet' |
| 322 | 336 |
| 323 class TestUserAgent(page_test.PageTest): | 337 class TestUserAgent(page_test.PageTest): |
| 324 def ValidatePage(self, _1, tab, _2): | 338 def ValidatePage(self, _1, tab, _2): |
| 325 actual_user_agent = tab.EvaluateJavaScript('window.navigator.userAgent') | 339 actual_user_agent = tab.EvaluateJavaScript('window.navigator.userAgent') |
| 326 expected_user_agent = user_agent.UA_TYPE_MAPPING['tablet'] | 340 expected_user_agent = user_agent.UA_TYPE_MAPPING['tablet'] |
| 327 assert actual_user_agent.strip() == expected_user_agent | 341 assert actual_user_agent.strip() == expected_user_agent |
| 328 | 342 |
| 329 # This is so we can check later that the test actually made it into this | 343 # This is so we can check later that the test actually made it into this |
| 330 # function. Previously it was timing out before even getting here, which | 344 # function. Previously it was timing out before even getting here, which |
| 331 # should fail, but since it skipped all the asserts, it slipped by. | 345 # should fail, but since it skipped all the asserts, it slipped by. |
| 332 self.hasRun = True # pylint: disable=W0201 | 346 self.hasRun = True # pylint: disable=W0201 |
| 333 | 347 |
| 334 test = TestUserAgent() | 348 test = TestUserAgent() |
| 335 options = options_for_unittests.GetCopy() | 349 options = options_for_unittests.GetCopy() |
| 336 options.output_format = 'none' | 350 options.output_format = 'none' |
| 337 SetUpPageRunnerArguments(options) | 351 SetUpPageRunnerArguments(options) |
| 338 page_runner.Run(test, ps, expectations, options) | 352 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 353 page_runner.Run(test, ps, expectations, options, results) |
| 339 | 354 |
| 340 self.assertTrue(hasattr(test, 'hasRun') and test.hasRun) | 355 self.assertTrue(hasattr(test, 'hasRun') and test.hasRun) |
| 341 | 356 |
| 342 # Ensure that page_runner forces exactly 1 tab before running a page. | 357 # Ensure that page_runner forces exactly 1 tab before running a page. |
| 343 @decorators.Enabled('has tabs') | 358 @decorators.Enabled('has tabs') |
| 344 def testOneTab(self): | 359 def testOneTab(self): |
| 345 ps = page_set.PageSet() | 360 ps = page_set.PageSet() |
| 346 expectations = test_expectations.TestExpectations() | 361 expectations = test_expectations.TestExpectations() |
| 347 page = page_module.Page( | 362 page = page_module.Page( |
| 348 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) | 363 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) |
| 349 ps.pages.append(page) | 364 ps.pages.append(page) |
| 350 | 365 |
| 351 class TestOneTab(page_test.PageTest): | 366 class TestOneTab(page_test.PageTest): |
| 352 def __init__(self): | 367 def __init__(self): |
| 353 super(TestOneTab, self).__init__() | 368 super(TestOneTab, self).__init__() |
| 354 self._browser = None | 369 self._browser = None |
| 355 | 370 |
| 356 def DidStartBrowser(self, browser): | 371 def DidStartBrowser(self, browser): |
| 357 self._browser = browser | 372 self._browser = browser |
| 358 self._browser.tabs.New() | 373 self._browser.tabs.New() |
| 359 | 374 |
| 360 def ValidatePage(self, *_): | 375 def ValidatePage(self, *_): |
| 361 assert len(self._browser.tabs) == 1 | 376 assert len(self._browser.tabs) == 1 |
| 362 | 377 |
| 363 test = TestOneTab() | 378 test = TestOneTab() |
| 364 options = options_for_unittests.GetCopy() | 379 options = options_for_unittests.GetCopy() |
| 365 options.output_format = 'none' | 380 options.output_format = 'none' |
| 366 SetUpPageRunnerArguments(options) | 381 SetUpPageRunnerArguments(options) |
| 367 page_runner.Run(test, ps, expectations, options) | 382 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 383 page_runner.Run(test, ps, expectations, options, results) |
| 368 | 384 |
| 369 # Ensure that page_runner allows the test to customize the browser before it | 385 # Ensure that page_runner allows the test to customize the browser before it |
| 370 # launches. | 386 # launches. |
| 371 def testBrowserBeforeLaunch(self): | 387 def testBrowserBeforeLaunch(self): |
| 372 ps = page_set.PageSet() | 388 ps = page_set.PageSet() |
| 373 expectations = test_expectations.TestExpectations() | 389 expectations = test_expectations.TestExpectations() |
| 374 page = page_module.Page( | 390 page = page_module.Page( |
| 375 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) | 391 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) |
| 376 ps.pages.append(page) | 392 ps.pages.append(page) |
| 377 | 393 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 389 assert self._did_call_will_start | 405 assert self._did_call_will_start |
| 390 self._did_call_did_start = True | 406 self._did_call_did_start = True |
| 391 | 407 |
| 392 def ValidatePage(self, *_): | 408 def ValidatePage(self, *_): |
| 393 assert self._did_call_did_start | 409 assert self._did_call_did_start |
| 394 | 410 |
| 395 test = TestBeforeLaunch() | 411 test = TestBeforeLaunch() |
| 396 options = options_for_unittests.GetCopy() | 412 options = options_for_unittests.GetCopy() |
| 397 options.output_format = 'none' | 413 options.output_format = 'none' |
| 398 SetUpPageRunnerArguments(options) | 414 SetUpPageRunnerArguments(options) |
| 399 page_runner.Run(test, ps, expectations, options) | 415 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 416 page_runner.Run(test, ps, expectations, options, results) |
| 400 | 417 |
| 401 def testRunPageWithStartupUrl(self): | 418 def testRunPageWithStartupUrl(self): |
| 402 ps = page_set.PageSet() | 419 ps = page_set.PageSet() |
| 403 expectations = test_expectations.TestExpectations() | 420 expectations = test_expectations.TestExpectations() |
| 404 expectations = test_expectations.TestExpectations() | 421 expectations = test_expectations.TestExpectations() |
| 405 page = page_module.Page( | 422 page = page_module.Page( |
| 406 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) | 423 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) |
| 407 page.startup_url = 'about:blank' | 424 page.startup_url = 'about:blank' |
| 408 ps.pages.append(page) | 425 ps.pages.append(page) |
| 409 | 426 |
| 410 class Measurement(page_measurement.PageMeasurement): | 427 class Measurement(page_measurement.PageMeasurement): |
| 411 def __init__(self): | 428 def __init__(self): |
| 412 super(Measurement, self).__init__() | 429 super(Measurement, self).__init__() |
| 413 self.browser_restarted = False | 430 self.browser_restarted = False |
| 414 | 431 |
| 415 def CustomizeBrowserOptionsForSinglePage(self, ps, options): | 432 def CustomizeBrowserOptionsForSinglePage(self, ps, options): |
| 416 self.browser_restarted = True | 433 self.browser_restarted = True |
| 417 super(Measurement, self).CustomizeBrowserOptionsForSinglePage(ps, | 434 super(Measurement, self).CustomizeBrowserOptionsForSinglePage(ps, |
| 418 options) | 435 options) |
| 419 def MeasurePage(self, page, tab, results): | 436 def MeasurePage(self, page, tab, results): |
| 420 pass | 437 pass |
| 421 | 438 |
| 422 options = options_for_unittests.GetCopy() | 439 options = options_for_unittests.GetCopy() |
| 423 options.page_repeat = 2 | 440 options.page_repeat = 2 |
| 424 options.output_format = 'none' | 441 options.output_format = 'none' |
| 425 if not browser_finder.FindBrowser(options): | 442 if not browser_finder.FindBrowser(options): |
| 426 return | 443 return |
| 427 test = Measurement() | 444 test = Measurement() |
| 428 SetUpPageRunnerArguments(options) | 445 SetUpPageRunnerArguments(options) |
| 429 page_runner.Run(test, ps, expectations, options) | 446 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 447 page_runner.Run(test, ps, expectations, options, results) |
| 430 self.assertEquals('about:blank', options.browser_options.startup_url) | 448 self.assertEquals('about:blank', options.browser_options.startup_url) |
| 431 self.assertTrue(test.browser_restarted) | 449 self.assertTrue(test.browser_restarted) |
| 432 | 450 |
| 433 # Ensure that page_runner calls cleanUp when a page run fails. | 451 # Ensure that page_runner calls cleanUp when a page run fails. |
| 434 def testCleanUpPage(self): | 452 def testCleanUpPage(self): |
| 435 ps = page_set.PageSet() | 453 ps = page_set.PageSet() |
| 436 expectations = test_expectations.TestExpectations() | 454 expectations = test_expectations.TestExpectations() |
| 437 page = page_module.Page( | 455 page = page_module.Page( |
| 438 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) | 456 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) |
| 439 ps.pages.append(page) | 457 ps.pages.append(page) |
| 440 | 458 |
| 441 class Test(page_test.PageTest): | 459 class Test(page_test.PageTest): |
| 442 def __init__(self): | 460 def __init__(self): |
| 443 super(Test, self).__init__() | 461 super(Test, self).__init__() |
| 444 self.did_call_clean_up = False | 462 self.did_call_clean_up = False |
| 445 | 463 |
| 446 def ValidatePage(self, *_): | 464 def ValidatePage(self, *_): |
| 447 raise exceptions.IntentionalException | 465 raise exceptions.IntentionalException |
| 448 | 466 |
| 449 def CleanUpAfterPage(self, page, tab): | 467 def CleanUpAfterPage(self, page, tab): |
| 450 self.did_call_clean_up = True | 468 self.did_call_clean_up = True |
| 451 | 469 |
| 452 | 470 |
| 453 test = Test() | 471 test = Test() |
| 454 options = options_for_unittests.GetCopy() | 472 options = options_for_unittests.GetCopy() |
| 455 options.output_format = 'none' | 473 options.output_format = 'none' |
| 456 SetUpPageRunnerArguments(options) | 474 SetUpPageRunnerArguments(options) |
| 457 page_runner.Run(test, ps, expectations, options) | 475 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 476 page_runner.Run(test, ps, expectations, options, results) |
| 458 assert test.did_call_clean_up | 477 assert test.did_call_clean_up |
| 459 | 478 |
| 460 # Ensure skipping the test if page cannot be run on the browser | 479 # Ensure skipping the test if page cannot be run on the browser |
| 461 def testPageCannotRunOnBrowser(self): | 480 def testPageCannotRunOnBrowser(self): |
| 462 ps = page_set.PageSet() | 481 ps = page_set.PageSet() |
| 463 expectations = test_expectations.TestExpectations() | 482 expectations = test_expectations.TestExpectations() |
| 464 | 483 |
| 465 class PageThatCannotRunOnBrowser(page_module.Page): | 484 class PageThatCannotRunOnBrowser(page_module.Page): |
| 466 | 485 |
| 467 def __init__(self): | 486 def __init__(self): |
| (...skipping 15 matching lines...) Expand all Loading... |
| 483 def ValidatePage(self, *args): | 502 def ValidatePage(self, *args): |
| 484 pass | 503 pass |
| 485 | 504 |
| 486 def WillNavigateToPage(self, _1, _2): | 505 def WillNavigateToPage(self, _1, _2): |
| 487 self.will_navigate_to_page_called = True | 506 self.will_navigate_to_page_called = True |
| 488 | 507 |
| 489 test = Test() | 508 test = Test() |
| 490 options = options_for_unittests.GetCopy() | 509 options = options_for_unittests.GetCopy() |
| 491 options.output_format = 'none' | 510 options.output_format = 'none' |
| 492 SetUpPageRunnerArguments(options) | 511 SetUpPageRunnerArguments(options) |
| 493 results = page_runner.Run(test, ps, expectations, options) | 512 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 513 page_runner.Run(test, ps, expectations, options, results) |
| 494 self.assertFalse(test.will_navigate_to_page_called) | 514 self.assertFalse(test.will_navigate_to_page_called) |
| 495 self.assertEquals(0, len(GetSuccessfulPageRuns(results))) | 515 self.assertEquals(0, len(GetSuccessfulPageRuns(results))) |
| 496 self.assertEquals(0, len(results.failures)) | 516 self.assertEquals(0, len(results.failures)) |
| 497 | 517 |
| 498 def TestUseLiveSitesFlag(self, options, expect_from_archive): | 518 def TestUseLiveSitesFlag(self, options, expect_from_archive): |
| 499 ps = page_set.PageSet( | 519 ps = page_set.PageSet( |
| 500 file_path=util.GetUnittestDataDir(), | 520 file_path=util.GetUnittestDataDir(), |
| 501 archive_data_file='data/archive_blank.json') | 521 archive_data_file='data/archive_blank.json') |
| 502 ps.pages.append(page_module.Page( | 522 ps.pages.append(page_module.Page( |
| 503 'file://blank.html', ps, base_dir=ps.base_dir)) | 523 'file://blank.html', ps, base_dir=ps.base_dir)) |
| 504 expectations = test_expectations.TestExpectations() | 524 expectations = test_expectations.TestExpectations() |
| 505 | 525 |
| 506 class ArchiveTest(page_measurement.PageMeasurement): | 526 class ArchiveTest(page_measurement.PageMeasurement): |
| 507 def __init__(self): | 527 def __init__(self): |
| 508 super(ArchiveTest, self).__init__() | 528 super(ArchiveTest, self).__init__() |
| 509 self.is_page_from_archive = False | 529 self.is_page_from_archive = False |
| 510 self.archive_path_exist = True | 530 self.archive_path_exist = True |
| 511 | 531 |
| 512 def WillNavigateToPage(self, page, tab): | 532 def WillNavigateToPage(self, page, tab): |
| 513 self.archive_path_exist = (page.archive_path | 533 self.archive_path_exist = (page.archive_path |
| 514 and os.path.isfile(page.archive_path)) | 534 and os.path.isfile(page.archive_path)) |
| 515 self.is_page_from_archive = ( | 535 self.is_page_from_archive = ( |
| 516 tab.browser._wpr_server is not None) # pylint: disable=W0212 | 536 tab.browser._wpr_server is not None) # pylint: disable=W0212 |
| 517 | 537 |
| 518 def MeasurePage(self, _, __, results): | 538 def MeasurePage(self, _, __, results): |
| 519 pass | 539 pass |
| 520 | 540 |
| 521 test = ArchiveTest() | 541 test = ArchiveTest() |
| 522 page_runner.Run(test, ps, expectations, options) | 542 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 543 page_runner.Run(test, ps, expectations, options, results) |
| 523 if expect_from_archive and not test.archive_path_exist: | 544 if expect_from_archive and not test.archive_path_exist: |
| 524 logging.warning('archive path did not exist, asserting that page ' | 545 logging.warning('archive path did not exist, asserting that page ' |
| 525 'is from archive is skipped.') | 546 'is from archive is skipped.') |
| 526 return | 547 return |
| 527 self.assertEquals(expect_from_archive, test.is_page_from_archive) | 548 self.assertEquals(expect_from_archive, test.is_page_from_archive) |
| 528 | 549 |
| 529 def testUseLiveSitesFlagSet(self): | 550 def testUseLiveSitesFlagSet(self): |
| 530 options = options_for_unittests.GetCopy() | 551 options = options_for_unittests.GetCopy() |
| 531 options.output_format = 'none' | 552 options.output_format = 'none' |
| 532 options.use_live_sites = True | 553 options.use_live_sites = True |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 page4 = TestPage( | 588 page4 = TestPage( |
| 568 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) | 589 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) |
| 569 ps.pages.append(page4) | 590 ps.pages.append(page4) |
| 570 page5 = TestPage( | 591 page5 = TestPage( |
| 571 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) | 592 'file://blank.html', ps, base_dir=util.GetUnittestDataDir()) |
| 572 ps.pages.append(page5) | 593 ps.pages.append(page5) |
| 573 | 594 |
| 574 options = options_for_unittests.GetCopy() | 595 options = options_for_unittests.GetCopy() |
| 575 options.output_format = 'none' | 596 options.output_format = 'none' |
| 576 SetUpPageRunnerArguments(options) | 597 SetUpPageRunnerArguments(options) |
| 577 results = page_runner.Run(Test(max_failures=2), ps, expectations, options) | 598 results = results_options.CreateResults(EmptyMetadataForTest(), options) |
| 599 page_runner.Run(Test(max_failures=2), ps, expectations, options, results) |
| 578 self.assertEquals(0, len(GetSuccessfulPageRuns(results))) | 600 self.assertEquals(0, len(GetSuccessfulPageRuns(results))) |
| 579 # Runs up to max_failures+1 failing tests before stopping, since | 601 # Runs up to max_failures+1 failing tests before stopping, since |
| 580 # every tests after max_failures failures have been encountered | 602 # every tests after max_failures failures have been encountered |
| 581 # may all be passing. | 603 # may all be passing. |
| 582 self.assertEquals(3, len(results.failures)) | 604 self.assertEquals(3, len(results.failures)) |
| 583 self.assertTrue(page1.was_run) | 605 self.assertTrue(page1.was_run) |
| 584 self.assertTrue(page2.was_run) | 606 self.assertTrue(page2.was_run) |
| 585 self.assertTrue(page3.was_run) | 607 self.assertTrue(page3.was_run) |
| 586 self.assertFalse(page4.was_run) | 608 self.assertFalse(page4.was_run) |
| 587 self.assertFalse(page5.was_run) | 609 self.assertFalse(page5.was_run) |
| OLD | NEW |