| 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 |
| 14 import os |
| 13 import platform | 15 import platform |
| 14 import os | |
| 15 import sys | 16 import sys |
| 16 import unittest | 17 import unittest |
| 18 import urllib |
| 17 import urllib2 | 19 import urllib2 |
| 18 import urlparse | 20 import urlparse |
| 19 | 21 |
| 20 from chromedriver_launcher import ChromeDriverLauncher | 22 from chromedriver_launcher import ChromeDriverLauncher |
| 21 import chromedriver_paths | 23 import chromedriver_paths |
| 22 from gtest_text_test_runner import GTestTextTestRunner | 24 from gtest_text_test_runner import GTestTextTestRunner |
| 23 | 25 |
| 24 sys.path += [chromedriver_paths.SRC_THIRD_PARTY] | 26 sys.path += [chromedriver_paths.SRC_THIRD_PARTY] |
| 25 sys.path += [chromedriver_paths.PYTHON_BINDINGS] | 27 sys.path += [chromedriver_paths.PYTHON_BINDINGS] |
| 26 | 28 |
| 27 import simplejson as json | 29 import simplejson as json |
| 28 | 30 |
| 29 from selenium.webdriver.remote.command import Command | 31 from selenium.webdriver.remote.command import Command |
| 30 from selenium.webdriver.remote.webdriver import WebDriver | 32 from selenium.webdriver.remote.webdriver import WebDriver |
| 31 | 33 |
| 32 | 34 |
| 35 def DataDir(): |
| 36 """Returns the path to the data dir chrome/test/data.""" |
| 37 return os.path.normpath( |
| 38 os.path.join(os.path.dirname(__file__), os.pardir, "data")) |
| 39 |
| 40 |
| 41 def GetFileURLForPath(path): |
| 42 """Get file:// url for the given path. |
| 43 Also quotes the url using urllib.quote(). |
| 44 """ |
| 45 abs_path = os.path.abspath(path) |
| 46 if sys.platform == 'win32': |
| 47 # Don't quote the ':' in drive letter ( say, C: ) on win. |
| 48 # Also, replace '\' with '/' as expected in a file:/// url. |
| 49 drive, rest = os.path.splitdrive(abs_path) |
| 50 quoted_path = drive.upper() + urllib.quote((rest.replace('\\', '/'))) |
| 51 return 'file:///' + quoted_path |
| 52 else: |
| 53 quoted_path = urllib.quote(abs_path) |
| 54 return 'file://' + quoted_path |
| 55 |
| 56 |
| 33 class Request(urllib2.Request): | 57 class Request(urllib2.Request): |
| 34 """Extends urllib2.Request to support all HTTP request types.""" | 58 """Extends urllib2.Request to support all HTTP request types.""" |
| 35 | 59 |
| 36 def __init__(self, url, method=None, data=None): | 60 def __init__(self, url, method=None, data=None): |
| 37 """Initialise a new HTTP request. | 61 """Initialise a new HTTP request. |
| 38 | 62 |
| 39 Arguments: | 63 Arguments: |
| 40 url: The full URL to send the request to. | 64 url: The full URL to send the request to. |
| 41 method: The HTTP request method to use; defaults to 'GET'. | 65 method: The HTTP request method to use; defaults to 'GET'. |
| 42 data: The data to send with the request as a string. Defaults to | 66 data: The data to send with the request as a string. Defaults to |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 self.assertNotEqual(cookie_dict, None) | 174 self.assertNotEqual(cookie_dict, None) |
| 151 self.assertEqual(cookie_dict["value"], "this is a test") | 175 self.assertEqual(cookie_dict["value"], "this is a test") |
| 152 | 176 |
| 153 def testDeleteCookie(self): | 177 def testDeleteCookie(self): |
| 154 self.testAddCookie(); | 178 self.testAddCookie(); |
| 155 self._driver.delete_cookie("chromedriver_cookie_test") | 179 self._driver.delete_cookie("chromedriver_cookie_test") |
| 156 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") | 180 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") |
| 157 self.assertEqual(cookie_dict, None) | 181 self.assertEqual(cookie_dict, None) |
| 158 | 182 |
| 159 | 183 |
| 184 class ScreenshotTest(unittest.TestCase): |
| 185 """Tests to verify screenshot retrieval""" |
| 186 |
| 187 REDBOX = "automation_proxy_snapshot/set_size.html" |
| 188 |
| 189 def setUp(self): |
| 190 self._launcher = ChromeDriverLauncher() |
| 191 self._driver = WebDriver(self._launcher.GetURL(), {}) |
| 192 |
| 193 def tearDown(self): |
| 194 self._driver.quit() |
| 195 self._launcher.Kill() |
| 196 |
| 197 def testScreenCaptureAgainstReference(self): |
| 198 # Create a red square of 2000x2000 pixels. |
| 199 url = GetFileURLForPath(os.path.join(DataDir(), |
| 200 self.REDBOX)) |
| 201 url += "?2000,2000" |
| 202 self._driver.get(url) |
| 203 s = self._driver.get_screenshot_as_base64(); |
| 204 self._driver.get_screenshot_as_file("/tmp/foo.png") |
| 205 h = hashlib.md5(s).hexdigest() |
| 206 # Compare the PNG created to the reference hash. |
| 207 self.assertEquals(h, '12c0ade27e3875da3d8866f52d2fa84f') |
| 208 |
| 209 |
| 160 class SessionTest(unittest.TestCase): | 210 class SessionTest(unittest.TestCase): |
| 161 """Tests dealing with WebDriver sessions.""" | 211 """Tests dealing with WebDriver sessions.""" |
| 162 | 212 |
| 163 def setUp(self): | 213 def setUp(self): |
| 164 self._launcher = ChromeDriverLauncher() | 214 self._launcher = ChromeDriverLauncher() |
| 165 | 215 |
| 166 def tearDown(self): | 216 def tearDown(self): |
| 167 self._launcher.Kill() | 217 self._launcher.Kill() |
| 168 | 218 |
| 169 def testCreatingSessionShouldRedirectToCorrectURL(self): | 219 def testCreatingSessionShouldRedirectToCorrectURL(self): |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 # for this. | 372 # for this. |
| 323 result = body1.execute(Command.ELEMENT_EQUALS, { | 373 result = body1.execute(Command.ELEMENT_EQUALS, { |
| 324 'other': body2.id | 374 'other': body2.id |
| 325 }) | 375 }) |
| 326 self.assertTrue(result['value']) | 376 self.assertTrue(result['value']) |
| 327 | 377 |
| 328 | 378 |
| 329 if __name__ == '__main__': | 379 if __name__ == '__main__': |
| 330 unittest.main(module='chromedriver_tests', | 380 unittest.main(module='chromedriver_tests', |
| 331 testRunner=GTestTextTestRunner(verbosity=1)) | 381 testRunner=GTestTextTestRunner(verbosity=1)) |
| OLD | NEW |