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 class ChromeDriverException(Exception): | 8 class ChromeDriverException(Exception): |
9 pass | 9 pass |
10 class UnknownCommand(ChromeDriverException): | 10 class UnknownCommand(ChromeDriverException): |
(...skipping 12 matching lines...) Expand all Loading... |
23 33: SessionNotCreatedException, | 23 33: SessionNotCreatedException, |
24 100: NoSuchSession | 24 100: NoSuchSession |
25 } | 25 } |
26 status = response['status'] | 26 status = response['status'] |
27 msg = response['value']['message'] | 27 msg = response['value']['message'] |
28 return exception_class_map.get(status, ChromeDriverException)(msg) | 28 return exception_class_map.get(status, ChromeDriverException)(msg) |
29 | 29 |
30 class ChromeDriver(object): | 30 class ChromeDriver(object): |
31 """Starts and controls a single Chrome instance on this machine.""" | 31 """Starts and controls a single Chrome instance on this machine.""" |
32 | 32 |
33 def __init__(self, lib_path): | 33 def __init__(self, lib_path, chrome_binary=None): |
34 self._lib = ctypes.CDLL(lib_path) | 34 self._lib = ctypes.CDLL(lib_path) |
35 self._session_id = self._ExecuteCommand('newSession') | 35 if chrome_binary is None: |
| 36 params = {} |
| 37 else: |
| 38 params = { |
| 39 'desiredCapabilities': { |
| 40 'chrome': { |
| 41 'binary': chrome_binary |
| 42 } |
| 43 } |
| 44 } |
| 45 self._session_id = self._ExecuteCommand('newSession', params) |
36 | 46 |
37 def _ExecuteCommand(self, name, params={}, session_id=''): | 47 def _ExecuteCommand(self, name, params={}, session_id=''): |
38 cmd = { | 48 cmd = { |
39 'name': name, | 49 'name': name, |
40 'parameters': params, | 50 'parameters': params, |
41 'sessionId': session_id | 51 'sessionId': session_id |
42 } | 52 } |
43 cmd_json = json.dumps(cmd) | 53 cmd_json = json.dumps(cmd) |
44 response_data = ctypes.c_char_p() | 54 response_data = ctypes.c_char_p() |
45 response_size = ctypes.c_uint() | 55 response_size = ctypes.c_uint() |
46 self._lib.ExecuteCommand( | 56 self._lib.ExecuteCommand( |
47 ctypes.c_char_p(cmd_json), | 57 ctypes.c_char_p(cmd_json), |
48 ctypes.c_uint(len(cmd_json)), | 58 ctypes.c_uint(len(cmd_json)), |
49 ctypes.byref(response_data), | 59 ctypes.byref(response_data), |
50 ctypes.byref(response_size)) | 60 ctypes.byref(response_size)) |
51 response_json = ctypes.string_at(response_data, response_size.value) | 61 response_json = ctypes.string_at(response_data, response_size.value) |
52 self._lib.Free(response_data) | 62 self._lib.Free(response_data) |
53 response = json.loads(response_json) | 63 response = json.loads(response_json) |
54 if response['status'] != 0: | 64 if response['status'] != 0: |
55 raise _ExceptionForResponse(response) | 65 raise _ExceptionForResponse(response) |
56 return response['value'] | 66 return response['value'] |
57 | 67 |
58 def _ExecuteSessionCommand(self, name, params={}): | 68 def _ExecuteSessionCommand(self, name, params={}): |
59 return self._ExecuteCommand(name, params, self._session_id) | 69 return self._ExecuteCommand(name, params, self._session_id) |
60 | 70 |
61 def Quit(self): | 71 def Quit(self): |
62 """Quits the browser and ends the session.""" | 72 """Quits the browser and ends the session.""" |
63 self._ExecuteSessionCommand('quit') | 73 self._ExecuteSessionCommand('quit') |
OLD | NEW |