How to use get_available_forward_local method in Airtest

Best Python code snippet using Airtest

adb.py

Source:adb.py Github

copy

Full Screen

...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,如不填写则清楚所以绑定...

Full Screen

Full Screen

__init__.py

Source:__init__.py Github

copy

Full Screen

...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,...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful