Best Python code snippet using uiautomator
__init__.py
Source:__init__.py  
...330    def install(self):331        base_dir = os.path.dirname(__file__)332        for apk in self.__apk_files:333            self.adb.cmd("install", "-r -t", os.path.join(base_dir, apk)).wait()334    def install_androidx(self):335        base_dir = os.path.dirname(__file__)336        for apk in self.__androidx_apk_files:337            self.adb.cmd("install", "-r -t", os.path.join(base_dir, apk)).wait()338    @property339    def jsonrpc(self):340        return self.jsonrpc_wrap(timeout=int(os.environ.get("jsonrpc_timeout", 90)))341    def jsonrpc_wrap(self, timeout):342        server = self343        ERROR_CODE_BASE = -32000344        def _JsonRPCMethod(url, method, timeout, restart=True):345            _method_obj = JsonRPCMethod(url, method, timeout)346            def wrapper(*args, **kwargs):347                URLError = urllib3.exceptions.HTTPError if os.name == "nt" else urllib2.URLError348                try:349                    return _method_obj(*args, **kwargs)350                except (URLError, socket.error, HTTPException) as e:351                    if restart:352                        server.stop()353                        server.start(timeout=30)354                        return _JsonRPCMethod(url, method, timeout, False)(*args, **kwargs)355                    else:356                        raise357                except JsonRPCError as e:358                    if e.code >= ERROR_CODE_BASE - 1:359                        server.stop()360                        server.start()361                        return _method_obj(*args, **kwargs)362                    elif e.code == ERROR_CODE_BASE - 2 and self.handlers['on']:  # Not Found363                        try:364                            self.handlers['on'] = False365                            # any handler returns True will break the left handlers366                            any(handler(self.handlers.get('device', None)) for handler in self.handlers['handlers'])367                        finally:368                            self.handlers['on'] = True369                        return _method_obj(*args, **kwargs)370                    raise371            return wrapper372        return JsonRPCClient(self.rpc_uri,373                             timeout=timeout,374                             method_class=_JsonRPCMethod)375    def __jsonrpc(self):376        return JsonRPCClient(self.rpc_uri, timeout=int(os.environ.get("JSONRPC_TIMEOUT", 90)))377    def sdk_version(self):378        '''sdk version of connected device.'''379        if self.__sdk == 0:380            try:381                self.__sdk = int(self.adb.cmd("shell", "getprop", "ro.build.version.sdk").communicate()[0].decode("utf-8").strip())382            except:383                pass384        return self.__sdk385    def start(self, timeout=5):386        if self.sdk_version() < 18:387            files = self.push()388            cmd = list(itertools.chain(389                ["shell", "uiautomator", "runtest"],390                files,391                ["-c", "com.github.uiautomatorstub.Stub"]392            ))393        elif self.sdk_version() >= 28:394            self.install_androidx()395            cmd = ["shell", "am", "instrument", "-w",396                   "com.github.uiautomator.test/androidx.test.runner.AndroidJUnitRunner"]           397        else:398            self.install()399            cmd = ["shell", "am", "instrument", "-w",400                   "com.github.uiautomator.test/android.support.test.runner.AndroidJUnitRunner"]401        self.uiautomator_process = self.adb.cmd(*cmd)402        self.adb.forward(self.local_port, self.device_port)403        while not self.alive and timeout > 0:404            time.sleep(0.1)405            timeout -= 0.1406        if not self.alive:407            raise IOError("RPC server not started!")408    def ping(self):...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!!
