Android自動化測試中如何處理各種彈窗


在UI自動化測試中彈窗是影響自動化用例穩定性的一大因素,如何方便快捷的處理各種情況下的彈窗,是搞UI自動化測試必須要面臨的問題.

彈窗的種類:

安裝APP時的系統彈窗
此類彈窗一般有兩種,一種是自動化測試框初始化本身也需要安裝一些APP,比如uiautomator2會安裝atx-agent、com.github.uiautomator,這些彈窗在初始化環境的時候可以手動點掉,case里不需要關注。另一種就是安裝我們的被測app,像下面這種

都是我們不得不去處理的,不然,自動化也就是不自動了。
APP啟動時的權限彈窗

file

這類彈窗是APP在啟動時會申請一些基礎的權限

APP內的業務彈窗

彈窗處理

本文使用的是uiautomator2這個自動化框架,它提供了一種watcher對象,可以用來配置要監控的元素,這里我們配置要監控的就是頁面上的彈窗,下面來看看具體怎么做。

watcher的使用

# 常用寫法,注冊匿名監控
d.watcher.when("安裝").click()

# 注冊名為ANR的監控,當出現ANR和Force Close時,點擊Force Close
d.watcher("ANR").when(xpath="ANR").when("Force Close").click()

# 其他回調例子
d.watcher.when("搶紅包").press("back")
d.watcher.when("//*[@text = 'Out of memory']").call(lambda d: d.shell('am force-stop com.im.qq'))

# 回調說明
def click_callback(d: u2.Device):
    d.xpath("確定").click() # 在回調中調用不會再次觸發watcher

d.xpath("繼續").click() # 使用d.xpath檢查元素的時候,會觸發watcher(目前最多觸發5次

# 移除ANR的監控
d.watcher.remove("ANR")
# 移除所有的監控
d.watcher.remove()
# 開始后台監控
d.watcher.start()
d.watcher.start(2.0) # 默認監控間隔2.0s
# 強制運行所有監控
d.watcher.run()
# 停止監控
d.watcher.stop()
# 停止並移除所有的監控,常用於初始化
d.watcher.reset()

上面是watcher的一些常用api以及解釋,來源於github。嘻嘻,自己懶的寫了。

實戰案例

下面我們用B站apk為例,處理從安裝到登錄后的一系列彈窗。

import uiautomator2 as u2
import os
import time

base_dir = os.path.dirname(__file__)
apk_path = os.path.join(base_dir, 'apks/bilibili.apk')

d = u2.connect_usb(serial='MDX0220924018819')

# 從安裝到登錄成功后,可能會出現的彈窗,在這里進行注冊,這個是華為手機出現的彈窗類型
d.watcher.when('繼續安裝').click()
d.watcher.when('完成').click()
d.watcher.when('同意並繼續').click()
d.watcher.when("我知道了").click()
d.watcher.start()

d.app_install(apk_path)

d.app_start('tv.danmaku.bili')

d(text='我的').click()
time.sleep(3)
if d(resourceId="tv.danmaku.bili:id/btn_change_account").exists:
    d(resourceId="tv.danmaku.bili:id/btn_change_account").click()
else:
    d(resourceId="tv.danmaku.bili:id/tv_login").click()
time.sleep(3)
d(resourceId="tv.danmaku.bili:id/username").set_text('xxxxxxxxx')

d(resourceId="tv.danmaku.bili:id/userpwd").set_text('xxxxxxxx')

d(resourceId="tv.danmaku.bili:id/log_reg_checkbox").click()

time.sleep(2)
d(resourceId="tv.danmaku.bili:id/btn_login").click()
d(text='首頁').click()

彈窗處理的核心思想是,起一個線程,不停的監聽頁面上有沒有彈窗出現,出現了就點擊,或點擊取消或點擊確認等等。

uiautomator2處理彈窗的核心思想

采用了后台運行了一個線程的方法(依賴threading庫),然后每隔一段時間dump一次hierarchy,匹配到元素之后執行相應的操作。

class Watcher():
    def __init__(self, d: "uiautomator2.Device"):
        self._d = d
        self._watchers = []

        self._watch_stop_event = threading.Event()
        self._watch_stopped = threading.Event()
        self._watching = False  # func start is calling
        self._triggering = False

        self.logger = setup_logger()
        self.logger.setLevel(logging.INFO)
       
   def when(self, xpath=None):
     return XPathWatcher(self, xpath)

Watcher對象個self._watchers 屬性來維護所有要監控的元素,d.watcher.when('繼續安裝')當我們調用when方法后會返回一個XPathWatcher對象,然后再調用這個對象的click方法實現對監控元素的操作。

class XPathWatcher():
    def __init__(self, parent: Watcher, xpath: str, name: str = ''):
        self._name = name
        self._parent = parent
        self._xpath_list = [xpath] if xpath else []

    def when(self, xpath=None):
        self._xpath_list.append(xpath)
        return self

    def call(self, func):
        """
        func accept argument, key(d, el)
        d=self._d, el=element
        """
        self._parent._watchers.append({
            "name": self._name,
            "xpaths": self._xpath_list,
            "callback": func,
        })

    def click(self):
        def _inner_click(selector):
            selector.get_last_match().click()

        self.call(_inner_click)

click方法就是將點擊的操作放到回調函數,然后調用XPathWatcher對象的call方法,這個方法會生成一個監控規則,並將監控規則放到我們前面提到的Watcher對象的self._watchers 屬性。

def start(self, interval: float = 2.0):
    """ stop watcher """
    if self._watching:
        self.logger.warning("already started")
        return
    self._watching = True
    th = threading.Thread(name="watcher",
                          target=self._watch_forever,
                          args=(interval, ))
    th.daemon = True
    th.start()
    return th

再然后調用Watcher對象的的start方法,開啟一個線程,按照指定間隔時間從頁面dump信息,查看是否有要監控的元素,找到后調用回調函數。

以上是我們關於彈窗處理的一些操作,但是有沒有發現,上面實戰哪里寫的是有問題,難道每一次有新的彈窗都要在這里寫一行代碼么,還有是不是能適配不同機型呢?

下片文章會對這里進行一下重構以及如何兼容不同機型,敬請期待。

歡迎大家去 我的博客 瞅瞅,里面有更多關於測試實戰的內容哦!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM