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): | |
John Grabowski
2010/11/11 21:43:06
2 more tests:
1. timeout 0, load a page, wait for
| |
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 | |
84 class TestJavaScriptExecution(RemoteWebDriverTest): | 131 class TestJavaScriptExecution(RemoteWebDriverTest): |
85 """ Test the execute javascript ability of the remote driver""" | 132 """ Test the execute javascript ability of the remote driver""" |
86 def testNoModification(self): | 133 def testNoModification(self): |
87 self.driver.get("http://www.google.com") | 134 self.driver.get("http://www.google.com") |
88 title = self.driver.execute_script("return document.title") | 135 title = self.driver.execute_script("return document.title") |
89 self.assertEqual(title, self.driver.get_title()) | 136 self.assertEqual(title, self.driver.get_title()) |
90 | 137 |
91 def testModification(self): | 138 def testModification(self): |
92 self.driver.get("http://www.google.com") | 139 self.driver.get("http://www.google.com") |
93 title = self.driver.get_title() | 140 title = self.driver.get_title() |
(...skipping 19 matching lines...) Expand all Loading... | |
113 def testJavascriptWithNoReturn(self): | 160 def testJavascriptWithNoReturn(self): |
114 self.driver.get("http://www.google.com") | 161 self.driver.get("http://www.google.com") |
115 try: | 162 try: |
116 ret = self.driver.execute_script("return window.foobar") | 163 ret = self.driver.execute_script("return window.foobar") |
117 self.assertTrue(type(ret) is types.NoneType) | 164 self.assertTrue(type(ret) is types.NoneType) |
118 except: | 165 except: |
119 self.assertTrue(False) | 166 self.assertTrue(False) |
120 | 167 |
121 | 168 |
122 class TestNavigation(RemoteWebDriverTest): | 169 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): | 170 def testNavigateToURL(self): |
137 # No redirects are allowed on the google home page. | 171 # No redirects are allowed on the google home page. |
138 self.navigate(self.SEARCH) | 172 self.navigate(self.SEARCH) |
139 | 173 |
140 def testGoBackWithNoHistory(self): | 174 def testGoBackWithNoHistory(self): |
141 # Go back one page with nothing to go back to. | 175 # Go back one page with nothing to go back to. |
142 self.assertRaises(ErrorInResponseException, self.driver.back) | 176 self.assertRaises(ErrorInResponseException, self.driver.back) |
143 | 177 |
144 def testGoForwardWithNoHistory(self): | 178 def testGoForwardWithNoHistory(self): |
145 # Go forward with nothing to move forward to. | 179 # 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: | 247 if options.port: |
214 WEBDRIVER_PORT = options.port | 248 WEBDRIVER_PORT = options.port |
215 if options.exe: | 249 if options.exe: |
216 WEBDRIVER_EXE = options.exe | 250 WEBDRIVER_EXE = options.exe |
217 if not os.path.exists(WEBDRIVER_EXE): | 251 if not os.path.exists(WEBDRIVER_EXE): |
218 parser.error('WebDriver server executable not found:\n\t%s\n' | 252 parser.error('WebDriver server executable not found:\n\t%s\n' |
219 'Please specify a valid path with the --exe flag.' | 253 'Please specify a valid path with the --exe flag.' |
220 % WEBDRIVER_EXE) | 254 % WEBDRIVER_EXE) |
221 | 255 |
222 unittest.main() | 256 unittest.main() |
OLD | NEW |