def connect_to_wifi_with_app(uiauto_device: Android_Device, wifi_name, wifi_password): is_existed = Utils.is_onslaught_app_installed(uiauto_device.device_serial) current_ssid = Utils.get_wifi_ssid(uiauto_device.device_serial) print("the wifi which " + uiauto_device.device_serial + " had been connected to wifi " + current_ssid) if current_ssid.__eq__(wifi_name): return save_setting_id = "com.android.settings:id/save" if is_existed: uiautomator = uiautomator2.connect_usb( serial=uiauto_device.device_serial) uiautomator.app_start(package_name=onslaughtapp_package) uiautomator(resourceId=onslaughtapp_resource_id + "wifi_ssid").clear_text() uiautomator(resourceId=onslaughtapp_resource_id + "wifi_ssid").set_text(wifi_name) uiautomator(resourceId=onslaughtapp_resource_id + "wifi_password").send_keys(wifi_password) uiautomator(resourceId=onslaughtapp_resource_id + "connect_to_wifi").click() print("the android version of current test device is : " + uiauto_device.version) """ 对于 android 11 以及以上的系统, 会出现一个系统的弹窗让用户需选择 """ version = str(uiauto_device.version) if version.__contains__("."): ver = version.split(".")[0] if int(ver) >= 11: uiautomator(resourceId=save_setting_id).click() elif int(uiauto_device.version) >= 11: uiautomator(resourceId=save_setting_id).click() time.sleep(6)
def connect_to_wifi(device_serial: str, wifi_name, wifi_password): print("the device " + device_serial + " try to connect the wifi " + wifi_name) # android.settings.WIFI_SETTINGS Utils.switch_on_wifi(device_serial) uiAuto = uiautomator2.connect_usb(serial=device_serial) """ 即使通过 start -a android.settings.WIFI_SETTINGS 进入到界面 也有可能是在其他的界面(比如当前已经连接的 WiFi的信息界面. 我们必须杀死进程才能再次进去 进去的时候 等待 20s, 增加等待时长, 有时候需要自动连接, 需要等待更长的时间 """ uiAuto.app_stop("com.android.settings") time.sleep(2) wifi_intent = "android.settings.WIFI_SETTINGS" start_wifi_activity = "adb -s " + device_serial + " shell am start -a " + wifi_intent subprocess.getstatusoutput(start_wifi_activity) time.sleep(20) if not uiAuto(text=wifi_name).exists: uiAuto.swipe(300, 900, 300, 200) if not uiAuto(text=wifi_name).exists: uiAuto.swipe(300, 900, 300, 200) if not uiAuto(text=wifi_name).exists: uiAuto.swipe(300, 900, 300, 200) if not uiAuto(text=wifi_name).exists: print("finally we have not found the wifi: " + wifi_name + ", for device: " + device_serial) return uiAuto(text=wifi_name).click() """ 在某些情况下, 这个设备已经连接过这个 WiFi 了, 执行 sendkeys 会报错, """ if uiAuto(text="Advanced options").exists() or uiAuto( text="高级选项").exists(): try: uiAuto.send_keys(wifi_password, clear=True) if uiAuto(text="连接").exists(): uiAuto(text="连接").click() else: if uiAuto(text="Connect").exists(): uiAuto(text="Connect").click() else: if uiAuto(text="CONNECT").exists(): uiAuto(text="CONNECT").click() except UiObjectNotFoundError as error: print(error) if uiAuto(text="Frequency").exists(): print("the device had been connected to the wifi " + wifi_name) finally: pass """ 等待 几秒钟, 这样才会dump出wifi的信息 """ time.sleep(5) device_wifi_name = Utils.get_wifi_ssid(device_serial) if device_wifi_name.__eq__(wifi_name): print("hi, the device switch to " + wifi_name + ", successfully! ") time.sleep(5)