UIAutomator2实现星管家APP自动化登录测试(涵盖多种情况)

6 篇文章 0 订阅
3 篇文章 0 订阅

星管家APP具体功能就不多介绍了,想了解的朋友可以在各大应用市场下载注册体验下~

一、环境:

1.python3
2.框架:UIAutomator2
3.windows or others
4.函数

二、下面说下自动化登录过程及会遇到的各种情况吧:

过程:

1.登录前在线下载星管家app并安装到测试机
2.启动app并输入账号密码
3.输入登录验证码
4.点击app权限申请弹窗
5.完成新手引导过程
6.登录完成

情况:

1.首次登陆时需要提供验证码登录
2.首次登陆需要提供app必须要权限
3.首次登陆需要进行新手引导操作
4.登录账号或者密码错误
5.验证码错误

三、逻辑和判断:

1.本次围绕以上情况展开,涵盖基本能想到的情况编写了登录界面的测试脚本。

代码如下:

import time

import uiautomator2 as ut2

# 连接手机
d = ut2.connect_usb('你的设备ID')

# 测试机亮屏、解锁,其中锁屏为九宫格“Z”形状
d.screen_on()
d.swipe_points([(0.5, 0.9),(0.5, 0.1)],0.05)
d.swipe_points([(0.228, 0.525),(0.5, 0.525),(0.766, 0.525),(0.5, 0.677),(0.228, 0.828),(0.497, 0.828),(0.761, 0.828)],0.05)


'''获取设备信息'''
def deviceInfo():
    device_info=d.info 
    print('测试机机型:',d.device_info['brand'],d.device_info['model'])
    print('当前前台应用包名为:',device_info['currentPackageName'])
    return
deviceInfo()

def size():
    return d.window_size()
print('屏幕分辨率为:',size())

# 安装测试APP

print('正在现在测试app')
d.app_install('https://filesystem-prod.oss-cn-shanghai.aliyuncs.com/downloads/appstore/org/starposmanager.apk')


def launchApp(pkg):
    print('启动测试app')
    d.make_toast('正在启动测试APP,请稍后!',3)
    d.app_start(pkg)
launchApp('com.newland.satrpos.starposmanager')
time.sleep(5)

'''验证码登录'''
def Verification():
    if d(resourceId="com.newland.satrpos.starposmanager:id/get_verification_code"):
        code = input('请输入登录验证码:')
        d(text='请输入手机验证码').set_text(code)
        d(resourceId="com.newland.satrpos.starposmanager:id/next").click()
        time.sleep(2)
        while True:
            if d(text='验证码不正确!'):
                print('验证码错误,请重新输入!')
                code2 = input('请重新输入登录验证码:')
                d(resourceId="com.newland.satrpos.starposmanager:id/verification_code").clear_text()
                d(resourceId="com.newland.satrpos.starposmanager:id/verification_code").set_text(code2)
                d(resourceId="com.newland.satrpos.starposmanager:id/next").click()
                # time.sleep(2)
            else:
                break
    else:
        print('已登录,不需要验证')

'''
输入账号密码并登录
'''
def login(userName,passWord):
    if d(resourceId="com.newland.satrpos.starposmanager:id/userName"):
        # needLogin = 1
        print("未登陆,需要登录")
        d(resourceId="com.newland.satrpos.starposmanager:id/userName").clear_text
        d(resourceId="com.newland.satrpos.starposmanager:id/userName").set_text(userName)
        d(resourceId="com.newland.satrpos.starposmanager:id/password").clear_text
        time.sleep(1)
        d(resourceId="com.newland.satrpos.starposmanager:id/password").set_text(passWord)
        d.press("back")
        d(resourceId="com.newland.satrpos.starposmanager:id/login").click()
        time.sleep(3)
        if d(resourceId="com.newland.satrpos.starposmanager:id/tv_error"):
            print('密码错误,请重新输入密码')
            exit()
        elif d(text=u"手机号") and d(text=u"验证码") and d(resourceId="com.newland.satrpos.starposmanager:id/verification_code"):
            Verification()
        else:
            print('登录成功')
    else:
        print('账号已登录,进入APP')
login('你的账号','你的密码')

'''
判断并运行系统权限弹窗
'''
def permissions():
    n = 0
    while True:
        if d(resourceId="com.lbe.security.miui:id/permission_message"):
            n = n + 1
            flag = 1
            print("正在授予第",n,"项权限")
            d(resourceId="android:id/button1").click(timeout=5)

            time.sleep(0.5)
            if flag ==1:
                continue
            else:
                break
        else:
            flag = 0
            FAQ()
            break           

def FAQ():
    while True:
        if d(resourceId="com.newland.satrpos.starposmanager:id/llt_page_indicator"):
            d.swipe_points([(0.759, 0.65),(0.286, 0.649)],0.05)
            d.click(0.504, 0.773)
        else:
            break   

def judge():
    try:
        if d(text='验证码') and d(text='请输入手机验证码') and d(text=u"手机号"):
            Verification()
        elif d(text='允许') and d(text='拒绝'):
            permissions()
        else:
            print('已进入首页')
    except:
        pass
time.sleep(1)
judge()

choices = str(input('是否需要清除数据(y/n):'))
if choices=='y':
    d.app_clear('com.newland.satrpos.starposmanager')
else:
    print('不清除数据')
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值