| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import ctypes | 5 import ctypes |
| 6 import json | 6 import json |
| 7 | 7 |
| 8 from webelement import WebElement | 8 from webelement import WebElement |
| 9 | 9 |
| 10 class ChromeDriverException(Exception): | 10 class ChromeDriverException(Exception): |
| 11 pass | 11 pass |
| 12 class NoSuchElement(ChromeDriverException): | 12 class NoSuchElement(ChromeDriverException): |
| 13 pass | 13 pass |
| 14 class UnknownCommand(ChromeDriverException): | 14 class UnknownCommand(ChromeDriverException): |
| 15 pass | 15 pass |
| 16 class StaleElementReference(ChromeDriverException): | 16 class StaleElementReference(ChromeDriverException): |
| 17 pass | 17 pass |
| 18 class UnknownError(ChromeDriverException): | 18 class UnknownError(ChromeDriverException): |
| 19 pass | 19 pass |
| 20 class JavaScriptError(ChromeDriverException): |
| 21 pass |
| 20 class XPathLookupError(ChromeDriverException): | 22 class XPathLookupError(ChromeDriverException): |
| 21 pass | 23 pass |
| 22 class NoSuchWindow(ChromeDriverException): | 24 class NoSuchWindow(ChromeDriverException): |
| 23 pass | 25 pass |
| 24 class InvalidCookieDomain(ChromeDriverException): | 26 class InvalidCookieDomain(ChromeDriverException): |
| 25 pass | 27 pass |
| 28 class ScriptTimeout(ChromeDriverException): |
| 29 pass |
| 26 class InvalidSelector(ChromeDriverException): | 30 class InvalidSelector(ChromeDriverException): |
| 27 pass | 31 pass |
| 28 class SessionNotCreatedException(ChromeDriverException): | 32 class SessionNotCreatedException(ChromeDriverException): |
| 29 pass | 33 pass |
| 30 class NoSuchSession(ChromeDriverException): | 34 class NoSuchSession(ChromeDriverException): |
| 31 pass | 35 pass |
| 32 | 36 |
| 33 def _ExceptionForResponse(response): | 37 def _ExceptionForResponse(response): |
| 34 exception_class_map = { | 38 exception_class_map = { |
| 35 7: NoSuchElement, | 39 7: NoSuchElement, |
| 36 9: UnknownCommand, | 40 9: UnknownCommand, |
| 37 10: StaleElementReference, | 41 10: StaleElementReference, |
| 38 13: UnknownError, | 42 13: UnknownError, |
| 43 17: JavaScriptError, |
| 39 19: XPathLookupError, | 44 19: XPathLookupError, |
| 40 23: NoSuchWindow, | 45 23: NoSuchWindow, |
| 41 24: InvalidCookieDomain, | 46 24: InvalidCookieDomain, |
| 47 28: ScriptTimeout, |
| 42 32: InvalidSelector, | 48 32: InvalidSelector, |
| 43 33: SessionNotCreatedException, | 49 33: SessionNotCreatedException, |
| 44 100: NoSuchSession | 50 100: NoSuchSession |
| 45 } | 51 } |
| 46 status = response['status'] | 52 status = response['status'] |
| 47 msg = response['value']['message'] | 53 msg = response['value']['message'] |
| 48 return exception_class_map.get(status, ChromeDriverException)(msg) | 54 return exception_class_map.get(status, ChromeDriverException)(msg) |
| 49 | 55 |
| 50 class ChromeDriver(object): | 56 class ChromeDriver(object): |
| 51 """Starts and controls a single Chrome instance on this machine.""" | 57 """Starts and controls a single Chrome instance on this machine.""" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 self.ExecuteSessionCommand('close') | 154 self.ExecuteSessionCommand('close') |
| 149 | 155 |
| 150 def Load(self, url): | 156 def Load(self, url): |
| 151 self.ExecuteSessionCommand('get', {'url': url}) | 157 self.ExecuteSessionCommand('get', {'url': url}) |
| 152 | 158 |
| 153 def ExecuteScript(self, script, *args): | 159 def ExecuteScript(self, script, *args): |
| 154 converted_args = list(args) | 160 converted_args = list(args) |
| 155 return self.ExecuteSessionCommand( | 161 return self.ExecuteSessionCommand( |
| 156 'executeScript', {'script': script, 'args': converted_args}) | 162 'executeScript', {'script': script, 'args': converted_args}) |
| 157 | 163 |
| 164 def ExecuteAsyncScript(self, script, *args): |
| 165 converted_args = list(args) |
| 166 return self.ExecuteSessionCommand( |
| 167 'executeAsyncScript', {'script': script, 'args': converted_args}) |
| 168 |
| 158 def SwitchToFrame(self, id_or_name): | 169 def SwitchToFrame(self, id_or_name): |
| 159 self.ExecuteSessionCommand('switchToFrame', {'id': id_or_name}) | 170 self.ExecuteSessionCommand('switchToFrame', {'id': id_or_name}) |
| 160 | 171 |
| 161 def SwitchToFrameByIndex(self, index): | 172 def SwitchToFrameByIndex(self, index): |
| 162 self.SwitchToFrame(index) | 173 self.SwitchToFrame(index) |
| 163 | 174 |
| 164 def SwitchToMainFrame(self): | 175 def SwitchToMainFrame(self): |
| 165 self.SwitchToFrame(None) | 176 self.SwitchToFrame(None) |
| 166 | 177 |
| 167 def GetTitle(self): | 178 def GetTitle(self): |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 self.ExecuteSessionCommand('setAlertValue', {'text': prompt_text}) | 250 self.ExecuteSessionCommand('setAlertValue', {'text': prompt_text}) |
| 240 if accept: | 251 if accept: |
| 241 cmd = 'acceptAlert' | 252 cmd = 'acceptAlert' |
| 242 else: | 253 else: |
| 243 cmd = 'dismissAlert' | 254 cmd = 'dismissAlert' |
| 244 self.ExecuteSessionCommand(cmd) | 255 self.ExecuteSessionCommand(cmd) |
| 245 | 256 |
| 246 def Quit(self): | 257 def Quit(self): |
| 247 """Quits the browser and ends the session.""" | 258 """Quits the browser and ends the session.""" |
| 248 self.ExecuteSessionCommand('quit') | 259 self.ExecuteSessionCommand('quit') |
| OLD | NEW |