一、app已经按装
# 计算器: C:Usersdel>adb shell dumpsys window w |findstr / |findstr name= # mSurface=Surface(name=com.youba.calculate/com.youba.calculate.MainActivity) #adb命令查看包名和启动activity #com.youba.calculate:id/btn_plus + #com.youba.calculate:id/btn_equal = #凤凰网: C:Usersdel>adb shell dumpsys window w |findstr / |findstr name= # mSurface=Surface(name=com.ifeng.news2/com.ifeng.news2.activity.IfengTabMainActivity) from appium import webdriver import time desired_caps = { 'platformName': 'Android', #待测试手机的系统 'deviceName':'8DF6R17503000072', #使用手机或模拟器的类型 'platformVersion': '8.0.0', #待测手机系统版本 "unicodeKeyboard":"true", #支持中文输入,会自动安装unicode输入法,默认是false "resetKeyboard":"true", #如果设置了中文输入为true,此设置为自动化退出后,重置输入法到原来状态 #'app': 'C:\Users\del\Desktop\b\jsq.apk', #手机未按照待测app时,设置安装路径、包名、启动activity 'appPackage': 'com.youba.calculate', 'appActivity': 'com.youba.calculate.MainActivity', } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(5) driver.find_element_by_id("com.youba.calculate:id/btn_one").click(); # 1+2= time.sleep(5) driver.find_element_by_id("com.youba.calculate:id/btn_plus").click(); time.sleep(5) driver.find_element_by_id("com.youba.calculate:id/btn_two").click(); time.sleep(5) driver.find_element_by_id("com.youba.calculate:id/btn_equal").click(); time.sleep(5) print(driver.find_element_by_id("com.youba.calculate:id/tv_display").text); print("#------------------------------------------------------------------------------------") print("#------------------------------------------------------------------------------------") print(driver.current_package) print('-------------------------') print(driver.current_activity) print('-------------------------') print(driver.capabilities) print('-------------------------') time.sleep(10); driver.open_notifications() #打开消息通知栏 time.sleep(10) driver.press_keycode(4) #按一下返回键,消息栏隐藏 time.sleep(10) print("#------------------------------------------------------------------------------------") print("#------------------------------------------------------------------------------------") print(driver.is_app_installed("com.ifeng.news2")) # 返回true,说明已经按装了 driver.quit()
执行结果:
3
#------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------
com.youba.calculate
-------------------------
.MainActivity
-------------------------
{'platform': 'LINUX', 'webStorageEnabled': False, 'takesScreenshot': True, 'javascriptEnabled': True, 'databaseEnabled': False, 'networkConnectionEnabled': True, 'locationContextEnabled': False, 'warnings': {}, 'desired': {'platformName': 'Android', 'deviceName': '8DF6R17503000072', 'platformVersion': '8.0.0', 'unicodeKeyboard': True, 'resetKeyboard': True, 'appPackage': 'com.youba.calculate', 'appActivity': 'com.youba.calculate.MainActivity'}, 'platformName': 'Android', 'deviceName': '8DF6R17503000072', 'platformVersion': '8.0.0', 'unicodeKeyboard': True, 'resetKeyboard': True, 'appPackage': 'com.youba.calculate', 'appActivity': 'com.youba.calculate.MainActivity', 'deviceUDID': '8DF6R17503000072', 'deviceApiLevel': 26, 'deviceScreenSize': '1440x2560', 'deviceScreenDensity': 560, 'deviceModel': 'EDI-AL10', 'deviceManufacturer': 'HUAWEI', 'pixelRatio': 3.5, 'statBarHeight': 84, 'viewportRect': {'left': 0, 'top': 84, 'width': 1440, 'height': 2344}}
-------------------------
#------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------
True
============================================================================================
============================================================================================
二、app没有安装,则安装
# 计算器: C:Usersdel>adb shell dumpsys window w |findstr / |findstr name= # mSurface=Surface(name=com.youba.calculate/com.youba.calculate.MainActivity) #adb命令查看包名和启动activity #com.youba.calculate:id/btn_plus + #com.youba.calculate:id/btn_equal = #凤凰网: C:Usersdel>adb shell dumpsys window w |findstr / |findstr name= # mSurface=Surface(name=com.ifeng.news2/com.ifeng.news2.activity.IfengTabMainActivity) from appium import webdriver import time desired_caps = { 'platformName': 'Android', #待测试手机的系统 'deviceName':'8DF6R17503000072', #使用手机或模拟器的类型 'platformVersion': '8.0.0', #待测手机系统版本 "unicodeKeyboard":"true", #支持中文输入,会自动安装unicode输入法,默认是false "resetKeyboard":"true", #如果设置了中文输入为true,此设置为自动化退出后,重置输入法到原来状态 #'app': 'C:\Users\del\Desktop\b\jsq.apk', #手机未按照待测app时,设置安装路径、包名、启动activity 'appPackage': 'com.youba.calculate', 'appActivity': 'com.youba.calculate.MainActivity', } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(5) driver.find_element_by_id("com.youba.calculate:id/btn_one").click(); # 1+2= time.sleep(5) driver.find_element_by_id("com.youba.calculate:id/btn_plus").click(); time.sleep(5) driver.find_element_by_id("com.youba.calculate:id/btn_two").click(); time.sleep(5) driver.find_element_by_id("com.youba.calculate:id/btn_equal").click(); time.sleep(5) print(driver.find_element_by_id("com.youba.calculate:id/tv_display").text); print("#------------------------------------------------------------------------------------") print("#------------------------------------------------------------------------------------") print(driver.current_package) print('-------------------------') print(driver.current_activity) print('-------------------------') print(driver.capabilities) print('-------------------------') time.sleep(10); driver.open_notifications() #打开消息通知栏 time.sleep(10) driver.press_keycode(4) #按一下返回键,消息栏隐藏 time.sleep(10) print("#------------------------------------------------------------------------------------") print("#------------------------------------------------------------------------------------") print(driver.is_app_installed("com.ifeng.news2")) # 返回false,说明没有安装 driver.install_app("C:\Users\del\Desktop\b\ifeng.apk") #按装app time.sleep(30)
driver.press_keycode(3) #按键 home键
driver.quit()