【Python + ATX基於uiautomator2】之編寫unittest自動化測試腳本

不說廢話上代碼:html

#!/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、專項一、平常二、火災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、交通一、維穩二、火災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()

 

具體文章請參考:node

感謝:codeskyblue 大神的文章python

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

相關文章
相關標籤/搜索