Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 | 2 |
| 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Tests for ChromeDriver. | 7 """Tests for ChromeDriver. |
| 8 | 8 |
| 9 If your test is testing a specific part of the WebDriver API, consider adding | 9 If your test is testing a specific part of the WebDriver API, consider adding |
| 10 it to the appropriate place in the WebDriver tree instead. | 10 it to the appropriate place in the WebDriver tree instead. |
| 11 """ | 11 """ |
| 12 | 12 |
| 13 import platform | 13 import platform |
| 14 import os | 14 import os |
| 15 import sys | 15 import sys |
| 16 import unittest | 16 import unittest |
| 17 import urllib2 | 17 import urllib2 |
| 18 import urlparse | 18 import urlparse |
| 19 | 19 |
| 20 from chromedriver_launcher import ChromeDriverLauncher | 20 from chromedriver_launcher import ChromeDriverLauncher |
| 21 import chromedriver_paths | 21 import chromedriver_paths |
| 22 from gtest_text_test_runner import GTestTextTestRunner | 22 from gtest_text_test_runner import GTestTextTestRunner |
| 23 | 23 |
| 24 sys.path += [chromedriver_paths.SRC_THIRD_PARTY] | 24 sys.path += [chromedriver_paths.SRC_THIRD_PARTY] |
| 25 sys.path += [chromedriver_paths.PYTHON_BINDINGS] | 25 sys.path += [chromedriver_paths.PYTHON_BINDINGS] |
| 26 | 26 |
| 27 import simplejson as json | 27 import simplejson as json |
| 28 | 28 |
| 29 from selenium.webdriver.remote.webdriver import WebDriver | 29 from selenium.webdriver.remote.webdriver import WebDriver |
| 30 from selenium.webdriver.common.keys import Keys | |
| 31 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | |
| 30 | 32 |
| 31 | 33 |
| 32 class Request(urllib2.Request): | 34 class Request(urllib2.Request): |
| 33 """Extends urllib2.Request to support all HTTP request types.""" | 35 """Extends urllib2.Request to support all HTTP request types.""" |
| 34 | 36 |
| 35 def __init__(self, url, method=None, data=None): | 37 def __init__(self, url, method=None, data=None): |
| 36 """Initialise a new HTTP request. | 38 """Initialise a new HTTP request. |
| 37 | 39 |
| 38 Arguments: | 40 Arguments: |
| 39 url: The full URL to send the request to. | 41 url: The full URL to send the request to. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 request_url = self._launcher.GetURL() + '/session/unkown_session_id' | 106 request_url = self._launcher.GetURL() + '/session/unkown_session_id' |
| 105 try: | 107 try: |
| 106 SendRequest(request_url, method='DELETE') | 108 SendRequest(request_url, method='DELETE') |
| 107 self.fail('Should have raised a urllib.HTTPError for returned 404') | 109 self.fail('Should have raised a urllib.HTTPError for returned 404') |
| 108 except urllib2.HTTPError, expected: | 110 except urllib2.HTTPError, expected: |
| 109 self.assertEquals(404, expected.code) | 111 self.assertEquals(404, expected.code) |
| 110 | 112 |
| 111 def testCanStartChromeDriverOnSpecificPort(self): | 113 def testCanStartChromeDriverOnSpecificPort(self): |
| 112 launcher = ChromeDriverLauncher(port=9520) | 114 launcher = ChromeDriverLauncher(port=9520) |
| 113 self.assertEquals(9520, launcher.GetPort()) | 115 self.assertEquals(9520, launcher.GetPort()) |
| 114 driver = WebDriver(launcher.GetURL(), {}) | 116 driver = WebDriver(launcher.GetURL(), DesiredCapabilities.CHROME) |
| 115 driver.quit() | 117 driver.quit() |
| 116 launcher.Kill() | 118 launcher.Kill() |
| 117 | 119 |
| 118 | 120 |
| 121 class NativeInputTest(unittest.TestCase): | |
| 122 """Native input ChromeDriver tests.""" | |
| 123 | |
| 124 SEARCH = "http://www.google.com/webhp?hl=en" | |
| 125 | |
| 126 def setUp(self): | |
| 127 self._launcher = ChromeDriverLauncher() | |
| 128 self.capabilities = DesiredCapabilities.CHROME | |
|
kkania
2011/03/24 23:20:32
I think the style guide suggests prefixing instanc
timothe
2011/03/25 11:07:59
Done.
| |
| 129 self.capabilities["chrome"] = { "nativeEvents" : True } | |
| 130 | |
| 131 def tearDown(self): | |
| 132 self._launcher.Kill() | |
| 133 | |
| 134 def testCanStartsWithNativeEvents(self): | |
| 135 driver = WebDriver(self._launcher.GetURL(), self.capabilities) | |
| 136 self.assertTrue(driver.capabilities["chrome"].has_key("nativeEvents")) | |
| 137 self.assertTrue(driver.capabilities["chrome"]["nativeEvents"]) | |
| 138 | |
| 139 def testSendKeysNative(self): | |
| 140 driver = WebDriver(self._launcher.GetURL(), self.capabilities) | |
| 141 driver.get(self.SEARCH) | |
|
kkania
2011/03/24 23:20:32
I know a few tests here use google search, but ple
timothe
2011/03/25 11:07:59
Done.
| |
| 142 # Find the Google search Button. | |
| 143 q = driver.find_element_by_name("q") | |
| 144 # Send some keys. | |
| 145 q.send_keys("tokyo") | |
| 146 self.assertEqual(q.value, "tokyo") | |
| 147 | |
| 148 #@unittest.skip("Need to run this on a machine with an IME installed.") | |
| 149 #def testSendKeysNativeProcessedByIME(self): | |
| 150 #driver = WebDriver(self._launcher.GetURL(), self.capabilities) | |
| 151 #driver.get(self.SEARCH) | |
| 152 #q = driver.find_element_by_name("q") | |
| 153 ##Send key combination to turn IME on. | |
| 154 #q.send_keys(Keys.F7) | |
| 155 #q.send_keys("toukyou") | |
| 156 #self.assertEqual(q.value, "\xe6\x9d\xb1\xe4\xba\xac") | |
| 157 | |
| 158 | |
| 119 class CookieTest(unittest.TestCase): | 159 class CookieTest(unittest.TestCase): |
| 120 """Cookie test for the json webdriver protocol""" | 160 """Cookie test for the json webdriver protocol""" |
| 121 | 161 |
| 122 def setUp(self): | 162 def setUp(self): |
| 123 self._launcher = ChromeDriverLauncher() | 163 self._launcher = ChromeDriverLauncher() |
| 124 self._driver = WebDriver(self._launcher.GetURL(), {}) | 164 self._driver = WebDriver(self._launcher.GetURL(), |
| 165 DesiredCapabilities.CHROME) | |
| 125 | 166 |
| 126 def tearDown(self): | 167 def tearDown(self): |
| 127 self._driver.quit() | 168 self._driver.quit() |
| 128 self._launcher.Kill() | 169 self._launcher.Kill() |
| 129 | 170 |
| 130 def testAddCookie(self): | 171 def testAddCookie(self): |
| 131 self._driver.get(self._launcher.GetURL() + '/test_page.html') | 172 self._driver.get(self._launcher.GetURL() + '/test_page.html') |
| 132 cookie_dict = None | 173 cookie_dict = None |
| 133 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") | 174 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") |
| 134 cookie_dict = {} | 175 cookie_dict = {} |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 self.assertEquals('windows', capabilities['platform'].lower()) | 233 self.assertEquals('windows', capabilities['platform'].lower()) |
| 193 elif system == 'Darwin': | 234 elif system == 'Darwin': |
| 194 self.assertEquals('mac', capabilities['platform'].lower()) | 235 self.assertEquals('mac', capabilities['platform'].lower()) |
| 195 else: | 236 else: |
| 196 # No python on ChromeOS, so we won't have a platform value, but | 237 # No python on ChromeOS, so we won't have a platform value, but |
| 197 # the server will know and return the value accordingly. | 238 # the server will know and return the value accordingly. |
| 198 self.assertEquals('chromeos', capabilities['platform'].lower()) | 239 self.assertEquals('chromeos', capabilities['platform'].lower()) |
| 199 driver.quit() | 240 driver.quit() |
| 200 | 241 |
| 201 def testSessionCreationDeletion(self): | 242 def testSessionCreationDeletion(self): |
| 202 driver = WebDriver(self._launcher.GetURL(), {}) | 243 driver = WebDriver(self._launcher.GetURL(), DesiredCapabilities.CHROME) |
| 203 driver.quit() | 244 driver.quit() |
| 204 | 245 |
| 205 def testMultipleSessionCreationDeletion(self): | 246 def testMultipleSessionCreationDeletion(self): |
| 206 for i in range(10): | 247 for i in range(10): |
| 207 driver = WebDriver(self._launcher.GetURL(), {}) | 248 driver = WebDriver(self._launcher.GetURL(), DesiredCapabilities.CHROME) |
| 208 driver.quit() | 249 driver.quit() |
| 209 | 250 |
| 210 def testSessionCommandsAfterSessionDeletionReturn404(self): | 251 def testSessionCommandsAfterSessionDeletionReturn404(self): |
| 211 driver = WebDriver(self._launcher.GetURL(), {}) | 252 driver = WebDriver(self._launcher.GetURL(), DesiredCapabilities.CHROME) |
| 212 session_id = driver.session_id | 253 session_id = driver.session_id |
| 213 driver.quit() | 254 driver.quit() |
| 214 try: | 255 try: |
| 215 response = SendRequest(self._launcher.GetURL() + '/session/' + session_id, | 256 response = SendRequest(self._launcher.GetURL() + '/session/' + session_id, |
| 216 method='GET') | 257 method='GET') |
| 217 self.fail('Should have thrown 404 exception') | 258 self.fail('Should have thrown 404 exception') |
| 218 except urllib2.HTTPError, expected: | 259 except urllib2.HTTPError, expected: |
| 219 self.assertEquals(404, expected.code) | 260 self.assertEquals(404, expected.code) |
| 220 | 261 |
| 221 def testMultipleConcurrentSessions(self): | 262 def testMultipleConcurrentSessions(self): |
| 222 drivers = [] | 263 drivers = [] |
| 223 for i in range(10): | 264 for i in range(10): |
| 224 drivers += [WebDriver(self._launcher.GetURL(), {})] | 265 drivers += [WebDriver(self._launcher.GetURL(), |
| 266 DesiredCapabilities.CHROME)] | |
| 225 for driver in drivers: | 267 for driver in drivers: |
| 226 driver.quit() | 268 driver.quit() |
| 227 | 269 |
| 228 | 270 |
| 229 class MouseTest(unittest.TestCase): | 271 class MouseTest(unittest.TestCase): |
| 230 """Mouse command tests for the json webdriver protocol""" | 272 """Mouse command tests for the json webdriver protocol""" |
| 231 | 273 |
| 232 def setUp(self): | 274 def setUp(self): |
| 233 self._launcher = ChromeDriverLauncher(root_path=os.path.dirname(__file__)) | 275 self._launcher = ChromeDriverLauncher(root_path=os.path.dirname(__file__)) |
| 234 self._driver = WebDriver(self._launcher.GetURL(), {}) | 276 self._driver = WebDriver(self._launcher.GetURL(), |
| 277 DesiredCapabilities.CHROME) | |
| 235 | 278 |
| 236 def tearDown(self): | 279 def tearDown(self): |
| 237 self._driver.quit() | 280 self._driver.quit() |
| 238 self._launcher.Kill() | 281 self._launcher.Kill() |
| 239 | 282 |
| 240 def testClickElementThatNeedsContainerScrolling(self): | 283 def testClickElementThatNeedsContainerScrolling(self): |
| 241 self._driver.get(self._launcher.GetURL() + '/test_page.html') | 284 self._driver.get(self._launcher.GetURL() + '/test_page.html') |
| 242 self._driver.find_element_by_name('hidden_scroll').click() | 285 self._driver.find_element_by_name('hidden_scroll').click() |
| 243 self.assertTrue(self._driver.execute_script('return window.success')) | 286 self.assertTrue(self._driver.execute_script('return window.success')) |
| 244 | 287 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 self.assertEquals('', url_parts[0]) | 329 self.assertEquals('', url_parts[0]) |
| 287 self.assertEquals('wd', url_parts[1]) | 330 self.assertEquals('wd', url_parts[1]) |
| 288 self.assertEquals('hub', url_parts[2]) | 331 self.assertEquals('hub', url_parts[2]) |
| 289 self.assertEquals('session', url_parts[3]) | 332 self.assertEquals('session', url_parts[3]) |
| 290 self.assertEquals(data['sessionId'], url_parts[4]) | 333 self.assertEquals(data['sessionId'], url_parts[4]) |
| 291 | 334 |
| 292 | 335 |
| 293 if __name__ == '__main__': | 336 if __name__ == '__main__': |
| 294 unittest.main(module='chromedriver_tests', | 337 unittest.main(module='chromedriver_tests', |
| 295 testRunner=GTestTextTestRunner(verbosity=1)) | 338 testRunner=GTestTextTestRunner(verbosity=1)) |
| OLD | NEW |