Excel驱动数据 - operate_method详细说明
click - 点击事件,location_type、location_value为必填字段,operate_value为空
input - 清除输入框文本并输入事件,location_type、location_value、operate_value为必填字段,其中operate_value填写输入内容
wait - 强制等待事件,location_type、location_value为空,operate_value(非必填项)填数字,如不填默认等待时间为1s,否则按照填写时间执行
imp_wait - 隐式等待,location_type、location_value为空,operate_value(非必填项)填数字,如不填默认等待时间为5s,否则按照填写时间执行
close_allow_box - 关闭权限弹框事件,location_type、location_value为空,operate_value(非必填项)填写格式为:权限button内容 关闭次数,如不填默认匹配允许字符,关闭次数为1次
swipe_by_ratio - 滑动事件,location_type、location_value为空,operate_value(非必填项)填写格式:开始坐标 方向 滑动比例 持续时间(毫秒),如不填默认start_x=500, start_y=800, direction='up', ratio=0.1, duration=200执行
text - 获取元素文本,location_type、location_value为空,operate_value(非必填项),通常用于check方法下,获得的元素文本与operate_value内容作对比
screenshot - 截图事件,location_type、location_value为空,operate_value(非必填项)填写内容为截图文件名,如不填默认为screenshot+当前时间戳
quit - 退出时间,一般用于运行结束,关闭app
is_login - 判断是否登录,如果是则退出,回到登录页,如果不是则直接进入登录页,location_type、location_value为空,operate_value(非必填项),填写内容为:登录标志字段 设置按钮的定位方式 设置按钮的定位值,如:登录 id com.aspirecn.xiaoxuntongParent.ln:id/me_setting
is_login方法源码 - BaseOperate.py
def is_login(self, login_text='登录', set_loc_type='id', set_loc='com.aspirecn.xiaoxuntongTeacher.ln:id/me_setting'):
"""
判断是否为登录状态,是退出,否进入登录页
:param login_text: 判断是否登录的标志字段
:param set_loc_type: 设置元素定位方式
:param set_loc: 设置元素定位
:return:
"""
wo_loc = "//*[@text='我']"
login_text_loc = "//*[contains(@text, '%s')]" % login_text
wo_el = self.find_element('xpath', wo_loc)
self.to_click(wo_el[1]) if wo_el[0] else logger.info("切换'我'定位失败,请检查xpath是否正确:%s" % wo_loc)
if self.find_element('xpath', login_text_loc)[0]:
logger.info('the app is logout status')
self.to_click(self.find_element('xpath', login_text_loc)[1])
else:
set_el = self.find_element(set_loc_type, set_loc)
self.to_click(set_el[1]) if set_el[0] else logger.info("'设置'定位失败,请检查%s是否正确:%s" % (set_loc_type, wo_loc))
logout_el = self.find_element('xpath', "//*[contains(@text, '退出')]")
self.to_click(logout_el[1]) if logout_el[0] else logger.info("'退出'定位失败,请检查xpath是否正确:%s" % "//*[contains(@text, '退出')]")
swipe_by_ratio方法源码 - BaseOperate.py
def swipe_by_ratio(self, start_x=500, start_y=800, direction='up', ratio=0.1, duration=200):
"""
The function of swipe by ratio(按比例滑动屏幕)
:param start_x: start abscissa
:param start_y: start ordinate
:param direction: The direction of swipe, support 'up', 'down', 'left', 'right'
:param ratio: The screen ration's distance of swiping
:param duration: continue time, unit ms
:return:
"""
self.force_wait(3)
direction_tuple = ('up', 'down', 'left', 'right')
if direction not in direction_tuple:
logger.info('This swiping direction is not support')
width, height = self.get_screen_size()
def swipe_up():
"""
swipe to up
:return:
"""
end_y = start_y - height * ratio
if end_y < 0:
logger.info('the distance of swiping too much')
else:
self.driver.swipe(start_x, start_y, start_x, end_y, duration)
logger.info('from (%s, %s) swiping up to (%s, %s), during %sms' % (start_x, start_y, start_x, end_y, duration))
def swipe_down():
"""
swipe to down
:return:
"""
end_y = start_y + height * ratio
if end_y > height:
logger.info('the distance of swiping too much')
else:
self.driver.swipe(start_x, start_y, start_x, end_y, duration)
logger.info('from (%s, %s) swiping down to (%s, %s), during %sms' % (start_x, start_y, start_x, end_y, duration))
def swipe_left():
"""
swipe to left
:return:
"""
end_x = start_x - width * ratio
if end_x < 0:
logger.info('the distance of swiping too much')
else:
self.driver.swipe(start_x, start_y, end_x, start_y, duration)
logger.info('from (%s, %s) swiping left to (%s, %s), during %sms' % (start_x, start_y, end_x, start_y, duration))
def swipe_right():
"""
swipe to right
:return:
"""
end_x = start_x + width * ratio
if end_x > width:
logger.info('the distance of swiping too much')
else:
self.driver.swipe(start_x, start_y, end_x, start_y, duration)
logger.info('from (%s, %s) swiping right to (%s, %s), during %sms' % (start_x, start_y, end_x, start_y, duration))
swipe_dict = {
'up': swipe_up,
'down': swipe_down,
'left': swipe_left,
'right': swipe_right
}
# 【错误】,以下写法逻辑有问题,即在初始化字典时候就已经执行了所有滑动方法,最后还要返回的是swipe_dict[direction]函数
# swipe_dict = {'up': swipe_up(),'down': swipe_down(),...}
# return swipe_dict[direction]
# 【正确】,字典中只初始化方向对应的函数,return返回具体的滑动函数调用
return swipe_dict[direction]()