| 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 |
| 24 class Timeout(ChromeDriverException): |
| 25 pass |
| 22 class NoSuchWindow(ChromeDriverException): | 26 class NoSuchWindow(ChromeDriverException): |
| 23 pass | 27 pass |
| 24 class InvalidSelector(ChromeDriverException): | 28 class InvalidSelector(ChromeDriverException): |
| 25 pass | 29 pass |
| 26 class SessionNotCreatedException(ChromeDriverException): | 30 class SessionNotCreatedException(ChromeDriverException): |
| 27 pass | 31 pass |
| 28 class NoSuchSession(ChromeDriverException): | 32 class NoSuchSession(ChromeDriverException): |
| 29 pass | 33 pass |
| 30 | 34 |
| 31 def _ExceptionForResponse(response): | 35 def _ExceptionForResponse(response): |
| 32 exception_class_map = { | 36 exception_class_map = { |
| 33 7: NoSuchElement, | 37 7: NoSuchElement, |
| 34 9: UnknownCommand, | 38 9: UnknownCommand, |
| 35 10: StaleElementReference, | 39 10: StaleElementReference, |
| 36 13: UnknownError, | 40 13: UnknownError, |
| 41 17: JavaScriptError, |
| 37 19: XPathLookupError, | 42 19: XPathLookupError, |
| 43 21: Timeout, |
| 38 23: NoSuchWindow, | 44 23: NoSuchWindow, |
| 39 32: InvalidSelector, | 45 32: InvalidSelector, |
| 40 33: SessionNotCreatedException, | 46 33: SessionNotCreatedException, |
| 41 100: NoSuchSession | 47 100: NoSuchSession |
| 42 } | 48 } |
| 43 status = response['status'] | 49 status = response['status'] |
| 44 msg = response['value']['message'] | 50 msg = response['value']['message'] |
| 45 return exception_class_map.get(status, ChromeDriverException)(msg) | 51 return exception_class_map.get(status, ChromeDriverException)(msg) |
| 46 | 52 |
| 47 class ChromeDriver(object): | 53 class ChromeDriver(object): |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 self.ExecuteSessionCommand('close') | 151 self.ExecuteSessionCommand('close') |
| 146 | 152 |
| 147 def Load(self, url): | 153 def Load(self, url): |
| 148 self.ExecuteSessionCommand('get', {'url': url}) | 154 self.ExecuteSessionCommand('get', {'url': url}) |
| 149 | 155 |
| 150 def ExecuteScript(self, script, *args): | 156 def ExecuteScript(self, script, *args): |
| 151 converted_args = list(args) | 157 converted_args = list(args) |
| 152 return self.ExecuteSessionCommand( | 158 return self.ExecuteSessionCommand( |
| 153 'executeScript', {'script': script, 'args': converted_args}) | 159 'executeScript', {'script': script, 'args': converted_args}) |
| 154 | 160 |
| 161 def ExecuteAsyncScript(self, script, *args): |
| 162 converted_args = list(args) |
| 163 return self.ExecuteSessionCommand( |
| 164 'executeAsyncScript', {'script': script, 'args': converted_args}) |
| 165 |
| 155 def SwitchToFrame(self, id_or_name): | 166 def SwitchToFrame(self, id_or_name): |
| 156 self.ExecuteSessionCommand('switchToFrame', {'id': id_or_name}) | 167 self.ExecuteSessionCommand('switchToFrame', {'id': id_or_name}) |
| 157 | 168 |
| 158 def SwitchToFrameByIndex(self, index): | 169 def SwitchToFrameByIndex(self, index): |
| 159 self.SwitchToFrame(index) | 170 self.SwitchToFrame(index) |
| 160 | 171 |
| 161 def SwitchToMainFrame(self): | 172 def SwitchToMainFrame(self): |
| 162 self.SwitchToFrame(None) | 173 self.SwitchToFrame(None) |
| 163 | 174 |
| 164 def GetTitle(self): | 175 def GetTitle(self): |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 self.ExecuteSessionCommand('setAlertValue', {'text': prompt_text}) | 235 self.ExecuteSessionCommand('setAlertValue', {'text': prompt_text}) |
| 225 if accept: | 236 if accept: |
| 226 cmd = 'acceptAlert' | 237 cmd = 'acceptAlert' |
| 227 else: | 238 else: |
| 228 cmd = 'dismissAlert' | 239 cmd = 'dismissAlert' |
| 229 self.ExecuteSessionCommand(cmd) | 240 self.ExecuteSessionCommand(cmd) |
| 230 | 241 |
| 231 def Quit(self): | 242 def Quit(self): |
| 232 """Quits the browser and ends the session.""" | 243 """Quits the browser and ends the session.""" |
| 233 self.ExecuteSessionCommand('quit') | 244 self.ExecuteSessionCommand('quit') |
| OLD | NEW |