公开课-10行代码轻松实现app自动化测试

参考链接

github 地址:GitHub - openatx/uiautomator2: Android Uiautomator2 Python Wrapper

大纲

uiautomator2 介绍

uiautomator2 常用命令

python3 -m uiautomator2   帮助文档
uiautomator2   version 获取版本号
uiautomator2   init 做一些初始化的操作,初始化手机设备
uiautomator2 current  获取当前的包名和activity的名字
uiautomator2 install   路径/apk包名     安装
uiautomator2 uninstall  package名      卸载
uiautomator2 start "com.tencent.wework"   启动应用
uiautomator2 stop "com.tencent.wework"   停止 应用
Uiautomator2 purge  卸载初始化安装的应用,

uiautomator2 常用api

uiautomator2 实战

"""
__author__ = 'hogwarts_xixi'
__time__ = '2021/5/10 3:29 下午'
"""
from time import sleep

import uiautomator2

class TestXueQiu:
    def setup(self):
        uiautomator2.
        self.d = uiautomator2.connect()
        self.package = "com.xueqiu.android"
        # activity= ".launch.LaunchSplashActivity"
        self.activity = "com.xueqiu.android.common.MainActivity"
        # 调用app_start方法启动应用
        self.d.app_start(self.package, self.activity, wait=True, stop=True)
        # sleep(2)
        # 隐式等待
        self.d.implicitly_wait(10)

    def teardown(self):
        self.d.app_stop(self.package)

    def test_search(self):
        # 点击搜索框
        self.d(resourceId='com.xueqiu.android:id/tv_search').click()
        search_key = "阿里巴巴"
        # 输入搜索内容
        self.d(resourceId='com.xueqiu.android:id/search_input_text').send_keys(search_key)
        # 点击搜索结果中的【BABA】
        self.d(text='BABA').click()
        # 点击【加关注】
        self.d(resourceId='com.xueqiu.android:id/add_attention').click()
        # 获取 Toast
        self.d.toast.get_message(10, 1, "添加成功")
#
# d = uiautomator2.connect()
# # print(d.device_info)
# # print(d.window_size())
#
# # d.app_install("/Users/juanxu/Downloads/weixin706android1460.apk")
# # d.app_uninstall()
# # d.current_app
# # 通过uiautomator2 current 获取到 包名和页面名
# # package= "com.tencent.wework"
# package= "com.xueqiu.android"
# # activity= ".launch.LaunchSplashActivity"
# activity= "com.xueqiu.android.common.MainActivity"
# # 调用app_start方法启动应用
# d.app_start(package,activity,wait=True,stop=True)
# # sleep(2)
# # 隐式等待
# d.implicitly_wait(10)
# # 点击搜索框
# d(resourceId='com.xueqiu.android:id/tv_search').click()
# search_key = "阿里巴巴"
# # 输入搜索内容
# d(resourceId='com.xueqiu.android:id/search_input_text').send_keys(search_key)
# # 点击搜索结果中的【BABA】
# d(text='BABA').click()
# # 点击【加关注】
# d(resourceId='com.xueqiu.android:id/add_attention').click()
# # 获取 Toast
# d.toast.get_message(10,1,"添加成功")
#
# # 停止应用
# d.app_stop(package)
关闭