Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / test / chromedriver / client / command_executor.py
blob4b5988122c41eb4bbce172b44a4f7d9fd942e17b
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import httplib
6 import json
9 class _Method(object):
10 GET = 'GET'
11 POST = 'POST'
12 DELETE = 'DELETE'
15 class Command(object):
16 NEW_SESSION = (_Method.POST, '/session')
17 GET_SESSION_CAPABILITIES = (_Method.GET, '/session/:sessionId')
18 GET_SESSIONS = (_Method.GET, '/sessions')
19 QUIT = (_Method.DELETE, '/session/:sessionId')
20 GET_CURRENT_WINDOW_HANDLE = (_Method.GET, '/session/:sessionId/window_handle')
21 GET_WINDOW_HANDLES = (_Method.GET, '/session/:sessionId/window_handles')
22 GET = (_Method.POST, '/session/:sessionId/url')
23 GET_ALERT = (_Method.GET, '/session/:sessionId/alert')
24 DISMISS_ALERT = (_Method.POST, '/session/:sessionId/dismiss_alert')
25 ACCEPT_ALERT = (_Method.POST, '/session/:sessionId/accept_alert')
26 GET_ALERT_TEXT = (_Method.GET, '/session/:sessionId/alert_text')
27 SET_ALERT_VALUE = (_Method.POST, '/session/:sessionId/alert_text')
28 GO_FORWARD = (_Method.POST, '/session/:sessionId/forward')
29 GO_BACK = (_Method.POST, '/session/:sessionId/back')
30 REFRESH = (_Method.POST, '/session/:sessionId/refresh')
31 EXECUTE_SCRIPT = (_Method.POST, '/session/:sessionId/execute')
32 EXECUTE_ASYNC_SCRIPT = (_Method.POST, '/session/:sessionId/execute_async')
33 LAUNCH_APP = (_Method.POST, '/session/:sessionId/chromium/launch_app')
34 GET_CURRENT_URL = (_Method.GET, '/session/:sessionId/url')
35 GET_TITLE = (_Method.GET, '/session/:sessionId/title')
36 GET_PAGE_SOURCE = (_Method.GET, '/session/:sessionId/source')
37 SCREENSHOT = (_Method.GET, '/session/:sessionId/screenshot')
38 SET_BROWSER_VISIBLE = (_Method.POST, '/session/:sessionId/visible')
39 IS_BROWSER_VISIBLE = (_Method.GET, '/session/:sessionId/visible')
40 FIND_ELEMENT = (_Method.POST, '/session/:sessionId/element')
41 FIND_ELEMENTS = (_Method.POST, '/session/:sessionId/elements')
42 GET_ACTIVE_ELEMENT = (_Method.POST, '/session/:sessionId/element/active')
43 FIND_CHILD_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/element')
44 FIND_CHILD_ELEMENTS = (
45 _Method.POST, '/session/:sessionId/element/:id/elements')
46 CLICK_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/click')
47 CLEAR_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/clear')
48 SUBMIT_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/submit')
49 GET_ELEMENT_TEXT = (_Method.GET, '/session/:sessionId/element/:id/text')
50 SEND_KEYS_TO_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/value')
51 UPLOAD_FILE = (_Method.POST, '/session/:sessionId/file')
52 GET_ELEMENT_VALUE = (_Method.GET, '/session/:sessionId/element/:id/value')
53 GET_ELEMENT_TAG_NAME = (_Method.GET, '/session/:sessionId/element/:id/name')
54 IS_ELEMENT_SELECTED = (
55 _Method.GET, '/session/:sessionId/element/:id/selected')
56 IS_ELEMENT_ENABLED = (_Method.GET, '/session/:sessionId/element/:id/enabled')
57 IS_ELEMENT_DISPLAYED = (
58 _Method.GET, '/session/:sessionId/element/:id/displayed')
59 HOVER_OVER_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/hover')
60 GET_ELEMENT_LOCATION = (
61 _Method.GET, '/session/:sessionId/element/:id/location')
62 GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW = (
63 _Method.GET, '/session/:sessionId/element/:id/location_in_view')
64 GET_ELEMENT_SIZE = (_Method.GET, '/session/:sessionId/element/:id/size')
65 GET_ELEMENT_ATTRIBUTE = (
66 _Method.GET, '/session/:sessionId/element/:id/attribute/:name')
67 ELEMENT_EQUALS = (
68 _Method.GET, '/session/:sessionId/element/:id/equals/:other')
69 GET_COOKIES = (_Method.GET, '/session/:sessionId/cookie')
70 ADD_COOKIE = (_Method.POST, '/session/:sessionId/cookie')
71 DELETE_ALL_COOKIES = (_Method.DELETE, '/session/:sessionId/cookie')
72 DELETE_COOKIE = (_Method.DELETE, '/session/:sessionId/cookie/:name')
73 SWITCH_TO_FRAME = (_Method.POST, '/session/:sessionId/frame')
74 SWITCH_TO_PARENT_FRAME = (_Method.POST, '/session/:sessionId/frame/parent')
75 SWITCH_TO_WINDOW = (_Method.POST, '/session/:sessionId/window')
76 GET_WINDOW_SIZE = (
77 _Method.GET, '/session/:sessionId/window/:windowHandle/size')
78 GET_WINDOW_POSITION = (
79 _Method.GET, '/session/:sessionId/window/:windowHandle/position')
80 SET_WINDOW_SIZE = (
81 _Method.POST, '/session/:sessionId/window/:windowHandle/size')
82 SET_WINDOW_POSITION = (
83 _Method.POST, '/session/:sessionId/window/:windowHandle/position')
84 MAXIMIZE_WINDOW = (
85 _Method.POST, '/session/:sessionId/window/:windowHandle/maximize')
86 CLOSE = (_Method.DELETE, '/session/:sessionId/window')
87 DRAG_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/drag')
88 GET_ELEMENT_VALUE_OF_CSS_PROPERTY = (
89 _Method.GET, '/session/:sessionId/element/:id/css/:propertyName')
90 IMPLICITLY_WAIT = (
91 _Method.POST, '/session/:sessionId/timeouts/implicit_wait')
92 SET_SCRIPT_TIMEOUT = (
93 _Method.POST, '/session/:sessionId/timeouts/async_script')
94 SET_TIMEOUT = (_Method.POST, '/session/:sessionId/timeouts')
95 EXECUTE_SQL = (_Method.POST, '/session/:sessionId/execute_sql')
96 GET_LOCATION = (_Method.GET, '/session/:sessionId/location')
97 SET_LOCATION = (_Method.POST, '/session/:sessionId/location')
98 GET_STATUS = (_Method.GET, '/session/:sessionId/application_cache/status')
99 IS_BROWSER_ONLINE = (_Method.GET, '/session/:sessionId/browser_connection')
100 SET_BROWSER_ONLINE = (_Method.POST, '/session/:sessionId/browser_connection')
101 GET_LOCAL_STORAGE_ITEM = (
102 _Method.GET, '/session/:sessionId/local_storage/key/:key')
103 REMOVE_LOCAL_STORAGE_ITEM = (
104 _Method.DELETE, '/session/:sessionId/local_storage/key/:key')
105 GET_LOCAL_STORAGE_KEYS = (_Method.GET, '/session/:sessionId/local_storage')
106 SET_LOCAL_STORAGE_ITEM = (_Method.POST, '/session/:sessionId/local_storage')
107 CLEAR_LOCAL_STORAGE = (_Method.DELETE, '/session/:sessionId/local_storage')
108 GET_LOCAL_STORAGE_SIZE = (
109 _Method.GET, '/session/:sessionId/local_storage/size')
110 GET_SESSION_STORAGE_ITEM = (
111 _Method.GET, '/session/:sessionId/session_storage/key/:key')
112 REMOVE_SESSION_STORAGE_ITEM = (
113 _Method.DELETE, '/session/:sessionId/session_storage/key/:key')
114 GET_SESSION_STORAGE_KEY = (_Method.GET, '/session/:sessionId/session_storage')
115 SET_SESSION_STORAGE_ITEM = (
116 _Method.POST, '/session/:sessionId/session_storage')
117 CLEAR_SESSION_STORAGE = (
118 _Method.DELETE, '/session/:sessionId/session_storage')
119 GET_SESSION_STORAGE_SIZE = (
120 _Method.GET, '/session/:sessionId/session_storage/size')
121 GET_SCREEN_ORIENTATION = (_Method.GET, '/session/:sessionId/orientation')
122 SET_SCREEN_ORIENTATION = (_Method.POST, '/session/:sessionId/orientation')
123 MOUSE_CLICK = (_Method.POST, '/session/:sessionId/click')
124 MOUSE_DOUBLE_CLICK = (_Method.POST, '/session/:sessionId/doubleclick')
125 MOUSE_BUTTON_DOWN = (_Method.POST, '/session/:sessionId/buttondown')
126 MOUSE_BUTTON_UP = (_Method.POST, '/session/:sessionId/buttonup')
127 MOUSE_MOVE_TO = (_Method.POST, '/session/:sessionId/moveto')
128 SEND_KEYS_TO_ACTIVE_ELEMENT = (_Method.POST, '/session/:sessionId/keys')
129 TOUCH_SINGLE_TAP = (_Method.POST, '/session/:sessionId/touch/click')
130 TOUCH_DOWN = (_Method.POST, '/session/:sessionId/touch/down')
131 TOUCH_UP = (_Method.POST, '/session/:sessionId/touch/up')
132 TOUCH_MOVE = (_Method.POST, '/session/:sessionId/touch/move')
133 TOUCH_SCROLL = (_Method.POST, '/session/:sessionId/touch/scroll')
134 TOUCH_DOUBLE_TAP = (_Method.POST, '/session/:sessionId/touch/doubleclick')
135 TOUCH_LONG_PRESS = (_Method.POST, '/session/:sessionId/touch/longclick')
136 TOUCH_FLICK = (_Method.POST, '/session/:sessionId/touch/flick')
137 GET_LOG = (_Method.POST, '/session/:sessionId/log')
138 GET_AVAILABLE_LOG_TYPES = (_Method.GET, '/session/:sessionId/log/types')
139 IS_AUTO_REPORTING = (_Method.GET, '/session/:sessionId/autoreport')
140 SET_AUTO_REPORTING = (_Method.POST, '/session/:sessionId/autoreport')
141 GET_SESSION_LOGS = (_Method.POST, '/logs')
142 STATUS = (_Method.GET, '/status')
144 # Custom Chrome commands.
145 IS_LOADING = (_Method.GET, '/session/:sessionId/is_loading')
148 class CommandExecutor(object):
149 def __init__(self, server_url):
150 self._server_url = server_url
151 port = int(server_url.split(':')[2].split('/')[0])
152 self._http_client = httplib.HTTPConnection('127.0.0.1', port, timeout=30)
154 def Execute(self, command, params):
155 url_parts = command[1].split('/')
156 substituted_parts = []
157 for part in url_parts:
158 if part.startswith(':'):
159 key = part[1:]
160 substituted_parts += [params[key]]
161 del params[key]
162 else:
163 substituted_parts += [part]
165 body = None
166 if command[0] == _Method.POST:
167 body = json.dumps(params)
168 self._http_client.request(command[0], '/'.join(substituted_parts), body)
169 response = self._http_client.getresponse()
171 if response.status == 303:
172 self._http_client.request(_Method.GET, response.getheader('location'))
173 response = self._http_client.getresponse()
174 if response.status != 200:
175 raise RuntimeError('Server returned error: ' + response.reason)
177 return json.loads(response.read())