| 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 hashlib | 13 import hashlib |
| 14 import os | 14 import os |
| 15 import platform | 15 import platform |
| 16 import sys | 16 import sys |
| 17 import unittest | 17 import unittest |
| 18 import urllib | 18 import urllib |
| 19 import urllib2 | 19 import urllib2 |
| 20 import urlparse | 20 import urlparse |
| 21 | 21 |
| 22 from chromedriver_launcher import ChromeDriverLauncher | 22 from chromedriver_launcher import ChromeDriverLauncher |
| 23 import chromedriver_paths | 23 import chromedriver_paths |
| 24 from gtest_text_test_runner import GTestTextTestRunner | 24 from gtest_text_test_runner import GTestTextTestRunner |
| 25 | 25 |
| 26 sys.path += [chromedriver_paths.SRC_THIRD_PARTY] | 26 sys.path += [chromedriver_paths.SRC_THIRD_PARTY] |
| 27 sys.path += [chromedriver_paths.PYTHON_BINDINGS] | 27 sys.path += [chromedriver_paths.PYTHON_BINDINGS] |
| 28 | 28 |
| 29 import simplejson as json | 29 try: |
| 30 import simplejson as json |
| 31 except ImportError: |
| 32 import json |
| 30 | 33 |
| 31 from selenium.webdriver.remote.command import Command | 34 from selenium.webdriver.remote.command import Command |
| 32 from selenium.webdriver.remote.webdriver import WebDriver | 35 from selenium.webdriver.remote.webdriver import WebDriver |
| 36 from selenium.webdriver.common.keys import Keys |
| 37 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
| 33 | 38 |
| 34 | 39 |
| 35 def DataDir(): | 40 def DataDir(): |
| 36 """Returns the path to the data dir chrome/test/data.""" | 41 """Returns the path to the data dir chrome/test/data.""" |
| 37 return os.path.normpath( | 42 return os.path.normpath( |
| 38 os.path.join(os.path.dirname(__file__), os.pardir, "data")) | 43 os.path.join(os.path.dirname(__file__), os.pardir, "data")) |
| 39 | 44 |
| 40 | 45 |
| 41 def GetFileURLForPath(path): | 46 def GetFileURLForPath(path): |
| 42 """Get file:// url for the given path. | 47 """Get file:// url for the given path. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 request_url = self._launcher.GetURL() + '/favicon.ico' | 144 request_url = self._launcher.GetURL() + '/favicon.ico' |
| 140 response = SendRequest(request_url, method='GET') | 145 response = SendRequest(request_url, method='GET') |
| 141 try: | 146 try: |
| 142 self.assertEquals(204, response.code) | 147 self.assertEquals(204, response.code) |
| 143 finally: | 148 finally: |
| 144 response.close() | 149 response.close() |
| 145 | 150 |
| 146 def testCanStartChromeDriverOnSpecificPort(self): | 151 def testCanStartChromeDriverOnSpecificPort(self): |
| 147 launcher = ChromeDriverLauncher(port=9520) | 152 launcher = ChromeDriverLauncher(port=9520) |
| 148 self.assertEquals(9520, launcher.GetPort()) | 153 self.assertEquals(9520, launcher.GetPort()) |
| 149 driver = WebDriver(launcher.GetURL(), {}) | 154 driver = WebDriver(launcher.GetURL(), DesiredCapabilities.CHROME) |
| 150 driver.quit() | 155 driver.quit() |
| 151 launcher.Kill() | 156 launcher.Kill() |
| 152 | 157 |
| 153 | 158 |
| 159 class NativeInputTest(unittest.TestCase): |
| 160 """Native input ChromeDriver tests.""" |
| 161 |
| 162 def setUp(self): |
| 163 self._launcher = ChromeDriverLauncher() |
| 164 self._capabilities = DesiredCapabilities.CHROME |
| 165 self._capabilities["chrome"] = { "nativeEvents" : True } |
| 166 |
| 167 def tearDown(self): |
| 168 self._launcher.Kill() |
| 169 |
| 170 def testCanStartsWithNativeEvents(self): |
| 171 driver = WebDriver(self._launcher.GetURL(), self._capabilities) |
| 172 self.assertTrue(driver.capabilities["chrome"].has_key("nativeEvents")) |
| 173 self.assertTrue(driver.capabilities["chrome"]["nativeEvents"]) |
| 174 |
| 175 def testSendKeysNative(self): |
| 176 driver = WebDriver(self._launcher.GetURL(), self._capabilities) |
| 177 driver.get(self._launcher.GetURL() + '/test_page.html') |
| 178 # Find the text input. |
| 179 q = driver.find_element_by_name("key_input_test") |
| 180 # Send some keys. |
| 181 q.send_keys("tokyo") |
| 182 #TODO(timothe): change to .text when beta 4 wrappers are out. |
| 183 self.assertEqual(q.value, "tokyo") |
| 184 |
| 185 #@unittest.skip("Need to run this on a machine with an IME installed.") |
| 186 #def testSendKeysNativeProcessedByIME(self): |
| 187 #driver = WebDriver(self._launcher.GetURL(), self.capabilities) |
| 188 #driver.get(self._launcher.GetURL() + '/test_page.html') |
| 189 #q = driver.find_element_by_name("key_input_test") |
| 190 ## Send key combination to turn IME on. |
| 191 #q.send_keys(Keys.F7) |
| 192 #q.send_keys("toukyou") |
| 193 ## Now turning it off. |
| 194 #q.send_keys(Keys.F7) |
| 195 #self.assertEqual(q.value, "\xe6\x9d\xb1\xe4\xba\xac") |
| 196 |
| 197 |
| 154 class CookieTest(unittest.TestCase): | 198 class CookieTest(unittest.TestCase): |
| 155 """Cookie test for the json webdriver protocol""" | 199 """Cookie test for the json webdriver protocol""" |
| 156 | 200 |
| 157 def setUp(self): | 201 def setUp(self): |
| 158 self._launcher = ChromeDriverLauncher() | 202 self._launcher = ChromeDriverLauncher() |
| 159 self._driver = WebDriver(self._launcher.GetURL(), {}) | 203 self._driver = WebDriver(self._launcher.GetURL(), |
| 204 DesiredCapabilities.CHROME) |
| 160 | 205 |
| 161 def tearDown(self): | 206 def tearDown(self): |
| 162 self._driver.quit() | 207 self._driver.quit() |
| 163 self._launcher.Kill() | 208 self._launcher.Kill() |
| 164 | 209 |
| 165 def testAddCookie(self): | 210 def testAddCookie(self): |
| 166 self._driver.get(self._launcher.GetURL() + '/test_page.html') | 211 self._driver.get(self._launcher.GetURL() + '/test_page.html') |
| 167 cookie_dict = None | 212 cookie_dict = None |
| 168 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") | 213 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") |
| 169 cookie_dict = {} | 214 cookie_dict = {} |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 self.assertEquals('windows', capabilities['platform'].lower()) | 298 self.assertEquals('windows', capabilities['platform'].lower()) |
| 254 elif system == 'Darwin': | 299 elif system == 'Darwin': |
| 255 self.assertEquals('mac', capabilities['platform'].lower()) | 300 self.assertEquals('mac', capabilities['platform'].lower()) |
| 256 else: | 301 else: |
| 257 # No python on ChromeOS, so we won't have a platform value, but | 302 # No python on ChromeOS, so we won't have a platform value, but |
| 258 # the server will know and return the value accordingly. | 303 # the server will know and return the value accordingly. |
| 259 self.assertEquals('chromeos', capabilities['platform'].lower()) | 304 self.assertEquals('chromeos', capabilities['platform'].lower()) |
| 260 driver.quit() | 305 driver.quit() |
| 261 | 306 |
| 262 def testSessionCreationDeletion(self): | 307 def testSessionCreationDeletion(self): |
| 263 driver = WebDriver(self._launcher.GetURL(), {}) | 308 driver = WebDriver(self._launcher.GetURL(), DesiredCapabilities.CHROME) |
| 264 driver.quit() | 309 driver.quit() |
| 265 | 310 |
| 266 def testMultipleSessionCreationDeletion(self): | 311 def testMultipleSessionCreationDeletion(self): |
| 267 for i in range(10): | 312 for i in range(10): |
| 268 driver = WebDriver(self._launcher.GetURL(), {}) | 313 driver = WebDriver(self._launcher.GetURL(), DesiredCapabilities.CHROME) |
| 269 driver.quit() | 314 driver.quit() |
| 270 | 315 |
| 271 def testSessionCommandsAfterSessionDeletionReturn404(self): | 316 def testSessionCommandsAfterSessionDeletionReturn404(self): |
| 272 driver = WebDriver(self._launcher.GetURL(), {}) | 317 driver = WebDriver(self._launcher.GetURL(), DesiredCapabilities.CHROME) |
| 273 session_id = driver.session_id | 318 session_id = driver.session_id |
| 274 driver.quit() | 319 driver.quit() |
| 275 try: | 320 try: |
| 276 response = SendRequest(self._launcher.GetURL() + '/session/' + session_id, | 321 response = SendRequest(self._launcher.GetURL() + '/session/' + session_id, |
| 277 method='GET') | 322 method='GET') |
| 278 self.fail('Should have thrown 404 exception') | 323 self.fail('Should have thrown 404 exception') |
| 279 except urllib2.HTTPError, expected: | 324 except urllib2.HTTPError, expected: |
| 280 self.assertEquals(404, expected.code) | 325 self.assertEquals(404, expected.code) |
| 281 | 326 |
| 282 def testMultipleConcurrentSessions(self): | 327 def testMultipleConcurrentSessions(self): |
| 283 drivers = [] | 328 drivers = [] |
| 284 for i in range(10): | 329 for i in range(10): |
| 285 drivers += [WebDriver(self._launcher.GetURL(), {})] | 330 drivers += [WebDriver(self._launcher.GetURL(), |
| 331 DesiredCapabilities.CHROME)] |
| 286 for driver in drivers: | 332 for driver in drivers: |
| 287 driver.quit() | 333 driver.quit() |
| 288 | 334 |
| 289 | 335 |
| 290 class MouseTest(unittest.TestCase): | 336 class MouseTest(unittest.TestCase): |
| 291 """Mouse command tests for the json webdriver protocol""" | 337 """Mouse command tests for the json webdriver protocol""" |
| 292 | 338 |
| 293 def setUp(self): | 339 def setUp(self): |
| 294 self._launcher = ChromeDriverLauncher(root_path=os.path.dirname(__file__)) | 340 self._launcher = ChromeDriverLauncher(root_path=os.path.dirname(__file__)) |
| 295 self._driver = WebDriver(self._launcher.GetURL(), {}) | 341 self._driver = WebDriver(self._launcher.GetURL(), |
| 342 DesiredCapabilities.CHROME) |
| 296 | 343 |
| 297 def tearDown(self): | 344 def tearDown(self): |
| 298 self._driver.quit() | 345 self._driver.quit() |
| 299 self._launcher.Kill() | 346 self._launcher.Kill() |
| 300 | 347 |
| 301 def testClickElementThatNeedsContainerScrolling(self): | 348 def testClickElementThatNeedsContainerScrolling(self): |
| 302 self._driver.get(self._launcher.GetURL() + '/test_page.html') | 349 self._driver.get(self._launcher.GetURL() + '/test_page.html') |
| 303 self._driver.find_element_by_name('hidden_scroll').click() | 350 self._driver.find_element_by_name('hidden_scroll').click() |
| 304 self.assertTrue(self._driver.execute_script('return window.success')) | 351 self.assertTrue(self._driver.execute_script('return window.success')) |
| 305 | 352 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 # for this. | 419 # for this. |
| 373 result = body1.execute(Command.ELEMENT_EQUALS, { | 420 result = body1.execute(Command.ELEMENT_EQUALS, { |
| 374 'other': body2.id | 421 'other': body2.id |
| 375 }) | 422 }) |
| 376 self.assertTrue(result['value']) | 423 self.assertTrue(result['value']) |
| 377 | 424 |
| 378 | 425 |
| 379 if __name__ == '__main__': | 426 if __name__ == '__main__': |
| 380 unittest.main(module='chromedriver_tests', | 427 unittest.main(module='chromedriver_tests', |
| 381 testRunner=GTestTextTestRunner(verbosity=1)) | 428 testRunner=GTestTextTestRunner(verbosity=1)) |
| OLD | NEW |