Best Python code snippet using avocado_python
command_executor.py
Source:command_executor.py
1# Copyright 2013 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4import httplib5import json6class _Method(object):7 GET = 'GET'8 POST = 'POST'9 DELETE = 'DELETE'10class Command(object):11 NEW_SESSION = (_Method.POST, '/session')12 GET_SESSION_CAPABILITIES = (_Method.GET, '/session/:sessionId')13 GET_SESSIONS = (_Method.GET, '/sessions')14 QUIT = (_Method.DELETE, '/session/:sessionId')15 GET_CURRENT_WINDOW_HANDLE = (_Method.GET, '/session/:sessionId/window_handle')16 GET_WINDOW_HANDLES = (_Method.GET, '/session/:sessionId/window_handles')17 GET = (_Method.POST, '/session/:sessionId/url')18 GET_ALERT = (_Method.GET, '/session/:sessionId/alert')19 DISMISS_ALERT = (_Method.POST, '/session/:sessionId/dismiss_alert')20 ACCEPT_ALERT = (_Method.POST, '/session/:sessionId/accept_alert')21 GET_ALERT_TEXT = (_Method.GET, '/session/:sessionId/alert_text')22 SET_ALERT_VALUE = (_Method.POST, '/session/:sessionId/alert_text')23 GO_FORWARD = (_Method.POST, '/session/:sessionId/forward')24 GO_BACK = (_Method.POST, '/session/:sessionId/back')25 REFRESH = (_Method.POST, '/session/:sessionId/refresh')26 EXECUTE_SCRIPT = (_Method.POST, '/session/:sessionId/execute')27 EXECUTE_ASYNC_SCRIPT = (_Method.POST, '/session/:sessionId/execute_async')28 LAUNCH_APP = (_Method.POST, '/session/:sessionId/chromium/launch_app')29 GET_CURRENT_URL = (_Method.GET, '/session/:sessionId/url')30 GET_TITLE = (_Method.GET, '/session/:sessionId/title')31 GET_PAGE_SOURCE = (_Method.GET, '/session/:sessionId/source')32 SCREENSHOT = (_Method.GET, '/session/:sessionId/screenshot')33 SET_BROWSER_VISIBLE = (_Method.POST, '/session/:sessionId/visible')34 IS_BROWSER_VISIBLE = (_Method.GET, '/session/:sessionId/visible')35 FIND_ELEMENT = (_Method.POST, '/session/:sessionId/element')36 FIND_ELEMENTS = (_Method.POST, '/session/:sessionId/elements')37 GET_ACTIVE_ELEMENT = (_Method.POST, '/session/:sessionId/element/active')38 FIND_CHILD_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/element')39 FIND_CHILD_ELEMENTS = (40 _Method.POST, '/session/:sessionId/element/:id/elements')41 CLICK_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/click')42 CLEAR_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/clear')43 SUBMIT_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/submit')44 GET_ELEMENT_TEXT = (_Method.GET, '/session/:sessionId/element/:id/text')45 SEND_KEYS_TO_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/value')46 UPLOAD_FILE = (_Method.POST, '/session/:sessionId/file')47 GET_ELEMENT_VALUE = (_Method.GET, '/session/:sessionId/element/:id/value')48 GET_ELEMENT_TAG_NAME = (_Method.GET, '/session/:sessionId/element/:id/name')49 IS_ELEMENT_SELECTED = (50 _Method.GET, '/session/:sessionId/element/:id/selected')51 IS_ELEMENT_ENABLED = (_Method.GET, '/session/:sessionId/element/:id/enabled')52 IS_ELEMENT_DISPLAYED = (53 _Method.GET, '/session/:sessionId/element/:id/displayed')54 HOVER_OVER_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/hover')55 GET_ELEMENT_LOCATION = (56 _Method.GET, '/session/:sessionId/element/:id/location')57 GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW = (58 _Method.GET, '/session/:sessionId/element/:id/location_in_view')59 GET_ELEMENT_SIZE = (_Method.GET, '/session/:sessionId/element/:id/size')60 GET_ELEMENT_ATTRIBUTE = (61 _Method.GET, '/session/:sessionId/element/:id/attribute/:name')62 ELEMENT_EQUALS = (63 _Method.GET, '/session/:sessionId/element/:id/equals/:other')64 GET_COOKIES = (_Method.GET, '/session/:sessionId/cookie')65 ADD_COOKIE = (_Method.POST, '/session/:sessionId/cookie')66 DELETE_ALL_COOKIES = (_Method.DELETE, '/session/:sessionId/cookie')67 DELETE_COOKIE = (_Method.DELETE, '/session/:sessionId/cookie/:name')68 SWITCH_TO_FRAME = (_Method.POST, '/session/:sessionId/frame')69 SWITCH_TO_PARENT_FRAME = (_Method.POST, '/session/:sessionId/frame/parent')70 SWITCH_TO_WINDOW = (_Method.POST, '/session/:sessionId/window')71 GET_WINDOW_SIZE = (72 _Method.GET, '/session/:sessionId/window/:windowHandle/size')73 GET_WINDOW_POSITION = (74 _Method.GET, '/session/:sessionId/window/:windowHandle/position')75 SET_WINDOW_SIZE = (76 _Method.POST, '/session/:sessionId/window/:windowHandle/size')77 SET_WINDOW_POSITION = (78 _Method.POST, '/session/:sessionId/window/:windowHandle/position')79 MAXIMIZE_WINDOW = (80 _Method.POST, '/session/:sessionId/window/:windowHandle/maximize')81 CLOSE = (_Method.DELETE, '/session/:sessionId/window')82 DRAG_ELEMENT = (_Method.POST, '/session/:sessionId/element/:id/drag')83 GET_ELEMENT_VALUE_OF_CSS_PROPERTY = (84 _Method.GET, '/session/:sessionId/element/:id/css/:propertyName')85 IMPLICITLY_WAIT = (86 _Method.POST, '/session/:sessionId/timeouts/implicit_wait')87 SET_SCRIPT_TIMEOUT = (88 _Method.POST, '/session/:sessionId/timeouts/async_script')89 SET_TIMEOUT = (_Method.POST, '/session/:sessionId/timeouts')90 EXECUTE_SQL = (_Method.POST, '/session/:sessionId/execute_sql')91 GET_LOCATION = (_Method.GET, '/session/:sessionId/location')92 SET_LOCATION = (_Method.POST, '/session/:sessionId/location')93 GET_NETWORK_CONNECTION = (94 _Method.GET, '/session/:sessionId/network_connection')95 GET_NETWORK_CONDITIONS = (96 _Method.GET, '/session/:sessionId/chromium/network_conditions')97 SET_NETWORK_CONDITIONS = (98 _Method.POST, '/session/:sessionId/chromium/network_conditions')99 DELETE_NETWORK_CONDITIONS = (100 _Method.DELETE, '/session/:sessionId/chromium/network_conditions')101 GET_STATUS = (_Method.GET, '/session/:sessionId/application_cache/status')102 IS_BROWSER_ONLINE = (_Method.GET, '/session/:sessionId/browser_connection')103 SET_BROWSER_ONLINE = (_Method.POST, '/session/:sessionId/browser_connection')104 GET_LOCAL_STORAGE_ITEM = (105 _Method.GET, '/session/:sessionId/local_storage/key/:key')106 REMOVE_LOCAL_STORAGE_ITEM = (107 _Method.DELETE, '/session/:sessionId/local_storage/key/:key')108 GET_LOCAL_STORAGE_KEYS = (_Method.GET, '/session/:sessionId/local_storage')109 SET_LOCAL_STORAGE_ITEM = (_Method.POST, '/session/:sessionId/local_storage')110 CLEAR_LOCAL_STORAGE = (_Method.DELETE, '/session/:sessionId/local_storage')111 GET_LOCAL_STORAGE_SIZE = (112 _Method.GET, '/session/:sessionId/local_storage/size')113 GET_SESSION_STORAGE_ITEM = (114 _Method.GET, '/session/:sessionId/session_storage/key/:key')115 REMOVE_SESSION_STORAGE_ITEM = (116 _Method.DELETE, '/session/:sessionId/session_storage/key/:key')117 GET_SESSION_STORAGE_KEY = (_Method.GET, '/session/:sessionId/session_storage')118 SET_SESSION_STORAGE_ITEM = (119 _Method.POST, '/session/:sessionId/session_storage')120 CLEAR_SESSION_STORAGE = (121 _Method.DELETE, '/session/:sessionId/session_storage')122 GET_SESSION_STORAGE_SIZE = (123 _Method.GET, '/session/:sessionId/session_storage/size')124 GET_SCREEN_ORIENTATION = (_Method.GET, '/session/:sessionId/orientation')125 SET_SCREEN_ORIENTATION = (_Method.POST, '/session/:sessionId/orientation')126 DELETE_SCREEN_ORIENTATION = (127 _Method.DELETE, '/session/:sessionId/orientation')128 MOUSE_CLICK = (_Method.POST, '/session/:sessionId/click')129 MOUSE_DOUBLE_CLICK = (_Method.POST, '/session/:sessionId/doubleclick')130 MOUSE_BUTTON_DOWN = (_Method.POST, '/session/:sessionId/buttondown')131 MOUSE_BUTTON_UP = (_Method.POST, '/session/:sessionId/buttonup')132 MOUSE_MOVE_TO = (_Method.POST, '/session/:sessionId/moveto')133 SEND_KEYS_TO_ACTIVE_ELEMENT = (_Method.POST, '/session/:sessionId/keys')134 TOUCH_SINGLE_TAP = (_Method.POST, '/session/:sessionId/touch/click')135 TOUCH_DOWN = (_Method.POST, '/session/:sessionId/touch/down')136 TOUCH_UP = (_Method.POST, '/session/:sessionId/touch/up')137 TOUCH_MOVE = (_Method.POST, '/session/:sessionId/touch/move')138 TOUCH_SCROLL = (_Method.POST, '/session/:sessionId/touch/scroll')139 TOUCH_DOUBLE_TAP = (_Method.POST, '/session/:sessionId/touch/doubleclick')140 TOUCH_LONG_PRESS = (_Method.POST, '/session/:sessionId/touch/longclick')141 TOUCH_FLICK = (_Method.POST, '/session/:sessionId/touch/flick')142 GET_LOG = (_Method.POST, '/session/:sessionId/log')143 GET_AVAILABLE_LOG_TYPES = (_Method.GET, '/session/:sessionId/log/types')144 IS_AUTO_REPORTING = (_Method.GET, '/session/:sessionId/autoreport')145 SET_AUTO_REPORTING = (_Method.POST, '/session/:sessionId/autoreport')146 GET_SESSION_LOGS = (_Method.POST, '/logs')147 STATUS = (_Method.GET, '/status')148 SET_NETWORK_CONNECTION = (149 _Method.POST, '/session/:sessionId/network_connection')150 # Custom Chrome commands.151 IS_LOADING = (_Method.GET, '/session/:sessionId/is_loading')152 TOUCH_PINCH = (_Method.POST, '/session/:sessionId/touch/pinch')153class CommandExecutor(object):154 def __init__(self, server_url):155 self._server_url = server_url156 port = int(server_url.split(':')[2].split('/')[0])157 self._http_client = httplib.HTTPConnection('127.0.0.1', port, timeout=30)158 def Execute(self, command, params):159 url_parts = command[1].split('/')160 substituted_parts = []161 for part in url_parts:162 if part.startswith(':'):163 key = part[1:]164 substituted_parts += [params[key]]165 del params[key]166 else:167 substituted_parts += [part]168 body = None169 if command[0] == _Method.POST:170 body = json.dumps(params)171 self._http_client.request(command[0], '/'.join(substituted_parts), body)172 response = self._http_client.getresponse()173 if response.status == 303:174 self._http_client.request(_Method.GET, response.getheader('location'))175 response = self._http_client.getresponse()176 result = json.loads(response.read())177 if response.status != 200 and 'error' not in result:178 raise RuntimeError('Server returned error: ' + response.reason)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!