Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
|
samuong
2014/10/15 22:08:17
This file is still in the wrong location. Could yo
| |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 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 json | 9 import json |
| 10 import math | 10 import math |
| 11 import optparse | 11 import optparse |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 super(ChromeDriverBaseTest, self).__init__(*args, **kwargs) | 145 super(ChromeDriverBaseTest, self).__init__(*args, **kwargs) |
| 146 self._drivers = [] | 146 self._drivers = [] |
| 147 | 147 |
| 148 def tearDown(self): | 148 def tearDown(self): |
| 149 for driver in self._drivers: | 149 for driver in self._drivers: |
| 150 try: | 150 try: |
| 151 driver.Quit() | 151 driver.Quit() |
| 152 except: | 152 except: |
| 153 pass | 153 pass |
| 154 | 154 |
| 155 def CreateDriver(self, server_url=None, **kwargs): | 155 def CreateDriver(self, server_url=None, download_dir=None, **kwargs): |
| 156 if server_url is None: | 156 if server_url is None: |
| 157 server_url = _CHROMEDRIVER_SERVER_URL | 157 server_url = _CHROMEDRIVER_SERVER_URL |
| 158 | 158 |
| 159 android_package = None | 159 android_package = None |
| 160 android_activity = None | 160 android_activity = None |
| 161 android_process = None | 161 android_process = None |
| 162 if _ANDROID_PACKAGE_KEY: | 162 if _ANDROID_PACKAGE_KEY: |
| 163 android_package = constants.PACKAGE_INFO[_ANDROID_PACKAGE_KEY].package | 163 android_package = constants.PACKAGE_INFO[_ANDROID_PACKAGE_KEY].package |
| 164 if _ANDROID_PACKAGE_KEY == 'chromedriver_webview_shell': | 164 if _ANDROID_PACKAGE_KEY == 'chromedriver_webview_shell': |
| 165 android_activity = constants.PACKAGE_INFO[_ANDROID_PACKAGE_KEY].activity | 165 android_activity = constants.PACKAGE_INFO[_ANDROID_PACKAGE_KEY].activity |
| 166 android_process = '%s:main' % android_package | 166 android_process = '%s:main' % android_package |
| 167 | 167 |
| 168 driver = chromedriver.ChromeDriver(server_url, | 168 driver = chromedriver.ChromeDriver(server_url, download_dir, |
| 169 chrome_binary=_CHROME_BINARY, | 169 chrome_binary=_CHROME_BINARY, |
| 170 android_package=android_package, | 170 android_package=android_package, |
| 171 android_activity=android_activity, | 171 android_activity=android_activity, |
| 172 android_process=android_process, | 172 android_process=android_process, |
| 173 **kwargs) | 173 **kwargs) |
| 174 self._drivers += [driver] | 174 self._drivers += [driver] |
| 175 return driver | 175 return driver |
| 176 | 176 |
| 177 | 177 |
| 178 class ChromeDriverTest(ChromeDriverBaseTest): | 178 class ChromeDriverTest(ChromeDriverBaseTest): |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 196 def GlobalTearDown(): | 196 def GlobalTearDown(): |
| 197 if _ANDROID_PACKAGE_KEY: | 197 if _ANDROID_PACKAGE_KEY: |
| 198 forwarder.Forwarder.UnmapAllDevicePorts(ChromeDriverTest._device) | 198 forwarder.Forwarder.UnmapAllDevicePorts(ChromeDriverTest._device) |
| 199 ChromeDriverTest._http_server.Shutdown() | 199 ChromeDriverTest._http_server.Shutdown() |
| 200 | 200 |
| 201 @staticmethod | 201 @staticmethod |
| 202 def GetHttpUrlForFile(file_path): | 202 def GetHttpUrlForFile(file_path): |
| 203 return ChromeDriverTest._http_server.GetUrl() + file_path | 203 return ChromeDriverTest._http_server.GetUrl() + file_path |
| 204 | 204 |
| 205 def setUp(self): | 205 def setUp(self): |
| 206 self._driver = self.CreateDriver() | 206 if self._testMethodName == 'testFileDownLoad': |
| 207 self.download_dir = tempfile.mkdtemp(dir='/tmp') | |
|
samuong
2014/10/15 22:08:18
By default, tempfile.mkdtemp() will use dir='/tmp'
| |
| 208 else: | |
| 209 self.download_dir = None | |
| 210 self._driver = self.CreateDriver(download_dir=self.download_dir) | |
| 211 | |
| 212 def tearDown(self): | |
| 213 try: | |
| 214 if self.download_dir: | |
| 215 os.removedirs(self.download_dir) | |
| 216 self.download_dir = None | |
| 217 except: | |
| 218 pass | |
|
samuong
2014/10/15 22:08:17
If and when this fails, let's print out a proper e
| |
| 219 ChromeDriverBaseTest.tearDown(self) | |
| 207 | 220 |
| 208 def testStartStop(self): | 221 def testStartStop(self): |
| 209 pass | 222 pass |
| 210 | 223 |
| 224 def testFileDownLoad(self): | |
| 225 # check download file to make sure it does not exist | |
| 226 download_name = self.download_dir + "/a_red_dot.png"; | |
|
samuong
2014/10/15 22:08:18
Windows uses "\" as the directory separator, so yo
| |
| 227 self.assertFalse(os.path.isfile(download_name), | |
|
samuong
2014/10/15 22:08:18
If there's a directory called "a_red_dot.png" then
| |
| 228 "Test prefer download directory failed! - download file already exist") | |
| 229 # | |
| 230 self._driver.Load(self.GetHttpUrlForFile('/chromedriver/download.html')) | |
| 231 self._driver.FindElement('id', 'red-dot').Click() | |
| 232 # wait until download completed | |
| 233 timeout = time.time() + 60*1 # 1 minutes from now | |
|
samuong
2014/10/15 22:08:18
How about we call this variable "deadline" instead
| |
| 234 while True: | |
| 235 time.sleep(0.1) | |
| 236 if os.path.isfile(download_name) or time.time() > timeout: | |
| 237 break | |
| 238 # | |
| 239 self.assertTrue(os.path.isfile(download_name), | |
| 240 "Test user prefer download directory failed!") | |
| 241 os.remove(download_name) | |
| 242 | |
| 211 def testLoadUrl(self): | 243 def testLoadUrl(self): |
| 212 self._driver.Load(self.GetHttpUrlForFile('/chromedriver/empty.html')) | 244 self._driver.Load(self.GetHttpUrlForFile('/chromedriver/empty.html')) |
| 213 | 245 |
| 214 def testGetCurrentWindowHandle(self): | 246 def testGetCurrentWindowHandle(self): |
| 215 self._driver.GetCurrentWindowHandle() | 247 self._driver.GetCurrentWindowHandle() |
| 216 | 248 |
| 217 def _WaitForNewWindow(self, old_handles): | 249 def _WaitForNewWindow(self, old_handles): |
| 218 """Wait for at least one new window to show up in 20 seconds. | 250 """Wait for at least one new window to show up in 20 seconds. |
| 219 | 251 |
| 220 Args: | 252 Args: |
| (...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 743 except urllib2.URLError as e: | 775 except urllib2.URLError as e: |
| 744 print 'Unable to fetch current version info from omahaproxy (%s)' % e | 776 print 'Unable to fetch current version info from omahaproxy (%s)' % e |
| 745 | 777 |
| 746 def testDeviceManagement(self): | 778 def testDeviceManagement(self): |
| 747 self._drivers = [self.CreateDriver() for x in | 779 self._drivers = [self.CreateDriver() for x in |
| 748 android_commands.GetAttachedDevices()] | 780 android_commands.GetAttachedDevices()] |
| 749 self.assertRaises(chromedriver.UnknownError, self.CreateDriver) | 781 self.assertRaises(chromedriver.UnknownError, self.CreateDriver) |
| 750 self._drivers[0].Quit() | 782 self._drivers[0].Quit() |
| 751 self._drivers[0] = self.CreateDriver() | 783 self._drivers[0] = self.CreateDriver() |
| 752 | 784 |
| 785 class ChromeDownloadDirTest(ChromeDriverBaseTest): | |
| 786 | |
| 787 # test existing prefence profile - check setting if it is correct | |
| 788 def testDownloadDirectoryOverridesExistingPreferences(self): | |
| 789 userDataDir='/usr/local/google/home/andrewcheng/zz' | |
|
samuong
2014/10/15 22:08:17
Please don't hardcode your home directory into tes
| |
| 790 defPrefFile = userDataDir+'/Default/Preferences' | |
|
samuong
2014/10/15 22:08:17
For both defPrefFile and userDataDir, use names wi
| |
| 791 # | |
|
samuong
2014/10/15 22:08:18
Also for consistency with the existing style in th
| |
| 792 data = {} | |
| 793 test_dir = {} | |
| 794 test_dir['directory_upgrade'] = 'true' | |
| 795 test_dir['default_directory'] = '/old/download/directory' | |
| 796 data['test'] = 'this should not be changed' | |
| 797 data['download'] = test_dir | |
|
samuong
2014/10/15 22:08:18
How about we change these lines above so that data
| |
| 798 # | |
| 799 with open(defPrefFile, 'w') as outfile: | |
| 800 json.dump(data, outfile) | |
| 801 | |
| 802 driver = self.CreateDriver(chrome_switches= | |
| 803 ['user-data-dir='+userDataDir], | |
| 804 download_dir="/usr/local/google/home/andrewcheng/zz") | |
|
samuong
2014/10/15 22:08:17
You're setting the user data directory to the same
| |
| 805 | |
| 806 with open(defPrefFile) as inFile: data = json.load(inFile) | |
|
samuong
2014/10/15 22:08:18
Please break the above line up into two lines:
wi
| |
| 807 self.assertEqual(data['test'], 'this should not be changed', | |
|
samuong
2014/10/15 22:08:17
Before doing this assert, please also check that t
| |
| 808 "test data has changed") | |
|
samuong
2014/10/15 22:08:18
"test data has changed" doesn't really tell anyone
| |
| 809 download = data['download'] | |
| 810 self.assertEqual(download['default_directory'], | |
| 811 '/usr/local/google/home/andrewcheng/zz', | |
| 812 "default_directory is not set") | |
| 753 | 813 |
| 754 class ChromeSwitchesCapabilityTest(ChromeDriverBaseTest): | 814 class ChromeSwitchesCapabilityTest(ChromeDriverBaseTest): |
| 755 """Tests that chromedriver properly processes chromeOptions.args capabilities. | 815 """Tests that chromedriver properly processes chromeOptions.args capabilities. |
| 756 | 816 |
| 757 Makes sure the switches are passed to Chrome. | 817 Makes sure the switches are passed to Chrome. |
| 758 """ | 818 """ |
| 759 | 819 |
| 760 def testSwitchWithoutArgument(self): | 820 def testSwitchWithoutArgument(self): |
| 761 """Tests that switch --dom-automation can be passed to Chrome. | 821 """Tests that switch --dom-automation can be passed to Chrome. |
| 762 | 822 |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1171 | 1231 |
| 1172 all_tests_suite = unittest.defaultTestLoader.loadTestsFromModule( | 1232 all_tests_suite = unittest.defaultTestLoader.loadTestsFromModule( |
| 1173 sys.modules[__name__]) | 1233 sys.modules[__name__]) |
| 1174 tests = unittest_util.FilterTestSuite(all_tests_suite, options.filter) | 1234 tests = unittest_util.FilterTestSuite(all_tests_suite, options.filter) |
| 1175 ChromeDriverTest.GlobalSetUp() | 1235 ChromeDriverTest.GlobalSetUp() |
| 1176 MobileEmulationCapabilityTest.GlobalSetUp() | 1236 MobileEmulationCapabilityTest.GlobalSetUp() |
| 1177 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(tests) | 1237 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(tests) |
| 1178 ChromeDriverTest.GlobalTearDown() | 1238 ChromeDriverTest.GlobalTearDown() |
| 1179 MobileEmulationCapabilityTest.GlobalTearDown() | 1239 MobileEmulationCapabilityTest.GlobalTearDown() |
| 1180 sys.exit(len(result.failures) + len(result.errors)) | 1240 sys.exit(len(result.failures) + len(result.errors)) |
| OLD | NEW |