Appium封装对日期控件的操作
在app中针对日期控件的常见处理方式
-
如果是第三方库,并且有公开的api接口,就可以直接使用python或者android的uiautomator这个工具直接去调用,从而设置日期的值,避免从界面上操作,效率非常高
-
通过界面方式进行滑动选择操作,这种方式比较通用,没有api也可以直接使用,但比较麻烦,编程和操作效率都比较低
from appium import webdriver
def select_date(year, month):
while True:
curr_year = driver.find_element('xpath', '//*[@bounds="[169,794][259,817]"]').text
curr_month = driver.find_element('xpath', '//*[@bounds="[281,794][371,817]"]').text
if year == int(curr_year) and month == int(curr_month):
break
# 移动年份
if year > int(curr_year):
driver.swipe(210, 830, 210, 780, duration=1)
elif year < int(curr_year):
driver.swipe(210, 780, 210, 830, duration=1)
# 移动月份
if month > int(curr_month):
driver.swipe(325, 830, 325, 780, duration=1)
elif month < int(curr_month):
driver.swipe(325, 780, 325, 830, duration=1)
settings_dict = {}
settings_dict['platformName'] = 'Android' # 必须参数,定义被测脚本,不区分大小写, 必须是android
settings_dict['platformVersion'] = '6.0.1' # 设置被测手机的android版本号
settings_dict['deviceName'] = '127.0.0.1:7555' # 可以写任意值,但不能为空
settings_dict['appPackage'] = 'com.laogejizhang.account' # 必须参数,指定被测软件的包名
settings_dict['appActivity'] = '.MainActivity' # 指定打开的app的页面是哪个
settings_dict['automationName'] = 'Uiautomator2' # 不是必须, 但一般需要指定
settings_dict['noReset'] = True # 不停止应用, 不清空数据,不卸载apk False 会清除应用数据 但不会卸载apk
settings_dict['newCommandTimeout'] = 6000
# 设置中文输入
settings_dict['unicodeKeyboard'] = True
settings_dict['resetKeyboard'] = True
driver = webdriver.Remote('http://localhost:4723/wd/hub', settings_dict)
driver.implicitly_wait(10)
driver.find_element('xpath', '//*[@text="流水"]').click()
driver.find_element('xpath', '//*[@bounds="[206,34][312,62]"]').click()
select_date(2018, 8)