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.
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')
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')
77 _Method
.GET
, '/session/:sessionId/window/:windowHandle/size')
78 GET_WINDOW_POSITION
= (
79 _Method
.GET
, '/session/:sessionId/window/:windowHandle/position')
81 _Method
.POST
, '/session/:sessionId/window/:windowHandle/size')
82 SET_WINDOW_POSITION
= (
83 _Method
.POST
, '/session/:sessionId/window/:windowHandle/position')
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')
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_NETWORK_CONDITIONS
= (
99 _Method
.GET
, '/session/:sessionId/chromium/network_conditions')
100 SET_NETWORK_CONDITIONS
= (
101 _Method
.POST
, '/session/:sessionId/chromium/network_conditions')
102 DELETE_NETWORK_CONDITIONS
= (
103 _Method
.DELETE
, '/session/:sessionId/chromium/network_conditions')
104 GET_STATUS
= (_Method
.GET
, '/session/:sessionId/application_cache/status')
105 IS_BROWSER_ONLINE
= (_Method
.GET
, '/session/:sessionId/browser_connection')
106 SET_BROWSER_ONLINE
= (_Method
.POST
, '/session/:sessionId/browser_connection')
107 GET_LOCAL_STORAGE_ITEM
= (
108 _Method
.GET
, '/session/:sessionId/local_storage/key/:key')
109 REMOVE_LOCAL_STORAGE_ITEM
= (
110 _Method
.DELETE
, '/session/:sessionId/local_storage/key/:key')
111 GET_LOCAL_STORAGE_KEYS
= (_Method
.GET
, '/session/:sessionId/local_storage')
112 SET_LOCAL_STORAGE_ITEM
= (_Method
.POST
, '/session/:sessionId/local_storage')
113 CLEAR_LOCAL_STORAGE
= (_Method
.DELETE
, '/session/:sessionId/local_storage')
114 GET_LOCAL_STORAGE_SIZE
= (
115 _Method
.GET
, '/session/:sessionId/local_storage/size')
116 GET_SESSION_STORAGE_ITEM
= (
117 _Method
.GET
, '/session/:sessionId/session_storage/key/:key')
118 REMOVE_SESSION_STORAGE_ITEM
= (
119 _Method
.DELETE
, '/session/:sessionId/session_storage/key/:key')
120 GET_SESSION_STORAGE_KEY
= (_Method
.GET
, '/session/:sessionId/session_storage')
121 SET_SESSION_STORAGE_ITEM
= (
122 _Method
.POST
, '/session/:sessionId/session_storage')
123 CLEAR_SESSION_STORAGE
= (
124 _Method
.DELETE
, '/session/:sessionId/session_storage')
125 GET_SESSION_STORAGE_SIZE
= (
126 _Method
.GET
, '/session/:sessionId/session_storage/size')
127 GET_SCREEN_ORIENTATION
= (_Method
.GET
, '/session/:sessionId/orientation')
128 SET_SCREEN_ORIENTATION
= (_Method
.POST
, '/session/:sessionId/orientation')
129 MOUSE_CLICK
= (_Method
.POST
, '/session/:sessionId/click')
130 MOUSE_DOUBLE_CLICK
= (_Method
.POST
, '/session/:sessionId/doubleclick')
131 MOUSE_BUTTON_DOWN
= (_Method
.POST
, '/session/:sessionId/buttondown')
132 MOUSE_BUTTON_UP
= (_Method
.POST
, '/session/:sessionId/buttonup')
133 MOUSE_MOVE_TO
= (_Method
.POST
, '/session/:sessionId/moveto')
134 SEND_KEYS_TO_ACTIVE_ELEMENT
= (_Method
.POST
, '/session/:sessionId/keys')
135 TOUCH_SINGLE_TAP
= (_Method
.POST
, '/session/:sessionId/touch/click')
136 TOUCH_DOWN
= (_Method
.POST
, '/session/:sessionId/touch/down')
137 TOUCH_UP
= (_Method
.POST
, '/session/:sessionId/touch/up')
138 TOUCH_MOVE
= (_Method
.POST
, '/session/:sessionId/touch/move')
139 TOUCH_SCROLL
= (_Method
.POST
, '/session/:sessionId/touch/scroll')
140 TOUCH_DOUBLE_TAP
= (_Method
.POST
, '/session/:sessionId/touch/doubleclick')
141 TOUCH_LONG_PRESS
= (_Method
.POST
, '/session/:sessionId/touch/longclick')
142 TOUCH_FLICK
= (_Method
.POST
, '/session/:sessionId/touch/flick')
143 GET_LOG
= (_Method
.POST
, '/session/:sessionId/log')
144 GET_AVAILABLE_LOG_TYPES
= (_Method
.GET
, '/session/:sessionId/log/types')
145 IS_AUTO_REPORTING
= (_Method
.GET
, '/session/:sessionId/autoreport')
146 SET_AUTO_REPORTING
= (_Method
.POST
, '/session/:sessionId/autoreport')
147 GET_SESSION_LOGS
= (_Method
.POST
, '/logs')
148 STATUS
= (_Method
.GET
, '/status')
150 # Custom Chrome commands.
151 IS_LOADING
= (_Method
.GET
, '/session/:sessionId/is_loading')
152 TOUCH_PINCH
= (_Method
.POST
, '/session/:sessionId/touch/pinch')
155 class CommandExecutor(object):
156 def __init__(self
, server_url
):
157 self
._server
_url
= server_url
158 port
= int(server_url
.split(':')[2].split('/')[0])
159 self
._http
_client
= httplib
.HTTPConnection('127.0.0.1', port
, timeout
=30)
161 def Execute(self
, command
, params
):
162 url_parts
= command
[1].split('/')
163 substituted_parts
= []
164 for part
in url_parts
:
165 if part
.startswith(':'):
167 substituted_parts
+= [params
[key
]]
170 substituted_parts
+= [part
]
173 if command
[0] == _Method
.POST
:
174 body
= json
.dumps(params
)
175 self
._http
_client
.request(command
[0], '/'.join(substituted_parts
), body
)
176 response
= self
._http
_client
.getresponse()
178 if response
.status
== 303:
179 self
._http
_client
.request(_Method
.GET
, response
.getheader('location'))
180 response
= self
._http
_client
.getresponse()
181 if response
.status
!= 200:
182 raise RuntimeError('Server returned error: ' + response
.reason
)
184 return json
.loads(response
.read())