【Python + ATX基于uiautomator2】之编写unittest自动化测试脚本

不说废话上代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/08/31 09:43
# @Author  : zc
# @File    : 发起任务.py

import uiautomator2 as u2
import unittest
import uiautomator2.ext.htmlreport as htmlreport
from time import sleep


class TestcitymineSafety(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.u = u2.connect_usb('708ad0fy')
        cls.u.healthcheck()  # 解锁屏幕并启动uiautomator服务
        # hrp = htmlreport.HTMLReport(cls.u, 'report')
        # hrp.patch_click()

        # cls.u.disable_popups(True)  # 允许自动处理弹出框
        cls.u.toast.show("测试开始", 3)

    @classmethod
    def tearDownClass(cls):
        cls.u.toast.show("测试结束", 3)
        cls.u.app_stop_all()
        cls.u.service("uiautomator").stop()  # 停止uiautomator守护程序,允许其他测试框架如 appium 运行

    def setUp(self):
        #启动小卖安全
        self.d = self.u.session("cn.sqm.citymine_safety")  # restart app
        sleep(0.2)
        # 输入用户名
        self.d(resourceId="cn.sqm.citymine_safety:id/et_login_id").send_keys("xxx")
        self.d.click(0.924, 0.607)
        # 输入密码
        self.d(resourceId="cn.sqm.citymine_safety:id/et_login_password").set_text("xxx")
        self.d.click(0.924, 0.607)
        # 点击登录按钮
        self.d(resourceId="cn.sqm.citymine_safety:id/btn_login").click()
        print("登陆成功!")


    def tearDown(self):
        #返回首页
        # sleep(0.5)
        # self.d(className="android.widget.ImageView").click()
        #点击我的
        self.d(resourceId="cn.sqm.citymine_safety:id/rb_single_center").click()
        #退出账号
        self.d(text="安全退出账号").click()
        #返回首页home
        self.d.press("home")
        print("返回首页")
        # pass

    def testPrivateFM(self):  # 发起任务
        # 点击发起任务
        self.d(className="android.widget.ImageView", instance=10).click()
        # 选择任务类型:全部0、专项1、日常2、火灾3
        self.d(resourceId="cn.sqm.citymine_safety:id/tv_select_task_type").click()
        sleep(0.5)
        for i in range(0):
            self.d(className="android.view.View").drag_to(0.498, 0.806, duration=0.5)
        self.d(resourceId="cn.sqm.citymine_safety:id/btn_confirm").click()
        # 检查单位:IOS开发
        self.d(resourceId="cn.sqm.citymine_safety:id/tv_select_inspection_area").click()
        self.d(resourceId="cn.sqm.citymine_safety:id/item_tv_next_level").click()
        self.d(text='区域1').click()
        # 隐患类型:其他0、交通1、维稳2、火灾3
        self.d(resourceId="cn.sqm.citymine_safety:id/tv_select_hidden_danger_type").click()
        for i in range(0):
            self.d(className="android.view.View").drag_to(0.49, 0.8, duration=0.5)
        self.d(text='确定').click()
        # 隐患描述
        self.d(resourceId="cn.sqm.citymine_safety:id/et_hidden_danger_description").set_text("其他问题")
        self.d.click(0.924, 0.607)
        # 照片:1
        self.d(resourceId="cn.sqm.citymine_safety:id/iv_choose_photos").click()
        self.d(text=u"手机相册").click()
        self.d(resourceId="cn.sqm.citymine_safety:id/v_selected", className="android.widget.ImageView", instance=10).click()
        self.d(resourceId="cn.sqm.citymine_safety:id/done").click()
        # 违反制度:违反员工行为规范
        self.d(resourceId="cn.sqm.citymine_safety:id/iv_add_violation").click()
        self.d(resourceId="cn.sqm.citymine_safety:id/item_tv_content_violation", text=u"其他").click()
        self.d(resourceId="cn.sqm.citymine_safety:id/tv_complete").click()
        # 向上滑动拖拽
        self.d(className="android.widget.LinearLayout").drag_to(0.498, 0.00, duration=0.5)
        # 整改期限:
        self.d(resourceId="cn.sqm.citymine_safety:id/tv_please_the_rectification_date").click()
        for i in range(0):
            self.d(resourceId="cn.sqm.citymine_safety:id/day").drag_to(0.82, 0.8, duration=0.5)
        self.d(text='确定').click()
        # 协同人员
        list = ["aa", "检查01"]
        self.d(resourceId="cn.sqm.citymine_safety:id/item_tv_personnel_name").click()
        for i in range(len(list)):
            self.d(resourceId="cn.sqm.citymine_safety:id/item_tv_personnel_name", text=list[i]).click()
        self.d(resourceId="cn.sqm.citymine_safety:id/tv_complete").click()
        # 区域负责人
        self.d(resourceId="cn.sqm.citymine_safety:id/item_tv_personnel_name", className="android.widget.TextView",
          instance=len(list) + 1).click()
        for i in range(2):
            self.d(resourceId="cn.sqm.citymine_safety:id/lv_area_manager").click()
        self.d(resourceId="cn.sqm.citymine_safety:id/item_tv_department", text=u"区域04").click()
        self.d(resourceId="cn.sqm.citymine_safety:id/tv_complete").click()
        sleep(0.5)
        #提交按钮
        self.d(resourceId="cn.sqm.citymine_safety:id/btn_submit").click()


        #断言
        # 向下滑动拖拽
        self.d(className="android.widget.LinearLayout").drag_to(0.473, 0.99, duration=0.5)
        sleep(0.5)
        text = self.d(resourceId="cn.sqm.citymine_safety:id/tv_select_task_type").get_text()
        print(text)
        self.assertEqual(text,"专项任务","错误!")



if __name__ == '__main__':
    unittest.main()

 

具体文章请参考:

感谢:codeskyblue 大神的文章

ATX 《小白入门篇:python uiautomator2 的代码示例 (包含 app 管理,session 控制,toast,htmlreport,unittest ..)

 

posted @ 2018-09-12 15:14  Owen_ET  阅读(2433)  评论(0编辑  收藏  举报