OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """End to end tests for ChromeDriver.""" | 6 """End to end tests for ChromeDriver.""" |
7 | 7 |
8 import base64 | 8 import base64 |
9 import optparse | 9 import optparse |
10 import os | 10 import os |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 'ChromeDriverTest.testShouldHandleNewWindowLoadingProperly', | 89 'ChromeDriverTest.testShouldHandleNewWindowLoadingProperly', |
90 # https://code.google.com/p/chromedriver/issues/detail?id=259 | 90 # https://code.google.com/p/chromedriver/issues/detail?id=259 |
91 'ChromeDriverTest.testSendKeysToElement', | 91 'ChromeDriverTest.testSendKeysToElement', |
92 # https://code.google.com/p/chromedriver/issues/detail?id=270 | 92 # https://code.google.com/p/chromedriver/issues/detail?id=270 |
93 'ChromeDriverTest.testPopups', | 93 'ChromeDriverTest.testPopups', |
94 # https://code.google.com/p/chromedriver/issues/detail?id=298 | 94 # https://code.google.com/p/chromedriver/issues/detail?id=298 |
95 'ChromeDriverTest.testWindowPosition', | 95 'ChromeDriverTest.testWindowPosition', |
96 'ChromeDriverTest.testWindowSize', | 96 'ChromeDriverTest.testWindowSize', |
97 'ChromeDriverTest.testWindowMaximize', | 97 'ChromeDriverTest.testWindowMaximize', |
98 'ChromeLogPathCapabilityTest.testChromeLogPath', | 98 'ChromeLogPathCapabilityTest.testChromeLogPath', |
| 99 # Don't enable perf testing on Android yet. |
| 100 'PerfTest.testSessionStartTime', |
| 101 'PerfTest.testSessionStopTime', |
| 102 'PerfTest.testColdExecuteScript', |
99 ] | 103 ] |
100 ) | 104 ) |
101 _ANDROID_NEGATIVE_FILTER['org.chromium.chrome.testshell'] = ( | 105 _ANDROID_NEGATIVE_FILTER['org.chromium.chrome.testshell'] = ( |
102 _ANDROID_NEGATIVE_FILTER['com.google.android.apps.chrome'] + [] | 106 _ANDROID_NEGATIVE_FILTER['com.google.android.apps.chrome'] + [] |
103 ) | 107 ) |
104 | 108 |
105 | 109 |
106 class ChromeDriverBaseTest(unittest.TestCase): | 110 class ChromeDriverBaseTest(unittest.TestCase): |
107 """Base class for testing chromedriver functionalities.""" | 111 """Base class for testing chromedriver functionalities.""" |
108 | 112 |
109 def __init__(self, *args, **kwargs): | 113 def __init__(self, *args, **kwargs): |
110 super(ChromeDriverBaseTest, self).__init__(*args, **kwargs) | 114 super(ChromeDriverBaseTest, self).__init__(*args, **kwargs) |
111 self._drivers = [] | 115 self._drivers = [] |
112 | 116 |
113 def tearDown(self): | 117 def tearDown(self): |
114 for driver in self._drivers: | 118 for driver in self._drivers: |
115 try: | 119 try: |
116 driver.Quit() | 120 driver.Quit() |
117 except chromedriver.ChromeDriverException: | 121 except: |
118 pass | 122 pass |
119 | 123 |
120 def CreateDriver(self, **kwargs): | 124 def CreateDriver(self, server_url=None, **kwargs): |
121 driver = chromedriver.ChromeDriver(_CHROMEDRIVER_SERVER_URL, | 125 if server_url is None: |
| 126 server_url = _CHROMEDRIVER_SERVER_URL |
| 127 driver = chromedriver.ChromeDriver(server_url, |
122 chrome_binary=_CHROME_BINARY, | 128 chrome_binary=_CHROME_BINARY, |
123 android_package=_ANDROID_PACKAGE, | 129 android_package=_ANDROID_PACKAGE, |
124 **kwargs) | 130 **kwargs) |
125 self._drivers += [driver] | 131 self._drivers += [driver] |
126 return driver | 132 return driver |
127 | 133 |
128 | 134 |
129 class ChromeDriverTest(ChromeDriverBaseTest): | 135 class ChromeDriverTest(ChromeDriverBaseTest): |
130 """End to end tests for ChromeDriver.""" | 136 """End to end tests for ChromeDriver.""" |
131 | 137 |
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 | 607 |
602 Verifies that a log message is written into the specified log file. | 608 Verifies that a log message is written into the specified log file. |
603 """ | 609 """ |
604 tmp_log_path = tempfile.NamedTemporaryFile() | 610 tmp_log_path = tempfile.NamedTemporaryFile() |
605 driver = self.CreateDriver(chrome_log_path=tmp_log_path.name) | 611 driver = self.CreateDriver(chrome_log_path=tmp_log_path.name) |
606 driver.ExecuteScript('console.info("%s")' % self.LOG_MESSAGE) | 612 driver.ExecuteScript('console.info("%s")' % self.LOG_MESSAGE) |
607 driver.Quit() | 613 driver.Quit() |
608 self.assertTrue(self.LOG_MESSAGE in open(tmp_log_path.name).read()) | 614 self.assertTrue(self.LOG_MESSAGE in open(tmp_log_path.name).read()) |
609 | 615 |
610 | 616 |
| 617 class PerfTest(ChromeDriverBaseTest): |
| 618 """Tests for ChromeDriver perf.""" |
| 619 def setUp(self): |
| 620 self.assertTrue(_REFERENCE_CHROMEDRIVER is not None, |
| 621 'must supply a reference-chromedriver arg') |
| 622 |
| 623 def _RunDriverPerfTest(self, name, test_func): |
| 624 """Runs a perf test comparing a reference and new ChromeDriver server. |
| 625 |
| 626 Args: |
| 627 name: The name of the perf test. |
| 628 test_func: Called with the server url to perform the test action. Must |
| 629 return the time elapsed. |
| 630 """ |
| 631 class Results(object): |
| 632 ref = [] |
| 633 new = [] |
| 634 |
| 635 ref_server = chromedriver.Server(_REFERENCE_CHROMEDRIVER) |
| 636 results = Results() |
| 637 result_url_pairs = zip([results.new, results.ref], |
| 638 [_CHROMEDRIVER_SERVER_URL, ref_server.GetUrl()]) |
| 639 for iteration in range(30): |
| 640 for result, url in result_url_pairs: |
| 641 result += [test_func(url)] |
| 642 # Reverse the order for the next run. |
| 643 result_url_pairs = result_url_pairs[::-1] |
| 644 |
| 645 def PrintResult(build, result): |
| 646 mean = sum(result) / len(result) |
| 647 avg_dev = sum([abs(sample - mean) for sample in result]) / len(result) |
| 648 print 'perf result', build, name, mean, avg_dev, result |
| 649 util.AddBuildStepText(u'%s %s: %.3f\u00b1%.3f' % ( |
| 650 build, name, mean, avg_dev)) |
| 651 |
| 652 # Discard first result, which may be off due to cold start. |
| 653 PrintResult('new', results.new[1:]) |
| 654 PrintResult('ref', results.ref[1:]) |
| 655 |
| 656 def testSessionStartTime(self): |
| 657 def Run(url): |
| 658 start = time.time() |
| 659 driver = self.CreateDriver(url) |
| 660 end = time.time() |
| 661 driver.Quit() |
| 662 return end - start |
| 663 self._RunDriverPerfTest('session start', Run) |
| 664 |
| 665 def testSessionStopTime(self): |
| 666 def Run(url): |
| 667 driver = self.CreateDriver(url) |
| 668 start = time.time() |
| 669 driver.Quit() |
| 670 end = time.time() |
| 671 return end - start |
| 672 self._RunDriverPerfTest('session stop', Run) |
| 673 |
| 674 def testColdExecuteScript(self): |
| 675 def Run(url): |
| 676 driver = self.CreateDriver(url) |
| 677 start = time.time() |
| 678 driver.ExecuteScript('return 1') |
| 679 end = time.time() |
| 680 driver.Quit() |
| 681 return end - start |
| 682 self._RunDriverPerfTest('cold exe js', Run) |
| 683 |
611 if __name__ == '__main__': | 684 if __name__ == '__main__': |
612 parser = optparse.OptionParser() | 685 parser = optparse.OptionParser() |
613 parser.add_option( | 686 parser.add_option( |
614 '', '--chromedriver', | 687 '', '--chromedriver', |
615 help='Path to chromedriver server (REQUIRED!)') | 688 help='Path to chromedriver server (REQUIRED!)') |
616 parser.add_option( | 689 parser.add_option( |
| 690 '', '--reference-chromedriver', |
| 691 help='Path to the reference chromedriver server') |
| 692 parser.add_option( |
617 '', '--chrome', help='Path to a build of the chrome binary') | 693 '', '--chrome', help='Path to a build of the chrome binary') |
618 parser.add_option( | 694 parser.add_option( |
619 '', '--chrome-version', default='HEAD', | 695 '', '--chrome-version', default='HEAD', |
620 help='Version of chrome. Default is \'HEAD\'.') | 696 help='Version of chrome. Default is \'HEAD\'.') |
621 parser.add_option( | 697 parser.add_option( |
622 '', '--filter', type='string', default='*', | 698 '', '--filter', type='string', default='*', |
623 help=('Filter for specifying what tests to run, "*" will run all. E.g., ' | 699 help=('Filter for specifying what tests to run, "*" will run all. E.g., ' |
624 '*testStartStop')) | 700 '*testStartStop')) |
625 parser.add_option( | 701 parser.add_option( |
626 '', '--android-package', help='Android package name') | 702 '', '--android-package', help='Android package name') |
627 options, args = parser.parse_args() | 703 options, args = parser.parse_args() |
628 | 704 |
629 if not options.chromedriver or not os.path.exists(options.chromedriver): | 705 if not options.chromedriver or not os.path.exists(options.chromedriver): |
630 parser.error('chromedriver is required or the given path is invalid.' + | 706 parser.error('chromedriver is required or the given path is invalid.' + |
631 'Please run "%s --help" for help' % __file__) | 707 'Please run "%s --help" for help' % __file__) |
632 | 708 |
633 server = chromedriver.Server(os.path.abspath(options.chromedriver)) | 709 server = chromedriver.Server(os.path.abspath(options.chromedriver)) |
634 global _CHROMEDRIVER_SERVER_URL | 710 global _CHROMEDRIVER_SERVER_URL |
635 _CHROMEDRIVER_SERVER_URL = server.GetUrl() | 711 _CHROMEDRIVER_SERVER_URL = server.GetUrl() |
636 | 712 |
| 713 global _REFERENCE_CHROMEDRIVER |
| 714 _REFERENCE_CHROMEDRIVER = options.reference_chromedriver |
| 715 |
637 global _CHROME_BINARY | 716 global _CHROME_BINARY |
638 if options.chrome: | 717 if options.chrome: |
639 _CHROME_BINARY = os.path.abspath(options.chrome) | 718 _CHROME_BINARY = os.path.abspath(options.chrome) |
640 else: | 719 else: |
641 _CHROME_BINARY = None | 720 _CHROME_BINARY = None |
642 | 721 |
643 global _ANDROID_PACKAGE | 722 global _ANDROID_PACKAGE |
644 _ANDROID_PACKAGE = options.android_package | 723 _ANDROID_PACKAGE = options.android_package |
645 | 724 |
646 if options.filter == '*': | 725 if options.filter == '*': |
647 if _ANDROID_PACKAGE: | 726 if _ANDROID_PACKAGE: |
648 negative_filter = _ANDROID_NEGATIVE_FILTER[_ANDROID_PACKAGE] | 727 negative_filter = _ANDROID_NEGATIVE_FILTER[_ANDROID_PACKAGE] |
649 else: | 728 else: |
650 negative_filter = _DESKTOP_NEGATIVE_FILTER[options.chrome_version] | 729 negative_filter = _DESKTOP_NEGATIVE_FILTER[options.chrome_version] |
651 options.filter = '*-' + ':__main__.'.join([''] + negative_filter) | 730 options.filter = '*-' + ':__main__.'.join([''] + negative_filter) |
652 | 731 |
653 all_tests_suite = unittest.defaultTestLoader.loadTestsFromModule( | 732 all_tests_suite = unittest.defaultTestLoader.loadTestsFromModule( |
654 sys.modules[__name__]) | 733 sys.modules[__name__]) |
655 tests = unittest_util.FilterTestSuite(all_tests_suite, options.filter) | 734 tests = unittest_util.FilterTestSuite(all_tests_suite, options.filter) |
656 ChromeDriverTest.GlobalSetUp() | 735 ChromeDriverTest.GlobalSetUp() |
657 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(tests) | 736 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(tests) |
658 ChromeDriverTest.GlobalTearDown() | 737 ChromeDriverTest.GlobalTearDown() |
659 sys.exit(len(result.failures) + len(result.errors)) | 738 sys.exit(len(result.failures) + len(result.errors)) |
OLD | NEW |