Python + Android + Uiautomator 测试样例

环境:

Python3 + Android + Uiautomator1

unittest单元测试框架:见:https://www.cnblogs.com/yufeihlf/p/5707929.html

 

 

一、启动 app startapp.py

# -*- coding:utf-8 -*-
import sys
#reload(sys)
#sys.setdefaultencoding("utf-8")
# author:Peng Ji time:2018/5/11
import os #导入系统命令模块,用于调用adb命令来启动指定APP

import time #导入时间模块,用于调用时间等待
#import automail #这里是导入我打包的自动发送mail脚本
import uiautomator #导入uiautomator模块
import urllib
import importlib
importlib.reload(sys)
from uiautomator import device as d #导入uiautomator模块中的device方法命名为d
 
Phone=d.info #d.info会返会设备信息,需要注意的是,返回的是字典型
print(Phone) #在屏幕上打印出设备信息
h=Phone['displayHeight'] #这里是从设备信息中获取屏幕的尺寸
w=Phone['displayWidth']
 
def devicesInfo():
    print(u"获取设备信息")
    Phone=d.info #d.info会返会设备信息,需要注意的是,返回的是字典型
    print(Phone) #在屏幕上打印出设备信息  
    
try:
    devicesInfo()#这里是捕获各种异常,出现异常会提示 "获取设备信息失败,请检查设备连接"
except:
    print("获取设备信息失败,请检查设备连接")
    
def LaunchAPP():  
    print('启动APP')
    os.system("adb shell monkey -p com.oneteam.theaccompanying -c android.intent.category.LAUNCHER 1") 
    #这里是调用cmd adb shell 启动指定的APP ,这里的包名可以从uiautomatorviewer 中看到
    if d(resourceId="com.oneteam.theaccompanying:id/textView_password").wait.exists(timeout=20000):
        #这里时判断APP首页中指定元素是否出现
        print("APP启动成功")
    else:
        print("APP首页搜索框加载时间超过20S")
try:
    LaunchAPP()
except:
    print("APP启动失败")

测试脚本:
 

from uiautomator import device as d
import time
import sys
import random
import unittest
import HTMLTestRunner

#reload(sys)
#sys.setdefaultencoding("utf-8")
import importlib
importlib.reload(sys)

class My_Test_Suite(unittest.TestCase):
    def setUp(self):
        print (u"--------------初始化工作") 

    # 测试注册
    def test_reg(self):
        print (u"--------------测试注册") 
        
    # ........ 
    # 测试登陆
    def test_login(self):
        print (u"test_login  ")
        try:
            #d(text="登录").click()
            d(resourceId="com.oneteam.theaccompanying:id/textView_forget").clear_text()
            d(resourceId="com.oneteam.theaccompanying:id/textView_forget").set_text('189xxxxxx')
            d(resourceId="com.oneteam.theaccompanying:id/textView_password").set_text("123456")
            d(text="登录").click()
           # d(text="请输入您的姓名").set_text("123456")
          #  d(text="完成").click()
            time.sleep(2)
           # if d(text="签到").exists:
            print (u"登录成功")
               
        except Exception as e:
            print (u"Error: 登录失败\n", e)

    # 测试忘记密码
    def test_forget_password(self):
        try:
            pass   # 一些测试步骤
        except Exception as e:
            print (u"Error: 重置密码or修改密码失败\n", e)

    #......更多的测试模块用例

    def tearDown(self):
        try:
            if d(text="我的").exists:
                d(text="我的").click()
                d(text="退出").click()
         
            print (u"退出APP")
        except Exception as e:
            print (u"Error: 退出APP失败\n", e)

if __name__ == "__main__":
    print (u"__main__  ")
    phone_number = '18911006376'

    #random.choice(['139', '188', '185', '136', '158', '151'])+"".join(random.choice("0123456789") for i in range(8))
    test_unit = unittest.TestSuite()
    test_unit.addTest(My_Test_Suite("test_login"))
    filename = './Result_auto_android.html'
    #fp = open(filename, 'w',encoding="utf-16")
    fp = open(filename, 'w')
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"测试报告",description=u"测试结果详情:")
    runner.run(test_unit)
    print (u"test_unit  ")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章