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 |
| 22 from selenium.common.exceptions import NoSuchElementException |
21 from selenium.remote.webdriver import WebDriver | 23 from selenium.remote.webdriver import WebDriver |
22 from selenium.common.exceptions import ErrorInResponseException | 24 from selenium.remote.webdriver.webelement import WebElement |
23 from urlparse import urlparse | 25 from urlparse import urlparse |
24 | 26 |
25 | 27 |
26 if sys.version_info < (2,6): | 28 if sys.version_info < (2,6): |
27 # Subprocess.Popen.kill is not available prior to 2.6. | 29 # Subprocess.Popen.kill is not available prior to 2.6. |
28 if platform.system() == 'Windows': | 30 if platform.system() == 'Windows': |
29 import win32api | 31 import win32api |
30 else: | 32 else: |
31 import signal | 33 import signal |
32 | 34 |
33 | 35 |
34 WEBDRIVER_EXE = os.path.abspath(os.path.join('.', 'chromedriver')) | 36 WEBDRIVER_EXE = os.path.abspath(os.path.join('.', 'chromedriver')) |
35 if platform.system() == 'Windows': | 37 if platform.system() == 'Windows': |
36 WEBDRIVER_EXE = '%s.exe' % WEBDRIVER_EXE | 38 WEBDRIVER_EXE = '%s.exe' % WEBDRIVER_EXE |
37 WEBDRIVER_PORT = 8080 | 39 WEBDRIVER_PORT = 8080 |
38 WEBDRIVER_SERVER_URL = None | 40 WEBDRIVER_SERVER_URL = None |
39 WEBDRIVER_PROCESS = None | 41 WEBDRIVER_PROCESS = None |
40 | 42 |
41 if not WEBDRIVER_SERVER_URL: | 43 if not WEBDRIVER_SERVER_URL: |
42 WEBDRIVER_SERVER_URL = 'http://localhost:%d' % WEBDRIVER_PORT | 44 WEBDRIVER_SERVER_URL = 'http://localhost:%d' % WEBDRIVER_PORT |
43 | 45 |
44 class RemoteWebDriverTest(unittest.TestCase): | 46 class RemoteWebDriverTest(unittest.TestCase): |
| 47 SEARCH = "http://www.google.com/webhp?hl=en" |
| 48 NEWS = "http://www.google.com/news?hl=en" |
| 49 |
| 50 """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 def navigate(self, url): |
| 53 self.driver.get(url) |
| 54 self.assertURL(url) |
| 55 |
| 56 def assertURL(self, url): |
| 57 u = self.driver.get_current_url() |
| 58 self.assertEqual(u, url) |
| 59 |
45 """A new instance of chrome driver is started for every test case""" | 60 """A new instance of chrome driver is started for every test case""" |
46 def setUp(self): | 61 def setUp(self): |
47 global WEBDRIVER_SERVER_URL | 62 global WEBDRIVER_SERVER_URL |
48 global WEBDRIVER_PROCESS | 63 global WEBDRIVER_PROCESS |
49 WEBDRIVER_PROCESS = subprocess.Popen([WEBDRIVER_EXE, | 64 WEBDRIVER_PROCESS = subprocess.Popen([WEBDRIVER_EXE, |
50 '--port=%d' % WEBDRIVER_PORT]) | 65 '--port=%d' % WEBDRIVER_PORT]) |
51 if WEBDRIVER_PROCESS == None: | 66 if WEBDRIVER_PROCESS == None: |
52 print "Chromium executable not found. The path used was: " | 67 print "Chromium executable not found. The path used was: " |
53 print WEBDRIVER_EXE | 68 print WEBDRIVER_EXE |
54 sys.exit(-1) | 69 sys.exit(-1) |
(...skipping 19 matching lines...) Expand all Loading... |
74 else: | 89 else: |
75 WEBDRIVER_PROCESS.kill() | 90 WEBDRIVER_PROCESS.kill() |
76 WEBDRIVER_PROCESS = None | 91 WEBDRIVER_PROCESS = None |
77 | 92 |
78 """Preforms a string search ignoring case""" | 93 """Preforms a string search ignoring case""" |
79 def assertFind(self, text, search): | 94 def assertFind(self, text, search): |
80 text = string.lower(text) | 95 text = string.lower(text) |
81 search = string.lower(search) | 96 search = string.lower(search) |
82 self.assertNotEqual(-1, string.find(text, search)) | 97 self.assertNotEqual(-1, string.find(text, search)) |
83 | 98 |
| 99 class TestFindElement(RemoteWebDriverTest): |
| 100 def testFindByName(self): |
| 101 navigate(SEARCH) |
| 102 # Find the Google search button. |
| 103 q = self.driver.find_element_by_name("q") |
| 104 self.assertTrue(isinstance(q, WebElement)) |
| 105 # Trying looking for an element not on the page. |
| 106 self.assertRaises(NoSuchElementException, |
| 107 self.driver.find_elment_by_name, "q2") |
| 108 # Try to find the Google search button using the multiple find method. |
| 109 q = self.driver.find_elements_by_name("q") |
| 110 self.assertTrue(isinstance(q, list)) |
| 111 self.assertTrue(len(q), 1) |
| 112 self.assertTrue(isinstance(q[0], WebElement)) |
| 113 # Try finding something not on page, with multiple find an empty array |
| 114 # should return and no exception thrown. |
| 115 q = self.driver.find_elements_by_name("q2") |
| 116 assertTrue(q == []) |
| 117 # Find a hidden element on the page |
| 118 q = self.driver.find_element_by_name("oq") |
| 119 self.assertTrue(isinstance(q, WebElement)) |
| 120 |
| 121 def testFindElementById(self): |
| 122 navigate(SEARCH) |
| 123 # Find the padding for the logo near the search bar. |
| 124 elem = self.driver.find_element_by_id("logocont") |
| 125 self.assertTrue(isinstance(elem, WebElement)) |
| 126 # Look for an ID not there. |
| 127 self.assertRaises(NoSuchElementException, |
| 128 self.driver.find_element_by_id, "logocont") |
| 129 |
| 130 def testFindElementById0WithTimeout(self): |
| 131 self.set_implicit_wait(0) |
| 132 navigate(SEARCH) |
| 133 # Find the padding for the logo near the search bar. |
| 134 elem = self.driver.find_element_by_id("logocont") |
| 135 self.assertTrue(isinstance(elem, WebElement)) |
| 136 self.set_implicit_wait(5000) |
| 137 navigate(SEARCH) |
| 138 # Look for an ID not there. |
| 139 self.assertRaises(NoSuchElementException, |
| 140 self.driver.find_element_by_id, "logocont") |
| 141 |
| 142 |
84 class TestJavaScriptExecution(RemoteWebDriverTest): | 143 class TestJavaScriptExecution(RemoteWebDriverTest): |
85 """ Test the execute javascript ability of the remote driver""" | 144 """ Test the execute javascript ability of the remote driver""" |
86 def testNoModification(self): | 145 def testNoModification(self): |
87 self.driver.get("http://www.google.com") | 146 self.driver.get("http://www.google.com") |
88 title = self.driver.execute_script("return document.title") | 147 title = self.driver.execute_script("return document.title") |
89 self.assertEqual(title, self.driver.get_title()) | 148 self.assertEqual(title, self.driver.get_title()) |
90 | 149 |
91 def testModification(self): | 150 def testModification(self): |
92 self.driver.get("http://www.google.com") | 151 self.driver.get("http://www.google.com") |
93 title = self.driver.get_title() | 152 title = self.driver.get_title() |
(...skipping 19 matching lines...) Expand all Loading... |
113 def testJavascriptWithNoReturn(self): | 172 def testJavascriptWithNoReturn(self): |
114 self.driver.get("http://www.google.com") | 173 self.driver.get("http://www.google.com") |
115 try: | 174 try: |
116 ret = self.driver.execute_script("return window.foobar") | 175 ret = self.driver.execute_script("return window.foobar") |
117 self.assertTrue(type(ret) is types.NoneType) | 176 self.assertTrue(type(ret) is types.NoneType) |
118 except: | 177 except: |
119 self.assertTrue(False) | 178 self.assertTrue(False) |
120 | 179 |
121 | 180 |
122 class TestNavigation(RemoteWebDriverTest): | 181 class TestNavigation(RemoteWebDriverTest): |
123 SEARCH = "http://www.google.com/webhp?hl=en" | |
124 NEWS = "http://www.google.com/news?hl=en" | |
125 | |
126 """Verifies that navigation to a specific page is correct by asserting on the | |
127 the reported URL. The function will not work with pages that redirect.""" | |
128 def navigate(self, url): | |
129 self.driver.get(url) | |
130 self.assertURL(url) | |
131 | |
132 def assertURL(self, url): | |
133 u = self.driver.get_current_url() | |
134 self.assertEqual(u, url) | |
135 | |
136 def testNavigateToURL(self): | 182 def testNavigateToURL(self): |
137 # No redirects are allowed on the google home page. | 183 # No redirects are allowed on the google home page. |
138 self.navigate(self.SEARCH) | 184 self.navigate(self.SEARCH) |
139 | 185 |
140 def testGoBackWithNoHistory(self): | 186 def testGoBackWithNoHistory(self): |
141 # Go back one page with nothing to go back to. | 187 # Go back one page with nothing to go back to. |
142 self.assertRaises(ErrorInResponseException, self.driver.back) | 188 self.assertRaises(ErrorInResponseException, self.driver.back) |
143 | 189 |
144 def testGoForwardWithNoHistory(self): | 190 def testGoForwardWithNoHistory(self): |
145 # Go forward with nothing to move forward to. | 191 # Go forward with nothing to move forward to. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 if options.port: | 259 if options.port: |
214 WEBDRIVER_PORT = options.port | 260 WEBDRIVER_PORT = options.port |
215 if options.exe: | 261 if options.exe: |
216 WEBDRIVER_EXE = options.exe | 262 WEBDRIVER_EXE = options.exe |
217 if not os.path.exists(WEBDRIVER_EXE): | 263 if not os.path.exists(WEBDRIVER_EXE): |
218 parser.error('WebDriver server executable not found:\n\t%s\n' | 264 parser.error('WebDriver server executable not found:\n\t%s\n' |
219 'Please specify a valid path with the --exe flag.' | 265 'Please specify a valid path with the --exe flag.' |
220 % WEBDRIVER_EXE) | 266 % WEBDRIVER_EXE) |
221 | 267 |
222 unittest.main() | 268 unittest.main() |
OLD | NEW |