def __init__(self, host, port=7912): """ Args: host (str): host address port (int): port number Raises: EnvironmentError """ self._host = host self._port = port self._reqsess = TimeoutRequestsSession( ) # use requests.Session to enable HTTP Keep-Alive self._server_url = 'http://{}:{}'.format(host, port) self._server_jsonrpc_url = self._server_url + "/jsonrpc/0" self._default_session = Session(self, None) self._cached_plugins = {} self.__devinfo = None self._hooks = {} self.platform = None # hot fix for weditor self.ash = AdbShell(self.shell) # the powerful adb shell self.wait_timeout = 20.0 # wait element timeout self.click_post_delay = None # wait after each click self._freeze() # prevent creating new attrs
def session(self, pkg_name=None, attach=False, launch_timeout=None): """ Create a new session Args: pkg_name (str): android package name attach (bool): attach to already running app launch_timeout (int): launch timeout Raises: requests.HTTPError, SessionBrokenError """ if pkg_name is None: return self._default_session if not attach: request_data = {"flags": "-W -S"} if launch_timeout: request_data["timeout"] = str(launch_timeout) resp = self._reqsess.post(self.path2url("/session/" + pkg_name), data=request_data) if resp.status_code == 410: # Gone raise SessionBrokenError(pkg_name, resp.text) resp.raise_for_status() jsondata = resp.json() if not jsondata["success"]: raise SessionBrokenError("app launch failed", jsondata["error"], jsondata["output"]) time.sleep(2.5) # wait launch finished, maybe no need pid = self._pidof_app(pkg_name) if not pid: raise SessionBrokenError(pkg_name) return Session(self, pkg_name, pid)