温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android自动化测试如何处理各种弹窗

发布时间:2022-03-30 10:48:35 来源:亿速云 阅读:699 作者:iii 栏目:移动开发

今天小编给大家分享一下Android自动化测试如何处理各种弹窗的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

弹窗的种类:

安装APP时的系统弹窗 此类弹窗一般有两种,一种是自动化测试框初始化本身也需要安装一些APP,比如uiautomator2会安装atx-agent、com.github.uiautomator,这些弹窗在初始化环境的时候可以手动点掉,case里不需要关注。另一种就是安装我们的被测app,像下面这种

都是我们不得不去处理的,不然,自动化也就是不自动了。 APP启动时的权限弹窗

Android自动化测试如何处理各种弹窗

这类弹窗是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信息,查看是否有要监控的元素,找到后调用回调函数。

以上就是“Android自动化测试如何处理各种弹窗”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI