1. 程式人生 > 實用技巧 >Appium-Python3--UI自動化-[-12-]-UI自動化框架介紹

Appium-Python3--UI自動化-[-12-]-UI自動化框架介紹


C:\chushujin\study\sales_ui_auto>tree
├─.idea
│ └─inspectionProfiles
├─app_package --APP的apk包
├─comm --封裝一些訪問資料庫,日誌輸出,讀取配置檔案,HTMLTestRunner報告,獲取裝置資訊方法封裝
├─config --封裝一些獲取專案路徑方法,讀取excel等

├─locator --封裝讀取配置中元素的路徑

│ └─app
│ ├─appCommon
│ │ ├─testLoginOut
│ │ │
│ │ └─testSalesLogin
│ │
│ └─BusinessProcess
│ ├─testLendRequestProcess
│ │ ├─testCustomerInfo
│ │ │
│ │ ├─testLoanInfo
│ │ │
│ │
│ └─testPotentialManager

├─page -- 通過locator中的讀取到的內容,進行後續操作的封裝
│ └─app
│ ├─appCommon
│ │ ├─testLoginOut
│ │ │
│ │ └─testSalesLogin
│ │
│ └─BusinessProcess
│ ├─testLendRequestProcess
│ │ ├─testCustomerInfo
│ │ │
│ │ ├─testLoanInfo
│ │ │
│ │
│ └─testPotentialManager

├─page_element --頁面元素的定位和操作的方法封裝

├─result --測試過程中的截圖,日誌,html報告
│ └─image
│ ├─2020-08-28
│ ├─2020-08-31
│ ├─2020-09-01
│ └─error
├─testCase -- 測試用例實現邏輯
│ ├─app
│ │ ├─appCommon
│ │ │ ├─testLogin
│ │ │ │
│ │ │ ├─testLoginOut
│ │ │ │
│ │ │
│ │ ├─BusinessProcess
│ │ │ ├─testLendRequestProcess
│ │ │ │ ├─testCustomerInfo
│ │ │ │ │
│ │ │ │ ├─testLoanInfo
│ │ │ │ │
│ │ │ │
│ │ │ ├─testPotentialManager
│ │ │ │
│ │ │
│ │

├─testFile -- 測試用例在excel中放置,名稱,引數,期望結果,描述
│ └─app
│ ├─appCommon
│ │ ├─testLogin
│ │ └─testLoginOut
│ └─BusinessProcess
│ ├─testLendRequestProcess
│ │ ├─testCustomerInfo
│ │ └─testLoanInfo
│ └─testPotentialManager
├─testModels -- 類似java中getter和setter方法
│ └─app
│ ├─appCommon
│ │ └─testLogin
│ │
│ └─BusinessProcess
│ ├─testLendRequestProcess
│ │ ├─testCustomerInfo
│ │ │
│ │ └─testLoanInfo
│ └─testPotentialManager
└─util -- 封裝生成一下身份證號,手機號,中文姓名,隨機字串等
├─deal_param

├─decorator

├─generator

├─caseList.txt --測試用例執行順序
├─config.ini --配置檔案
├─runCase.py -- 主入口



1. 配置檔案中放置config.ini

#登入id
login_id = com.ucredit.sale.android:id/account_et
#密碼id
password_id = com.ucredit.sale.android:id/password_et
#登入按鈕id
login_submit_id = com.ucredit.sale.android:id/sign_in_tv

2.locattor

class LocatorSalesLogin:


    # 位置授權是否彈出
    auth_location_is_show_text = myConfig.getAppElement("auth_location_is_show_text")

    # 位置授權按鈕
    location_check_id = myConfig.getAppElement("login_click_id")

    # 使用者名稱
    username_id = myConfig.getAppElement("login_id")

    # 密碼
    password_id = myConfig.getAppElement("password_id")

    # 登入按鈕
    login_btn_id = myConfig.getAppElement("login_submit_id")

  

3. page

class SalesLoginPage(AppPageCommon):

    def location_check(self):
        """
        位置授權
        :return:
        """
        # 檢查位置授權是否彈出
        return self.is_toast_exist(LocatorSalesLogin.auth_location_is_show_text)



    def location_click(self):
        """
        位置授權
        :return:
        """

        # 點選
        logger.info("開始授權位置...")
        print("開始授權位置...")
        self.click_element("id",LocatorSalesLogin.location_check_id)



    def login(self,u_data,p_data):
        """
        登入元素
        :param u_data:
        :param p_data:
        :return:
        """
        logger.info("開始登入...")
        self.input_id(LocatorSalesLogin.username_id,u_data)
        self.input_id(LocatorSalesLogin.password_id,p_data)
        logger.info("登入引數為:username : %s,password : %s" % (u_data,p_data))
        print("登入引數為:username : %s,password : %s" % (u_data,p_data))

        self.click_element("id",LocatorSalesLogin.login_btn_id)

4. testCase-測試用例邏輯

readconfig = readConfig.ReadConfig()
writeconfig = writeConfig.WriteConfig()

logger = logger


sheet_name = readconfig.get_case("business")
login_xls = readExcel.readExcel().get_xls('app/appCommon/testLogin','test_001_sales_login_module.xlsx', sheet_name)

@paramunittest.parametrized(*login_xls)
class TestLoginSales(unittest.TestCase):

    def setParameters(self,case_Name,param,excepted,reMarks):

        self.case_name = case_Name
        self.param = str(param)
        self.excepted = excepted
        self.reMarks = reMarks


    def setUp(self):
        """

        :return:
        """
        logger.info((self.case_name+"測試開始前準備").center(50,"*"))
        print('\n'+(self.case_name+"測試開始前準備").center(50,"*"))

        init_sales = initSalesObj
        self.driver = init_sales.get_driver()
        self.sales_login_page = SalesLoginPage(self.driver)

    def tearDown(self):

        logger.info((self.case_name + "測試結束,輸出log完結").center(50, "*"))
        print((self.case_name + "測試結束,輸出log完結").center(50, "*"))

    def test_Login(self):
        """登入"""
        # 輸入引數處理
        param = json.loads(str(self.param))

        logger.info("檢查位置授權彈框之前時間為:%s" % datetime.datetime.now())

        # 檢查位置授權是否彈出
        is_show = self.sales_login_page.location_check()

        logger.info("位置授權是否存在: %s " % is_show)
        print("位置授權是否存在: %s " % is_show)

        # app正常啟動,截圖儲存
        common.take_screenShot(self.driver,u"啟動頁面")


        if is_show is True:

            # 獲取位置授權
            self.sales_login_page.location_click()

       # 登入
        self.sales_login_page.login(param['username'],param['password'])

        # 斷言
        self.checkResult()


    def checkResult(self):
# 斷言登入成功
        self.assertTrue(self.sales_login_page.is_toast_exist(self.excepted))

        common.take_screenShot(self.driver,"登入成功")

5. testFile-測試用例

6. caseList.txt

#這裡存放要執行的case,帶#號的測試用例不會執行
# 登入
# app/appCommon/testSalesLogin/test_001_sales_login_module

7. runCase.py

讀取caseList.txt中的用例到一個列表。

    def set_case_list(self):
          """
          讀取caselist.txt檔案中的用例名稱,並新增到caselist元素組
          :return:
          """
          fb = open(self.caseListFile,encoding="utf-8")
          for value in fb.readlines():
                data = str(value)
                if data != '' and not data.startswith("#"):  # 如果data非空且不以#開頭
                      self.caseList.append(data.replace("\n", ""))  # 讀取每行資料會將換行轉換為\n,去掉每行資料中的\n
          fb.close()
          # print(self.caseList)

生成測試套件,並將用例新增至其中

    def set_case_suite(self):
          """

          :return:
          """
          # 通過set_case_list()拿到caselist元素組
          self.set_case_list()
          # 建立測試套件
          test_suite = unittest.TestSuite()
          suite_module = []

          # 從caselist元素組中迴圈取出case
          for case in self.caseList:
                # 通過split函式來將aaa/bbb分割字串,-1取後面,0取前面
                case_name = case.split("/")[-1]
                # 打印出取出來的名稱
                print(case_name + ".py")
                # 批量載入用例,第一個引數為用例存放路徑,第二個引數為規則
                discover = unittest.defaultTestLoader.discover(self.caseFile, pattern=case_name + '.py',
                                                               top_level_dir=None)
                # 將discover存入suite_module元素組
                suite_module.append(discover)
                # print('suite_module:'+str(suite_module))
          # 判斷suite_module元素組是否存在元素
          if len(suite_module) > 0:
                # 如果存在,迴圈取出元素組內容,命名為suite
                for suite in suite_module:
                      # 從discover中取出test_name,使用addTest新增到測試集
                      for test_name in suite:
                            test_suite.addTest(test_name)
          else:
                print('測試套件中無可執行的測試用例')
                return None
          return test_suite

run