安卓手机自动化测试_unittest业务场景测试

#装饰器

#忽略warning提示

#断言失败截图

import time
import warnings
import unittest
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from test import errorscreen

class TestSuite_ydflow(unittest.TestCase):
    #装饰器,标签说明代表是类方法
    @classmethod
    #测试套中只执行一次
    def setUpClass(self) -> None:
        # unittest框架自身问题,有很多进程不关闭造成warn,引入warnings模块忽略告警
        warnings.filterwarnings('ignore')
        self.caps = {}
        self.caps['automationName'] = 'UIAutomator2'
        self.caps['platformName'] = 'Android'
        self.caps['platformVersion'] = '6.0'
        self.caps['deviceName'] = '192.168.1410101:5555'
        self.caps['appPackage'] = 'com.youdao.note'
        self.caps['appActivity'] = '.activity2.MainActivity t362'
        self.driver=WebDriver('http://127.0.0.1:4723/wd/hub',self.caps)
        self.driver.implicitly_wait(10)

    #装饰器,断言失败截图
    @errorscreen
    def test_case1_addnote(self):
        # 智能等待
        el = WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_id(
            'com..android.packageinstaller:id/permission_a;;ow_button'))
        if el:
            # 点击允许按钮
            self.driver.find_element_by_id('com.android.packageinstaller:id/permission_a;;ow_button').click()
            # 点击新建按钮
            self.driver.find_element_by_id('com.youdao.note:id/add_note').click()
            # 点击新建笔记
            self.driver.find_element_by_id('com.youdao.note:id/add_note_floater_add_note').click()
            # 点击取消按钮
            self.driver.find_element_by_id('com.youdao.note:id/btn_cancel').click()
            # 输入内容
            # xpath定位
            self.driver.find_element_by_xpath(
                "//*[@resource-id='com.youdao.note:id/note_content']/android.widget.EditText").send_keys('testcontent')

            # 输入标题
            self.driver.find_element_by_id('com.youdao.note:id/note_title').send_keys('testtitle')
            # 点击完成按钮
            # class_name定位

            self.driver.find_element_by_class_name('android.support.done').click()
        result = self.driver.find_element_by_id('com.youdao.note:id/title').text
        # 如果不相等输出‘新增笔记测试失败’
        # self.assertEqual(result, 'testtitle', '新增笔记测试失败')
        #用try except实现断言失败截图
        # try:
        #     self.assertEqual(result, 'testtitle', '新增笔记测试失败')
        # except AssertionError:
        #     nowtime = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
        #     self.driver.get_screenshot_as_file('新增笔记失败' + nowtime + '.png')
        #     raise AssertionError
        self.assertEqual(result, 'testtitle', '新增笔记测试失败')
    def test_case2_editnote(self):
        pass
    @classmethod
    # 测试套中只执行一次
    def tearDownClass(self) -> None:
        self.driver.quit()

if __name__ == '__main__':
    suite=unittest.TestSuite()
    suite.addTest(TestSuite_ydflow('test_case1_addnote'))
    runner=unittest.TextTestRunner()
    runner.run(suite)

#装饰器具体实现

#获取当前时间

#装饰器
import time
def errorscreen(func):
    def wrapper(self,*args,**kwargs):
        try:
            func(self)
        except AssertionError:
            nowtime = time.strftime('%Y%m%d%H%M%S')
            self.driver.get_screenshot_as_file('新增笔记失败' + nowtime + '.png')
        raise AssertionError
    return wrapper

#使用测试套,实现框架下执行

import csv
import os

from HTMLTestRunner import HTMLTestRunner
import unittest
import unittest2


if __name__ == '__main__':
    dirpath = os.getcwd()
    seppath = os.path.abspath(dirpath + os.path.sep + '.')
    frameapath = seppath + r'\appium_test\frame\framefile.csv'
    file1=open(frameapath,'r')
    table=csv.reader(file1)
    for row in table:
        #控制用例是否执行
        if row[1]=='Run':
            #类名
            testname=row[0]
            #生产测试套
            #vars()将常量转换成变量指这里传入的类
            suite=unittest.TestSuite(unittest.TestLoader().loadTestsFromTestCase(vars()[testname]))
            #老方式,不使用了
            # suite=unittest.makeSuite(vars()[testname])
            #创建测试报告,row[2]生产测试报告的名字
            reportpath=seppath+r'\appium\report\\'+row[2]
            file2=open(reportpath,'wb')
            runner=HTMLTestRunner(stream=file2,title=row[3],description=row[4])
            runner.run(suite)
            file2.close()
        else:
            continue

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值