Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 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 try: | 6 try: |
| 7 import json | 7 import json |
| 8 except ImportError: # < 2.6 | 8 except ImportError: # < 2.6 |
| 9 import simplejson as json | 9 import simplejson as json |
| 10 | 10 |
| 11 import os | 11 import os |
| 12 import optparse | 12 import optparse |
| 13 import platform | 13 import platform |
| 14 import string | 14 import string |
| 15 import subprocess | 15 import subprocess |
| 16 import sys | 16 import sys |
| 17 import time | 17 import time |
| 18 import types | 18 import types |
| 19 import unittest | 19 import unittest |
| 20 import urllib2 | 20 import urllib2 |
| 21 from selenium.common.exceptions import ErrorInResponseException | 21 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
| 22 from selenium.common.exceptions import NoSuchElementException | 22 from selenium.webdriver.common.exceptions import ErrorInResponseException |
| 23 from selenium.remote.webdriver import WebDriver | 23 from selenium.webdriver.common.exceptions import NoSuchElementException |
| 24 from selenium.remote.webdriver.webelement import WebElement | 24 from selenium.webdriver.remote.webdriver import WebDriver |
| 25 from selenium.webdriver.remote.webelement import WebElement | |
| 25 from urlparse import urlparse | 26 from urlparse import urlparse |
| 26 | 27 |
| 27 | 28 |
| 28 if sys.version_info < (2,6): | 29 if sys.version_info < (2,6): |
| 29 # Subprocess.Popen.kill is not available prior to 2.6. | 30 # Subprocess.Popen.kill is not available prior to 2.6. |
| 30 if platform.system() == 'Windows': | 31 if platform.system() == 'Windows': |
| 31 import win32api | 32 import win32api |
| 32 else: | 33 else: |
| 33 import signal | 34 import signal |
| 34 | 35 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 47 SEARCH = "http://www.google.com/webhp?hl=en" | 48 SEARCH = "http://www.google.com/webhp?hl=en" |
| 48 NEWS = "http://www.google.com/news?hl=en" | 49 NEWS = "http://www.google.com/news?hl=en" |
| 49 | 50 |
| 50 """Verifies that navigation to a specific page is correct by asserting on the | 51 """Verifies that navigation to a specific page is correct by asserting on the |
| 51 the reported URL. The function will not work with pages that redirect.""" | 52 the reported URL. The function will not work with pages that redirect.""" |
| 52 def navigate(self, url): | 53 def navigate(self, url): |
| 53 self.driver.get(url) | 54 self.driver.get(url) |
| 54 self.assertURL(url) | 55 self.assertURL(url) |
| 55 | 56 |
| 56 def assertURL(self, url): | 57 def assertURL(self, url): |
| 57 u = self.driver.get_current_url() | 58 u = self.driver.current_url |
| 58 self.assertEqual(u, url) | 59 self.assertEqual(u, url) |
| 59 | 60 |
| 60 """A new instance of chrome driver is started for every test case""" | 61 """A new instance of chrome driver is started for every test case""" |
| 61 def setUp(self): | 62 def setUp(self): |
| 62 global WEBDRIVER_SERVER_URL | 63 global WEBDRIVER_SERVER_URL |
| 63 global WEBDRIVER_PROCESS | 64 global WEBDRIVER_PROCESS |
| 64 WEBDRIVER_PROCESS = subprocess.Popen([WEBDRIVER_EXE, | 65 WEBDRIVER_PROCESS = subprocess.Popen([WEBDRIVER_EXE, |
| 65 '--port=%d' % WEBDRIVER_PORT]) | 66 '--port=%d' % WEBDRIVER_PORT]) |
| 66 if WEBDRIVER_PROCESS == None: | 67 if WEBDRIVER_PROCESS == None: |
| 67 print "Chromium executable not found. The path used was: " | 68 print "Chromium executable not found. The path used was: " |
| 68 print WEBDRIVER_EXE | 69 print WEBDRIVER_EXE |
| 69 sys.exit(-1) | 70 sys.exit(-1) |
| 70 | 71 |
| 71 time.sleep(5) | 72 time.sleep(5) |
| 72 self.driver = WebDriver.WebDriver(WEBDRIVER_SERVER_URL, "chrome", "ANY") | 73 self.driver = WebDriver(WEBDRIVER_SERVER_URL, {}) |
| 73 self.assertTrue(self.driver) | 74 self.assertTrue(self.driver) |
| 74 | 75 |
| 75 def tearDown(self): | 76 def tearDown(self): |
| 76 global WEBDRIVER_PROCESS | 77 global WEBDRIVER_PROCESS |
| 77 self.driver.quit() | 78 self.driver.quit() |
| 78 if WEBDRIVER_PROCESS: | 79 if WEBDRIVER_PROCESS: |
| 79 if sys.version_info < (2,6): | 80 if sys.version_info < (2,6): |
| 80 # From http://stackoverflow.com/questions/1064335 | 81 # From http://stackoverflow.com/questions/1064335 |
| 81 if platform.system() == 'Windows': | 82 if platform.system() == 'Windows': |
| 82 PROCESS_TERMINATE = 1 | 83 PROCESS_TERMINATE = 1 |
| 83 handle = win32api.OpenProcess(PROCESS_TERMINATE, False, | 84 handle = win32api.OpenProcess(PROCESS_TERMINATE, False, |
| 84 WEBDRIVER_PROCESS.pid) | 85 WEBDRIVER_PROCESS.pid) |
| 85 win32api.TerminateProcess(handle, -1) | 86 win32api.TerminateProcess(handle, -1) |
| 86 win32api.CloseHandle(handle) | 87 win32api.CloseHandle(handle) |
| 87 else: | 88 else: |
| 88 os.kill(WEBDRIVER_PROCESS.pid, signal.SIGKILL) | 89 os.kill(WEBDRIVER_PROCESS.pid, signal.SIGKILL) |
| 89 else: | 90 else: |
| 90 WEBDRIVER_PROCESS.kill() | 91 WEBDRIVER_PROCESS.kill() |
| 91 WEBDRIVER_PROCESS = None | 92 WEBDRIVER_PROCESS = None |
| 92 | 93 |
| 93 """Preforms a string search ignoring case""" | 94 """Preforms a string search ignoring case""" |
| 94 def assertFind(self, text, search): | 95 def assertFind(self, text, search): |
| 95 text = string.lower(text) | 96 text = string.lower(text) |
| 96 search = string.lower(search) | 97 search = string.lower(search) |
| 97 self.assertNotEqual(-1, string.find(text, search)) | 98 self.assertNotEqual(-1, string.find(text, search)) |
| 98 | 99 |
| 100 class RemoteWebdriverNativeEventsTest(RemoteWebDriverTest): | |
|
kkania
2011/03/10 00:28:58
Do not add to this file. I was planning on deletin
timothe faudot
2011/03/10 05:34:32
Where should I add those tests then?
| |
| 101 def setUp(self): | |
| 102 global WEBDRIVER_SERVER_URL | |
| 103 global WEBDRIVER_PROCESS | |
| 104 WEBDRIVER_PROCESS = subprocess.Popen([WEBDRIVER_EXE, | |
| 105 '--port=%d' % WEBDRIVER_PORT]) | |
| 106 if WEBDRIVER_PROCESS == None: | |
| 107 print "Chromium executable not found. The path used was: " | |
| 108 print WEBDRIVER_EXE | |
| 109 sys.exit(-1) | |
| 110 | |
| 111 time.sleep(5) | |
| 112 custom_cap = DesiredCapabilities.CHROME | |
| 113 custom_cap["chrome"] = {"nativeEvents" : True} | |
| 114 self.driver = WebDriver(WEBDRIVER_SERVER_URL, custom_cap) | |
| 115 self.assertTrue(self.driver) | |
| 116 | |
| 117 def testCanStartWithNativeEventsCapabilities(self): | |
| 118 self.assertTrue(self.driver.capabilities["chrome"]["nativeEvents"]) | |
| 119 | |
| 120 def testSendKeysNative(self): | |
| 121 self.navigate(SEARCH) | |
| 122 # Find the Google search Button. | |
| 123 q = self.driver.find_element_by_name("q") | |
| 124 # Send some keys. | |
| 125 q.send_keys("toukyou") | |
| 126 self.assertEqual(q.get_text(), "toukyou") | |
| 127 | |
| 128 def testSendKeysNativeIME(self): | |
| 129 self.navigate(SEARCH) | |
| 130 q = self.driver.find_element_by_name("q") | |
| 131 #TODO(timothe): Send the key combination that switches an JPN IME on/OFF. | |
| 132 #q.send_keys(Keys.F7) | |
| 133 q.send_keys("toukyou") | |
| 134 self.assertEqual(q.get_text(), "\xe6\x9d\xb1\xe4\xba\xac") | |
| 135 | |
| 99 | 136 |
| 100 class TestFindElement(RemoteWebDriverTest): | 137 class TestFindElement(RemoteWebDriverTest): |
| 101 def testFindByName(self): | 138 def testFindByName(self): |
| 102 self.navigate(SEARCH) | 139 self.navigate(SEARCH) |
| 103 # Find the Google search button. | 140 # Find the Google search button. |
| 104 q = self.driver.find_element_by_name("q") | 141 q = self.driver.find_element_by_name("q") |
| 105 self.assertTrue(isinstance(q, WebElement)) | 142 self.assertTrue(isinstance(q, WebElement)) |
| 106 # Trying looking for an element not on the page. | 143 # Trying looking for an element not on the page. |
| 107 self.assertRaises(NoSuchElementException, | 144 self.assertRaises(NoSuchElementException, |
| 108 self.driver.find_elment_by_name, "q2") | 145 self.driver.find_elment_by_name, "q2") |
| 109 # Try to find the Google search button using the multiple find method. | 146 # Try to find the Google search button using the multiple find method. |
| 110 q = self.driver.find_elements_by_name("q") | 147 q = self.driver.find_elements_by_name("q") |
| 111 self.assertTrue(isinstance(q, list)) | 148 self.assertTrue(isinstance(q, list)) |
| 112 self.assertTrue(len(q), 1) | 149 self.assertTrue(len(q), 1) |
| 113 self.assertTrue(isinstance(q[0], WebElement)) | 150 self.assertTrue(isinstance(q[0], WebElement)) |
| 114 # Try finding something not on page, with multiple find an empty array | 151 # Try finding something not on page, with multiple find an empty array |
| 115 # should return and no exception thrown. | 152 # should return and no exception thrown. |
| 116 q = self.driver.find_elements_by_name("q2") | 153 q = self.driver.find_elements_by_name("q2") |
| 117 assertTrue(q == []) | 154 assertTrue(q == []) |
| 118 # Find a hidden element on the page | 155 # Find a hidden element on the page |
| 119 q = self.driver.find_element_by_name("oq") | 156 q = self.driver.find_element_by_name("oq") |
| 120 self.assertTrue(isinstance(q, WebElement)) | 157 self.assertTrue(isinstance(q, WebElement)) |
| 121 | 158 |
| 122 def testFindElementById(self): | 159 def testFindElementById(self): |
| 123 self.navigate(SEARCH) | 160 self.navigate(SEARCH) |
| 124 # Find the padding for the logo near the search bar. | 161 # Find the padding for the logo near the search bar. |
| 125 elem = self.driver.find_element_by_id("logocont") | 162 elem = self.driver.find_element_by_id("logocont") |
| 126 self.assertTrue(isinstance(elem, WebElement)) | 163 self.assertTrue(isinstance(elem, WebElement)) |
| 127 # Look for an ID not there. | 164 # Look for an ID not there. |
| 128 self.assertRaises(NoSuchElementException, | 165 self.assertRaises(NoSuchElementException, |
| 129 self.driver.find_element_by_id, "logocont") | 166 self.driver.find_element_by_id, "logocont") |
| 130 | 167 |
| 131 def testFindElementById0WithTimeout(self): | 168 def testFindElementById0WithTimeout(self): |
| 132 self.set_implicit_wait(0) | 169 self.set_implicit_wait(0) |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 156 self.assertTrue(len(q), 1) | 193 self.assertTrue(len(q), 1) |
| 157 self.assertTrue(isinstance(q[0], WebElement)) | 194 self.assertTrue(isinstance(q[0], WebElement)) |
| 158 # Try finding something not on page, with multiple find an empty array | 195 # Try finding something not on page, with multiple find an empty array |
| 159 # should return and no exception thrown. | 196 # should return and no exception thrown. |
| 160 q = self.driver.find_elements_by_name("q2") | 197 q = self.driver.find_elements_by_name("q2") |
| 161 assertTrue(q == []) | 198 assertTrue(q == []) |
| 162 # Find a hidden element on the page | 199 # Find a hidden element on the page |
| 163 q = self.driver.find_element_by_name("oq") | 200 q = self.driver.find_element_by_name("oq") |
| 164 self.assertTrue(isinstance(q, WebElement)) | 201 self.assertTrue(isinstance(q, WebElement)) |
| 165 | 202 |
| 166 def testFindElementById(self): | 203 def testFindElementById(self): |
| 167 self.navigate(SEARCH) | 204 self.navigate(SEARCH) |
| 168 # Find the padding for the logo near the search bar. | 205 # Find the padding for the logo near the search bar. |
| 169 elem = self.driver.find_element_by_id("logocont") | 206 elem = self.driver.find_element_by_id("logocont") |
| 170 self.assertTrue(isinstance(elem, WebElement)) | 207 self.assertTrue(isinstance(elem, WebElement)) |
| 171 # Look for an ID not there. | 208 # Look for an ID not there. |
| 172 self.assertRaises(NoSuchElementException, | 209 self.assertRaises(NoSuchElementException, |
| 173 self.driver.find_element_by_id, "logocont") | 210 self.driver.find_element_by_id, "logocont") |
| 174 | 211 |
| 175 | 212 |
| 176 class TestJavaScriptExecution(RemoteWebDriverTest): | 213 class TestJavaScriptExecution(RemoteWebDriverTest): |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 help=('Specifies the URL of a remote WebDriver server to ' | 306 help=('Specifies the URL of a remote WebDriver server to ' |
| 270 'test against. If not specified, a server will be ' | 307 'test against. If not specified, a server will be ' |
| 271 'started on localhost according to the --exe and ' | 308 'started on localhost according to the --exe and ' |
| 272 '--port flags')) | 309 '--port flags')) |
| 273 parser.add_option('-e', '--exe', dest='exe', action='store', | 310 parser.add_option('-e', '--exe', dest='exe', action='store', |
| 274 type='string', default=None, | 311 type='string', default=None, |
| 275 help=('Path to the WebDriver server executable that should ' | 312 help=('Path to the WebDriver server executable that should ' |
| 276 'be started for testing; This flag is ignored if ' | 313 'be started for testing; This flag is ignored if ' |
| 277 '--url is provided for a remote server.')) | 314 '--url is provided for a remote server.')) |
| 278 parser.add_option('-p', '--port', dest='port', action='store', | 315 parser.add_option('-p', '--port', dest='port', action='store', |
| 279 type='int', default=8080, | 316 type='int', default=WEBDRIVER_PORT, |
| 280 help=('The port to start the WebDriver server executable ' | 317 help=('The port to start the WebDriver server executable ' |
| 281 'on; This flag is ignored if --url is provided for a ' | 318 'on; This flag is ignored if --url is provided for a ' |
| 282 'remote server.')) | 319 'remote server.')) |
| 283 | 320 |
| 284 (options, args) = parser.parse_args() | 321 (options, args) = parser.parse_args() |
| 285 # Strip out our flags so unittest.main() correct parses the remaining. | 322 # Strip out our flags so unittest.main() correct parses the remaining. |
| 286 sys.argv = sys.argv[:1] | 323 sys.argv = sys.argv[:1] |
| 287 sys.argv.extend(args) | 324 sys.argv.extend(args) |
| 288 | 325 |
| 289 if options.url: | 326 if options.url: |
| 290 WEBDRIVER_SERVER_URL = options.url | 327 WEBDRIVER_SERVER_URL = options.url |
| 291 else: | 328 else: |
| 292 if options.port: | 329 if options.port: |
| 293 WEBDRIVER_PORT = options.port | 330 WEBDRIVER_PORT = options.port |
| 294 if options.exe: | 331 if options.exe: |
| 295 WEBDRIVER_EXE = options.exe | 332 WEBDRIVER_EXE = options.exe |
| 296 if not os.path.exists(WEBDRIVER_EXE): | 333 if not os.path.exists(WEBDRIVER_EXE): |
| 297 parser.error('WebDriver server executable not found:\n\t%s\n' | 334 parser.error('WebDriver server executable not found:\n\t%s\n' |
| 298 'Please specify a valid path with the --exe flag.' | 335 'Please specify a valid path with the --exe flag.' |
| 299 % WEBDRIVER_EXE) | 336 % WEBDRIVER_EXE) |
| 300 | 337 |
| 301 unittest.main() | 338 unittest.main() |
| OLD | NEW |