def install_app(driver, device, app_pkg): # 手机是否已安装app,未安装则直接安装 apk = ApkInfo(app_pkg) pkg_name = apk.get_package_name() installed_pkg = adb_command(device, 'shell pm list package', False) if pkg_name not in installed_pkg: logger.info('app未安装,正在安装') Thread(target=loop_accept_alert, args=[driver]).start() # 启动多线程处理弹框 adb_command(device, 'install --no-streaming %s' % app_pkg) logger.info('app安装完成') return # 已安装与待安装的app版本号是否一致,不一致则卸载重装 new_version = apk.get_version_code() old_version = re.findall( r'versionCode=(\d+)', adb_command(device, 'shell dumpsys package %s' % pkg_name, False))[0] logger.info('新版本号:%s 旧版本号:%s' % (new_version, old_version)) if new_version != old_version: logger.info('app版本号不一致,卸载重装') adb_command(device, 'uninstall %s' % pkg_name) Thread(target=loop_accept_alert, args=[driver]).start() # 启动多线程处理弹框 adb_command(device, 'install --no-streaming %s' % app_pkg) logger.info('app安装完成') return logger.info('app已安装,无需安装')
def get_desired_caps(self, device_alias, app): desired_caps = self.device_info.get(device_alias) if not desired_caps: raise Exception('找不到此设备:%s,请检查配置./config/devices.yaml' % device_alias) # app包 desired_caps['app'] = app # 闲置超时时间 desired_caps['newCommandTimeout'] = 3600 # 如果配置文件定义了appPackage、appActivity,则按配置文件来,否则根据apk包自动获取; if not desired_caps["appPackage"] or not desired_caps["appActivity"]: apk_info = ApkInfo(app) if not desired_caps["appPackage"]: desired_caps["appPackage"] = apk_info.get_package_name() if not desired_caps["appActivity"]: desired_caps["appActivity"] = apk_info.get_activity() # 先检查有线连接是否OK,若未连接,采用无线连接 device_name = desired_caps['deviceName'] if not self.adb.is_connected(device_name): device_list = desired_caps['device_address'] if not device_list: device_list = [] for i in device_list: try: device_name = self.adb.connect(i) # 兼容性模拟器调试 if not device_name: continue break except ConnectionError as e: print(e) desired_caps['deviceName'] = device_name phone_info = get_phone_info(device_name) # 自动设置platformVersion desired_caps["platformVersion"] = phone_info["release"] # 从记忆库中选择chromedriver,如果没有则自动选择,并记录到记忆库 webview_version = self._read_webview_version(phone_info["model"]) if not webview_version: webview_version = get_webview_version(device_name, desired_caps['appPackage'], desired_caps['appActivity']) self._save_webview_version(phone_info["model"], webview_version) desired_caps['chromedriverExecutable'] = auto_chromedriver(webview_version) return desired_caps