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 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.webdriver import WebDriver | 31 from selenium.webdriver.remote.webdriver import WebDriver |
| 30 | 32 |
| 31 | 33 |
| 34 def DataDir(): | |
| 35 """Returns the path to the data dir chrome/test/data.""" | |
| 36 return os.path.normpath( | |
| 37 os.path.join(os.path.dirname(__file__), os.pardir, "data")) | |
| 38 | |
|
kkania
2011/03/22 22:14:29
you forgot to add a newline here
Joe
2011/03/22 22:39:50
sorry about that
On 2011/03/22 22:14:29, kkania w
| |
| 39 def GetFileURLForPath(path): | |
| 40 """Get file:// url for the given path. | |
| 41 Also quotes the url using urllib.quote(). | |
| 42 """ | |
| 43 abs_path = os.path.abspath(path) | |
| 44 if sys.platform == 'win32': | |
| 45 # Don't quote the ':' in drive letter ( say, C: ) on win. | |
| 46 # Also, replace '\' with '/' as expected in a file:/// url. | |
| 47 drive, rest = os.path.splitdrive(abs_path) | |
| 48 quoted_path = drive.upper() + urllib.quote((rest.replace('\\', '/'))) | |
| 49 return 'file:///' + quoted_path | |
| 50 else: | |
| 51 quoted_path = urllib.quote(abs_path) | |
| 52 return 'file://' + quoted_path | |
| 53 | |
| 54 | |
| 32 class Request(urllib2.Request): | 55 class Request(urllib2.Request): |
| 33 """Extends urllib2.Request to support all HTTP request types.""" | 56 """Extends urllib2.Request to support all HTTP request types.""" |
| 34 | 57 |
| 35 def __init__(self, url, method=None, data=None): | 58 def __init__(self, url, method=None, data=None): |
| 36 """Initialise a new HTTP request. | 59 """Initialise a new HTTP request. |
| 37 | 60 |
| 38 Arguments: | 61 Arguments: |
| 39 url: The full URL to send the request to. | 62 url: The full URL to send the request to. |
| 40 method: The HTTP request method to use; defaults to 'GET'. | 63 method: The HTTP request method to use; defaults to 'GET'. |
| 41 data: The data to send with the request as a string. Defaults to | 64 data: The data to send with the request as a string. Defaults to |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 self.assertNotEqual(cookie_dict, None) | 162 self.assertNotEqual(cookie_dict, None) |
| 140 self.assertEqual(cookie_dict["value"], "this is a test") | 163 self.assertEqual(cookie_dict["value"], "this is a test") |
| 141 | 164 |
| 142 def testDeleteCookie(self): | 165 def testDeleteCookie(self): |
| 143 self.testAddCookie(); | 166 self.testAddCookie(); |
| 144 self._driver.delete_cookie("chromedriver_cookie_test") | 167 self._driver.delete_cookie("chromedriver_cookie_test") |
| 145 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") | 168 cookie_dict = self._driver.get_cookie("chromedriver_cookie_test") |
| 146 self.assertEqual(cookie_dict, None) | 169 self.assertEqual(cookie_dict, None) |
| 147 | 170 |
| 148 | 171 |
| 172 class ScreenshotTest(unittest.TestCase): | |
| 173 """Tests to verify screenshot retrieval""" | |
| 174 | |
| 175 REDBOX = "automation_proxy_snapshot/set_size.html" | |
| 176 | |
| 177 def setUp(self): | |
| 178 self._launcher = ChromeDriverLauncher() | |
| 179 self._driver = WebDriver(self._launcher.GetURL(), {}) | |
| 180 | |
| 181 def tearDown(self): | |
| 182 self._driver.quit() | |
| 183 self._launcher.Kill() | |
| 184 | |
| 185 def testScreenCaptureAgainstReference(self): | |
| 186 # Create a red square of 2000x2000 pixels. | |
| 187 url = GetFileURLForPath(os.path.join(DataDir(), | |
| 188 self.REDBOX)) | |
| 189 url += "?2000,2000" | |
| 190 self._driver.get(url) | |
| 191 s = self._driver.get_screenshot_as_base64(); | |
| 192 self._driver.get_screenshot_as_file("/tmp/foo.png") | |
| 193 h = hashlib.md5(s).hexdigest() | |
| 194 # Compare the PNG created to the reference hash. | |
| 195 self.assertEquals(h, '12c0ade27e3875da3d8866f52d2fa84f') | |
|
kkania
2011/03/22 22:14:29
add a newline after this too
Joe
2011/03/22 22:39:50
Done.
| |
| 196 | |
| 149 class SessionTest(unittest.TestCase): | 197 class SessionTest(unittest.TestCase): |
| 150 """Tests dealing with WebDriver sessions.""" | 198 """Tests dealing with WebDriver sessions.""" |
| 151 | 199 |
| 152 def setUp(self): | 200 def setUp(self): |
| 153 self._launcher = ChromeDriverLauncher() | 201 self._launcher = ChromeDriverLauncher() |
| 154 | 202 |
| 155 def tearDown(self): | 203 def tearDown(self): |
| 156 self._launcher.Kill() | 204 self._launcher.Kill() |
| 157 | 205 |
| 158 def testCreatingSessionShouldRedirectToCorrectURL(self): | 206 def testCreatingSessionShouldRedirectToCorrectURL(self): |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 self.assertEquals('', url_parts[0]) | 334 self.assertEquals('', url_parts[0]) |
| 287 self.assertEquals('wd', url_parts[1]) | 335 self.assertEquals('wd', url_parts[1]) |
| 288 self.assertEquals('hub', url_parts[2]) | 336 self.assertEquals('hub', url_parts[2]) |
| 289 self.assertEquals('session', url_parts[3]) | 337 self.assertEquals('session', url_parts[3]) |
| 290 self.assertEquals(data['sessionId'], url_parts[4]) | 338 self.assertEquals(data['sessionId'], url_parts[4]) |
| 291 | 339 |
| 292 | 340 |
| 293 if __name__ == '__main__': | 341 if __name__ == '__main__': |
| 294 unittest.main(module='chromedriver_tests', | 342 unittest.main(module='chromedriver_tests', |
| 295 testRunner=GTestTextTestRunner(verbosity=1)) | 343 testRunner=GTestTextTestRunner(verbosity=1)) |
| OLD | NEW |