def connect_usb(serial=None, healthcheck=False): """ Args: serial (str): android device serial healthcheck (bool): start uiautomator if not ready Returns: UIAutomatorServer Raises: ConnectError """ adb = adbutils.AdbClient() if not serial: device = adb.must_one_device() else: device = adbutils.AdbDevice(adb, serial) # adb = adbutils.Adb(serial) lport = device.forward_port(7912) d = connect_wifi('127.0.0.1:' + str(lport)) if healthcheck: if not d.agent_alive: warnings.warn("backend atx-agent is not alive, start again ...", RuntimeWarning) # TODO: /data/local/tmp might not be execuable and atx-agent can be somewhere else device.shell_output("/data/local/tmp/atx-agent", "server", "-d") deadline = time.time() + 3 while time.time() < deadline: if d.alive: break elif not d.alive: warnings.warn("backend uiautomator2 is not alive, start again ...", RuntimeWarning) d.reset_uiautomator() return d
def connect_usb(serial=None, healthcheck=False, init=True): """ Args: serial (str): android device serial healthcheck (bool): start uiautomator if not ready init (bool): initial with apk and atx-agent Returns: Device Raises: ConnectError """ adb = adbutils.AdbClient() if not serial: device = adb.device() else: device = adbutils.AdbDevice(adb, serial) lport = device.forward_port(7912) d = connect_wifi('127.0.0.1:' + str(lport)) d._serial = device.serial if not d.agent_alive or not d.alive: initer = Initer(device) if not initer.check_install(): if not init: raise RuntimeError( "Device need to be init with command: uiautomator2 init -s " + device.serial) initer.install() # same as run cli: uiautomator2 init elif not d.agent_alive: warnings.warn("start atx-agent ...", RuntimeWarning) # TODO: /data/local/tmp might not be execuable and atx-agent can be somewhere else device.shell( ["/data/local/tmp/atx-agent", "server", "--nouia", "-d"]) deadline = time.time() + 3 while time.time() < deadline: if d.agent_alive: break else: raise RuntimeError("atx-agent recover failed") if healthcheck: if not d.alive: warnings.warn("start uiautomator2 ...", RuntimeWarning) d.healthcheck() return d