一.appium基本介绍 1.搭建移动端自动化环境,具体搭建环境可以百度 a.安装jdk并配置环境变量 b.安装安装sdk并配置环境变量 c.安装node.js d.安装appium e.安装夜神模拟器 2.查看被测app包信息 1.冷启动时,windows系统输入adb logcat | findstr START
2.通过aapt命令查看:aapt dump badging +安装包路径 3.appium配置启动
4.启动appium服务后,运行代码会在模拟器或手机上运行打开QQ
from appium import webdriver from appium.webdriver.common.appiumbyimport AppiumBy # 应用相关配置 desired_capabilities = { "platformName": "Android", # 测试手机类型 "plathformVersion": "7.1.2", # 手机版本号 "deviceName": "127.0.0.1:62001 device", # 手机唯一id,设备号 "appPackage": "com.tencent.mobileqq", # app包名 "appActivity": "com.tencent.mobileqq.activity.SplashActivity", # app入口 "autoAcceptAlerts": "true", # "noReset":True # } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_capabilities)
# # id方式定位元素 el4 = driver.find_element('id',"com.tencent.mobileqq:id/dialogRightBtn") el4.click()
import time time.sleep(45) # id 方式定位元素 # el1 = driver.find_element('id',"com.tencent.mobileqq:id/btn_login") # el1.click() # xpath定位元素 # driver.find_element(AppiumBy.XPATH,"//android.widget.Button[@text='登录']").click() driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().text("登录")').click() # accessibility_id 定位元素 driver.find_element(AppiumBy.ACCESSIBILITY_ID,"请输入QQ号码或手机或邮箱").send_keys('75146075') driver.find_element(AppiumBy.ACCESSIBILITY_ID,"密码 安全").send_keys('Z09115') driver.find_element(AppiumBy.ACCESSIBILITY_ID,"登 录").click() time.sleep(2) driver.find_element(AppiumBy.ID,"com.tencent.mobileqq:id/dialogRightBtn").click() time.sleep(1) driver.find_element(AppiumBy.ID,"com.android.packageinstaller:id/permission_allow_button").click() time.sleep(1) driver.find_element(AppiumBy.ID,"com.android.packageinstaller:id/permission_allow_button").click()
二.appium元素定位
1.appium客户端元素定位 # id 方式定位元素 # el1 = driver.find_element('id',"com.tencent.mobileqq:id/btn_login") # el1.click() # xpath定位元素 # driver.find_element(AppiumBy.XPATH,"//android.widget.Button[@text='登录']").click()
# accessibility_id 定位元素 driver.find_element(AppiumBy.ACCESSIBILITY_ID,"请输入QQ号码或手机或邮箱").send_keys('7562075')
# UI Automator定位元素 # 全部匹配text文字 driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().text("登录")').click() # 包含text文字 driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().textContains("登录")').click() # 以text什么开始 driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().textStartsWith("登")') # 正则匹配text driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().textMatches("^登.*")') # className driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().className("android.widget.Button")') # className正则匹配 driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().classNameMatches("^android.widget.*")') # resource-id driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().resourceId("com.tencent.mobileqq:id/btn_login")') # description driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().description("请输入QQ号码或手机或邮箱")')2.UI Automator元素定位 在D:\zogchn\android-sdk_r24.4.1-windows\android-sdk-windows\tools目录下启动UI Automator
3.页面滑动 from appium import webdriver from appium.webdriver.common.appiumbyimport AppiumBy desired_capabilities = { "platformName": "Android", # 测试手机类型 "plathformVersion": "7.1.2", # 手机版本号 "deviceName": "127.0.0.1:62001 device", # 手机唯一id,设备号 "appPackage": "com.android.settings", # app包名 "appActivity": ".Settings", # app入口 "autoAcceptAlerts": "true", # "noReset":True # } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_capabilities) # 根据页面尺寸进行滑动 size=driver.get_window_size() print(size) # 向下滑动 x=size['width']*0.5 start_y=size['height']*0.8 end_y=size['height']*0.1 driver.swipe(x,start_y,x,end_y) import time time.sleep(5) # 向上滑动 start_y=size['height']*0.1 end_y=size['height']*0.8 driver.swipe(x,start_y,x,end_y)
4.appium基本操作 from appium import webdriver from appium.webdriver.common.appiumbyimport AppiumBy desired_capabilities = { "platformName": "Android", # 测试手机类型 "plathformVersion": "7.1.2", # 手机版本号 # "deviceName": "127.0.0.1:62001 device", # 手机唯一id,设备号 # "appPackage": "com.android.settings", # app包名 # "appActivity": ".Settings", # app入口 "appPackage": "com.android.browser", # app包名 "appActivity": ".BrowserActivity", # app入口 "autoAcceptAlerts": "true", # "noReset":True # } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_capabilities) """操作原生Native App""" # 返回是否安装了对应包名的App result=driver.is_app_installed('com.android.settings') print(result) # # 关闭初始化的app,相当于退到后台 driver.close_app() # 启动app driver.launch_app() # 退到后台3秒启动app driver.background_app(3) # 安装app,参数为安装包绝对路径 driver.install_app("") # 卸载app,参数为包名 driver.remove_app("") r=driver.current_activity print("获取当前app入口",r) # # 启动一个新的app driver.start_activity("com.tencent.mobileqq","com.tencent.mobileqq.activity.SplashActivity") r=driver.current_activity print("调用获取当前app入口",r) # 重置 driver.reset() import time time.sleep(20) driver.find_element("id","android:id/button1").click() ele=driver.find_element("id","com.android.browser:id/url").click() driver.find_element("id","com.android.browser:id/url").set_value("http://www.baidu.com") driver.keyevent(66) time.sleep(5) """原生Native切换至webview """ # 获取当前的context current_context=driver.current_context print("当前的",current_context) # 所有的contexts contexts=driver.contexts print("所有的",contexts) # 切换 driver.switch_to.context("WEBVIEW_com.android.browser") driver.find_element("id","index-kw").set_value("python") driver.keyevent(66) # 获取当前手机状态 print(driver.network_connection) from appium.webdriver.connectiontype import ConnectionType # 设置成飞行模式 driver.set_network_connection(ConnectionType.DATA_ONLY) # 是否启动输入法 print("是否启动输入法",driver.is_ime_active()) # 返回安卓可用的输入法 print(driver.available_ime_engines) # 返回当前输入法的包名 print(driver.active_ime_engine,"当前输入法包名") # 切换输入法 # driver.activate_ime_engine(driver.available_ime_engines[-1]) # 关闭当前输入法 # driver.deactivate_ime_engine() # 锁定手机多少秒 driver.lock(10) # 摇手机 driver.shake() # 打开通知栏 driver.open_notifications() # 获取手机的当前时间 t=driver.device_time # 开启或关闭手机定位服务 driver.toggle_location_services()
# 长按操作
from appium.webdriver.common.touch_action import TouchAction
el=driver.find_element(AppiumBy.id,"")TouchAction(driver).long_press(el).perform()
time.sleep(3)
标签:Appium,AppiumBy,app,driver,find,UI,自动化,element,id From: https://www.cnblogs.com/zongchen/p/17284926.html