Appium--app自动化常用Api汇总
一、动作事件
# 先定位元素
ele = driver.find_element_by_id('ccn.xiaochuankeji.tieba:id/title')
# 点击事件
ele.click()
# 文本输入事件
ele.sendkeys()
# 清空文本数据
ele.clear()
二、属性获取
# 获取当前设备的屏幕分辨率[页面滑动会使用相关]
height = driver.get_window_size()['height'] # 获取屏幕高度
width = driver.get_window_size()['width'] # 获取屏幕的宽度
print('该手机的屏幕分辨率为:{}*{}'.format(height,width))
# 先定位元素
ele = driver.find_element_by_id('ccn.xiaochuankeji.tieba:id/title')
# 获取控件文本信息
text = ele.text
# 获取当前窗口的Activity名
activity = driver.current_activity
# 获取控件的属性值
attibute = ele.get_attribute('className')
# 判断控件是否可用
ele.is_enabled()
# 判断控件是否显示
ele.is_displayed()
三、swipe事件
'''
# 页面滑动
driver.swipe(startX,startY,endX,endY,time)
在持续时间内,完成起始坐标--终止坐标的移动操作
'''
# 【起始、终止坐标不变】实现控件点击操作
driver.swipe(500,200,500,200,100)
# 实现【短时】点击操作
driver.swipe(500,1000,500,200,100)
# 实现点击长按操作
driver.swipe(500,1000,500,200,3000)
'''
适应于各个分辨率手机的swipe操作事件
'''
# 获取当前设备的屏幕分辨率
height = driver.get_window_size()['height'] # 获取屏幕高度
width = driver.get_window_size()['width'] # 获取屏幕的宽度
# 通过控件:bounds属性值/分辨率=百分比
driver.swipe(width*0.5,height*0.8,width*0.5,height*0.5)
四、TouchAction类详解
# 先导入TouchAction类【触摸事件】
from appium.webdriver.common.touch_action import TouchAction
# 使用.tap()方法点击,再使用perform()方法释放动作
# 控件点击
ele = driver.find_element_by_id('ccn.xiaochuankeji.tieba:id/title')
TouchAction(driver).tap(ele).perform()
# 坐标点击
TouchAction(driver).tap(x=200,y=500).perform()
# press + release 实现点击事件
TouchAction(driver).press(x=500,y=300).release().perform()
# long_press + release 实现长按点击事件
TouchAction(driver).long_press(x=500,y=300,duration=3000).release().perform()
五、keyevent手机按键事件
'''
五、keyevent手机按键事件
参考博客:https://blog.csdn.net/midux/article/details/80064054
常用手机按键编号:
返回键:4
搜索键:84
回车键:66
退格键:67
'''
driver.keyevent(4)
driver.keyevent(66)
六、屏幕截图
driver.get_screenshot_as_file("E:/new_file/app.png")
七、输入框输入中文【需切换appium输入法】
# 给模拟器加各appium自带输入法
desired_caps['unicodeKeyboard'] = 'True'
# 重置当前输入法
desired_caps['resetKeyboard'] = 'True'
八、每次执行代码后卸载重装APP
'''
使用此项需定义PATH
'''
PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p))
# 每一次执行完app,卸载app,清除所有的数据【如:已登录状态】
desired_caps['fullReset'] = True
# 每一次卸载后去文件绝对路径找到apk文件进行安装
desired_caps['app'] = PATH('E:/newapp/app.apk')
九、toast事件
# 导入显式等待模块
import time
# 导入隐式等待模块
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# 如需要操作toast控件【一闪而过的,如:登陆成功提示】
desired_caps['automationName'] = 'Uiautomator2'
# 最大等待时间为10秒,每0.5秒找一次,直到元素没有出现
ele = WebDriverWait(driver,10,0.5).until_not(
EC.presence_of_element_located(('xpath','//*contains(@text,"登陆成功")')))
print(ele.text)
版权声明:本文为J_____Q原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。