前言
本次我們將會對 uiautomator2 的一些基本操作進行簡單的封裝,以便更好的應用到UI自動化中。
重復多次滑動
在 uiautomator2 中,給我們提供了一些滑動的操作 swipe()
,以及滑動擴展的操作 swipe_ext()
,基於此我們可以對重復多次的滑動操作進行簡單封裝。
def up(self, scale=0.9, times=1, duration=1.0, **kwargs):
"""
上滑操作
:param scale: 滑動單位,默認0.9個單位
:param times: 滑動次數,默認1次
:param duration: 滑動時間,默認1.0秒
:return:
"""
for i in range(times):
self.d.swipe_ext("up", scale, duration=duration, **kwargs)
def down(self, scale=0.9, times=1, duration=1.0, **kwargs):
"""
下滑操作
:param scale: 滑動單位,默認0.9個單位
:param times: 滑動次數,默認1次
:param duration: 滑動時間,默認1.0秒
:return:
"""
for i in range(times):
self.d.swipe_ext("down", scale, duration=duration, **kwargs)
def left(self, scale=0.9, times=1, duration=1.0, **kwargs):
"""
左滑操作
:param scale: 滑動單位,默認0.9個單位
:param times: 滑動次數,默認1次
:param duration: 滑動時間,默認1.0秒
:return:
"""
for i in range(times):
self.d.swipe_ext("left", scale, duration=duration, **kwargs)
def right(self, scale=0.9, times=1, duration=1.0, **kwargs):
"""
右滑操作
:param scale: 滑動單位,默認0.9個單位
:param times: 滑動次數,默認1次
:param duration: 滑動時間,默認1.0秒
:return:
"""
for i in range(times):
self.d.swipe_ext("right", scale, duration=duration, **kwargs)
間隔等待元素
我們在做UI自動化時,如果需要在進入某個APP頁面后點擊元素,那么一般先會等待一段時間,然后再進行點擊操作,但是在這個過程中,到底等待多長時間才比較合適,這個就不太好判斷。
因此,我們可以用另一種思路來實現:
- 檢查定位元素是否存在
- 如果定位失敗就間隔一段時間后重試
- 循環以上操作,直到元素定位成功就點擊,或超時拋出異常
def wait_until_element_found(self, param, timeout=30.0, retry_interval=2, wait_after_found=0.0):
"""
定位元素,如果不存在就間隔若干秒后重試,直到元素定位成功或超時
:param param: xpath字符串 或 元素對象
:param timeout: 超時, 默認30秒
:param retry_interval: 間隔時間, 默認2秒
:param wait_after_found: 找到元素后,原地等待時間
:return:
"""
element = self.d.xpath(param) if isinstance(param, str) else param
max_time = time.time() + timeout
while True:
try:
assert element.exists
if wait_after_found:
print("Element found,then sleep {} seconds".format(wait_after_found))
time.sleep(wait_after_found)
return element
except AssertionError:
param = param if isinstance(param, str) else param.selector
print("Element 【 {} 】 Not found, Retry...".format(param))
if time.time() > max_time > 0:
raise AssertionError("Element 【 {} 】 located failed after {} timeout".format(param, timeout))
time.sleep(retry_interval)
def wait_for_click(self, param, wait_after_click=0.0, **kwargs):
"""
判斷UI元素是否存在, 不存在則等待UI元素在一定時間內出現,再進行點擊
:param param: xpath字符串 或 元素對象
:param wait_after_click: 點擊后等待時間
:return:
"""
element = self.wait_until_element_found(param, **kwargs)
element.click()
if wait_after_click:
print("Element found and click,then sleep {} seconds".format(wait_after_click))
time.sleep(wait_after_click)
重復多次點擊
在 uiautomator2 中,給我們提供了一些點擊的操作,如 單擊click()
,雙擊double_click()
,長按long_click()
等,基於此我們可以對重復多次的點擊操作進行簡單封裝。
def repeat_click(self, param, times, wait_after_repeat_click=0.0):
"""
重復多次點擊UI元素
:param param: xpath字符串 或 元素對象
:param times: 點擊次數
:param wait_after_repeat_click: 重復點擊后等待時間,默認為0.0
:return:
"""
element = self.wait_until_element_found(param)
for i in range(times):
element.click()
if wait_after_repeat_click:
print("Element click,then sleep {} seconds".format(wait_after_repeat_click))
time.sleep(wait_after_repeat_click)
滑動查找元素
我們在做UI自動化時,有時需要進行多次上滑操作,比如我進入某個APP頁面,需要定位是否某個元素,而這個元素可能位於頁面中間部分,也可能位於頁面最底部,需滑到頁面底部時才出現,又或者頁面上不存在該元素,在整個過程中,我們需在滑動的過程中對元素進行定位。
因此,我們可以用以下思路來實現:
- 檢查定位元素是否存在
- 如果定位失敗就進行一次滑動,滑動后重試
- 循環以上操作,直到滑動到頁面底部,若該過程中元素定位成功就點擊
def swipe_until_element_found(self, param, wait_after_found=0.0, **kwargs):
"""
檢查元素是否存在,若不存在則進行上滑,滑動后再次檢查,直到滑動到頁面底部
若找到元素則返回,否則滑動到頁面底部后,仍未找到元素,則拋出異常,提示找不到元素
:param param: xpath字符串 或 元素對象
:param wait_after_found: 找到元素后,原地等待時間
:param kwargs:
:return:
"""
element = self.d.xpath(param) if isinstance(param, str) else param
param = param if isinstance(param, str) else param.selector
while True:
try:
assert element.exists
if wait_after_found:
print("Element found,sleep {} seconds".format(wait_after_found))
time.sleep(wait_after_found)
return element
except AssertionError:
print("Element 【 {} 】 Not found, Continue to swipe up...".format(param))
# 獲取滑動前頁面下半部分的所有元素
page_content = self.d.dump_hierarchy()[(len(self.d.dump_hierarchy()) // 2):]
self.up(**kwargs)
time.sleep(0.5)
# 獲取滑動后頁面下半部分的所有元素,並與上一次滑動前的頁面元素對比,頁面元素沒有變化時跳出循環
if self.d.dump_hierarchy()[(len(self.d.dump_hierarchy()) // 2):] == page_content:
break
if not element.exists:
raise AssertionError("Element 【 {} 】 located failed in this page".format(param))
def swipe_for_click(self, param, wait_after_click=0.0, **kwargs):
"""
判斷UI元素是否存在, 不存在則持續向上滑動到底部,直到UI元素在頁面內出現,再進行點擊
:param param: xpath字符串 或 元素對象
:param wait_after_click: 點擊后等待時間
:return:
"""
element = self.swipe_until_element_found(param, **kwargs)
element.click()
if wait_after_click:
print("Element found and click,then sleep {} seconds".format(wait_after_click))
time.sleep(wait_after_click)