Best Python code snippet using Airtest
adb.py
Source:adb.py
...286 else:287 forward_local_using = self.get_forwards()288 logger.info('{} {} has been forward', forward_local_using[index]['local'],289 forward_local_using[index]['remote'])290 def get_available_forward_local(self) -> int:291 """292 è·åä¸ä¸ªå¯ç¨ç«¯å£293 :return:294 port295 """296 sock = socket.socket()297 port = random.randint(11111, 20000)298 result = False299 try:300 sock.bind((self.host, port))301 result = True302 # logger.debug('port:{} can use'.format(port))303 except socket.error:304 logger.debug('port:{} is in use'.format(port))305 sock.close()306 if not result:307 return self.get_available_forward_local()308 return port309 def _local_in_forwards(self, local: str = None, remote: str = None) -> Union[Tuple[bool, int], Tuple[bool, None]]:310 """311 æ£æ¥localæ¯å¦å·²ç»å¯ç¨312 :return:313 bool, if True return index in _forward_local_using314 """315 l = self.get_forwards()316 for i, value in enumerate(l):317 if local:318 if value['local'] == local:319 return True, i320 if remote:321 if value['remote'] == remote:322 return True, i323 return False, None324 def abi_version(self):325 """ get abi (application binary interface) """326 abi = self.getprop('ro.product.cpu.abi')327 logger.info('device {} abi is {}'.format(self.device_id, abi).rstrip('\r\n'))328 return abi329 def sdk_version(self):330 """ get sdk version """331 sdk = self.getprop('ro.build.version.sdk')332 logger.info('device {} sdk is {}'.format(self.device_id, sdk).rstrip('\r\n'))333 return int(sdk)334 def check_file(self, path: str, name: str) -> bool:335 """336 command adb shell find 'name' in the 'path'337 Args:338 path: å¨è®¾å¤ä¸çè·¯å¾339 name: éè¦æ£æ¥çæ件340 :return:341 bool342 """343 return bool(self.raw_shell(['find', path, '-name', name]))344 def get_process_status(self, pid: int = None, name: str = None) -> list:345 """346 adb shell ps347 è·ååºç¨ç¶æ348 Args:349 pid: æç
§pid寻æ¾350 name: éè¿grep寻æ¾å¹é
çname(并ä¸æ¯ç²¾å寻æ¾,åªè¦æå¹é
ç项é½ä¼è¿å)351 :return:352 list æ¯ä¸é¡¹é½å
å«äºpsä¿¡æ¯353 """354 if pid:355 shell = ['ps -x', str(pid)]356 elif name:357 shell = "ps | grep -w \"{}\"".format(name)358 else:359 shell = 'ps'360 out = self.raw_shell(shell, skip_error=True)361 out = split_process_status(out)362 if not out:363 logger.error('{}: {} is not started', pid and 'pid' or 'name', pid and pid or name)364 return out365 def kill_process(self, pid: int = None, name: str = None):366 """367 command adb shell kill [pid]368 :param name: éè¦ææ»çè¿ç¨å369 :param pid: éè¦ææ»çè¿ç¨pid370 :return:371 None372 """373 if pid:374 out = self.get_process_status(pid=pid)375 if out:376 pid = out[0]['PID']377 else:378 return False379 elif name:380 out = self.get_process_status(name=name)381 if out:382 if len(out) > 1:383 logger.info('å¹é
å°å¤ä¸ªè¿ç¨')384 pid = out[0]['PID']385 else:386 return False387 self.start_shell(['kill', str(pid)])388 logger.info('{} PID:{} NAME:{} is kill', self.device_id, pid, out[0]['NAME'])389 def get_device_id(self, decode: bool = False) -> str:390 return decode and self.device_id.replace(':', '_') or self.device_id391 def set_forward(self, remote: str):392 """393 éè¿get_available_forward_localè·åå¯ç¨ç«¯å£,并ä¸remoteç»å®394 Args:395 remote: è¦ä¸localç»å®ç设å¤ç«¯å£ localabstract:{remote}"396 :return:397 localport:è¦è½¬åçæ¬å°ç«¯å£ tcp:<local>398 remote:è¦ä¸localç»å®ç设å¤ç«¯å£ localabstract:{remote}"`399 """400 localport = self.get_available_forward_local()401 self.forward('tcp:%s' % localport, remote)402 return localport, remote403 def get_forward_port(self, remote: str):404 """è·åå¼æ¾ç«¯å£ç端å£å·"""405 index = self._local_in_forwards(remote='localabstract:%s' % remote)406 if not index[0]:407 logger.error('No port corresponding to remote was found')408 return None409 return int(re.compile(r'tcp:(\d+)').findall(self.get_forwards()[index[1]]['local'])[0])410 def remove_forward(self, local=None):411 """412 è¿è¡adb forward -- remove413 :param local:414 tcp port,å¦ä¸å¡«ååæ¸
æ¥æ以ç»å®...
__init__.py
Source:__init__.py
...87 remote = f'localabstract:{self.MNC_LOCAL_NAME}'88 if port := self.device.get_forward_port(remote=remote, device_id=self.device.device_id):89 self.MNC_PORT = port90 return91 self.MNC_PORT = self.MNC_PORT or self.device.get_available_forward_local()92 self.device.forward(local=f'tcp:{self.MNC_PORT}', remote=remote)93 def _install_minicap(self) -> None:94 """95 check if minicap and minicap.so installed96 Returns:97 None98 """99 if not self.device.check_file(ANDROID_TMP_PATH, 'minicap'):100 self.device.push(local=MNC_LOCAL_PATH.format(abi_version=self.device.abi_version),101 remote=MNC_REMOTE_PATH)102 time.sleep(1)103 self.device.shell(['chmod', '755', MNC_REMOTE_PATH])104 if not self.device.check_file(ANDROID_TMP_PATH, 'minicap.so'):105 self.device.push(local=MNC_SO_LOCAL_PATH.format(abi_version=self.device.abi_version,...
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!!