<ruby id="h6500"><table id="h6500"></table></ruby>
    1. <ruby id="h6500"><video id="h6500"></video></ruby>
          1. <progress id="h6500"><u id="h6500"><form id="h6500"></form></u></progress>

            使用python編寫(xiě)selenium自動(dòng)化測試框架

            發(fā)表于:2023-07-01來(lái)源:博客園作者:YinJia點(diǎn)擊數: 標簽:
            本文主要使用思維導圖目錄結構介紹基礎測試框架,編寫(xiě)測試用例進(jìn)行功能測試用例,希望對您的學(xué)習有所幫助。

            設計思路

            框架采用python3 + selenium3 + PO + yaml + ddt + unittest等技術(shù)編寫(xiě)成基礎測試框架,能適應日常測試工作需要。

            1、使用Page Object模式將頁(yè)面定位和業(yè)務(wù)操作分開(kāi),分離測試對象(元素對象)和測試腳本(用例腳本),一個(gè)頁(yè)面建一個(gè)對象類(lèi),提高用例的可維護性;

            2、使用yaml管理頁(yè)面控件元素數據和測試用例數據。例如元素ID等發(fā)生變化時(shí),不需要去修改測試代碼,只需要在對應的頁(yè)面元素yaml文件中修改即可;

            3、分模塊管理,互不影響,隨時(shí)組裝,即拿即用。

            GitHub項目地址:https://github.com/yingoja/DemoUI

            測試框架分層設計

            把常見(jiàn)的操作和查找封裝成基礎類(lèi),不管是什么產(chǎn)品,可直接拿來(lái)復用

            業(yè)務(wù)層主要是封裝對象頁(yè)面類(lèi),一個(gè)頁(yè)面建一個(gè)類(lèi),業(yè)務(wù)層頁(yè)面繼承基礎層

            用例層針對產(chǎn)品頁(yè)面功能進(jìn)行構造摸擬執行測試

            框架層提供基礎組件,支撐整個(gè)流程執行及功能擴展,給用例層提供各頁(yè)面的元素數據、用例測試數據,測試報告輸出等

            測試框架目錄結構

            如下思維導圖目錄結構介紹:

            編寫(xiě)用例方法

            testinfo:
            - id: test_login001
            title: 登錄測試
            info: 打開(kāi)抽屜首頁(yè)
            testcase:
            - element_info: login-link-a
            find_type: ID
            operate_type: click
            info: 打開(kāi)登錄對話(huà)框
            - element_info: mobile
            find_type: ID
            operate_type: send_keys
            info: 輸入手機號
            - element_info: mbpwd
            find_type: ID
            operate_type: send_keys
            info: 輸入密碼
            - element_info: //input[@class='keeplogin']
            find_type: XPATH
            operate_type: click
            info: 單擊取消自動(dòng)登錄單選框
            - element_info: //span[text()='登錄']
            find_type: XPATH
            operate_type: click
            info: 單擊登錄按鈕
            - element_info: userProNick
            find_type: ID
            operate_type: perform
            info: 鼠標懸停賬戶(hù)菜單
            - element_info: //a[@class='logout']
            find_type: XPATH
            operate_type: click
            info: 選擇退出
            check:
            - element_info: //div[@class='box-mobilelogin']
            /div[1]/span
            find_type: XPATH
            info: 檢查輸入手機號或密碼,登錄異常提示
            - element_info: userProNick
            find_type: ID
            info: 成功登錄
            - element_info: reg-link-a
            find_type: ID
            info: 檢查退出登錄是否成功

            login.yaml

            例如,我們要新增登錄功能測試用例:

            首先,只需在testyaml目錄下新增一個(gè)頁(yè)面對象yaml文件,參考login.yaml格式編寫(xiě)即可。這些文件是提供給封裝頁(yè)面對象類(lèi)調用并執行定位識別操作。

            -
            id: test_login001.1
            detail : 手機號和密碼為空登錄
            screenshot : phone_pawd_empty
            data:
            phone: ""
            password: ""
            check :
            - 手機號不能為空
            -
            id: test_login001.2
            detail : 手機號為空登錄
            screenshot : phone_empty
            data :
            phone: ""
            password : aa
            check :
            - 手機號不能為空
            -
            id: test_login001.3
            detail : 密碼為空登錄
            screenshot : pawd_empty
            data :
            phone : 13511112222
            password: ""
            check :
            - 密碼不能為空
            -
            id: test_login001.4
            detail : 非法手機號登錄
            screenshot : phone_error
            data :
            phone : abc
            password: aa
            check :
            - 手機號格式不對
            -
            id: test_login001.5
            detail : 手機號或密碼不匹配
            screenshot : pawd_error
            data :
            phone : 13511112222
            password: aa
            check :
            - 賬號密碼錯誤
            -
            id: test_login001.6
            detail : 手機號和密碼正確
            screenshot : phone_pawd_success
            data :
            phone : 13865439800
            password: ********
            check :
            - yingoja

            login_data.yaml

            login_data.yaml

            其次,在testdata目錄下新增一個(gè)login_data.yaml文件提供給登錄接口傳參的測試數據,編寫(xiě)格式參考login_data.yaml文件。

            #!/usr/bin/env python
            # _*_ coding:utf-8 _*_
            __author__ = 'YinJia'
            import os,sys
            sys.path.append(os.path.dirname(os.path.dirname
            (os.path.dirname(__file__))))
            from config import setting
            from selenium.webdriver.support.select import Select
            from selenium.webdriver.common.action_chains
            import ActionChains
            from selenium.webdriver.common.by import By
            from public.page_obj.base import Page
            from time import sleep
            from public.models.GetYaml import getyaml
            testData = getyaml(setting.TEST_Element_YAML
            + '/' + 'login.yaml')
            class login(Page):
            """
            用戶(hù)登錄頁(yè)面
            """
            url = '/'
            dig_login_button_loc = (By.ID, testData.
            get_elementinfo(0)) def dig_login(self):
            """
            首頁(yè)登錄
            :return:
            """
            self.find_element(*self.dig_login_button_loc)
            .click() sleep(1)
            # 定位器,通過(guò)元素屬性定位元素對象
            # 手機號輸入框
            login_phone_loc = (By.ID,testData.
            get_elementinfo(1)) # 密碼輸入框
            login_password_loc = (By.ID,testData.
            get_elementinfo(2)) # 取消自動(dòng)登錄
            keeplogin_button_loc = (By.XPATH,testData.
            get_elementinfo(3)) # 單擊登錄
            login_user_loc = (By.XPATH,testData.
            get_elementinfo(4)) # 退出登錄
            login_exit_loc = (By.ID, testData.
            get_elementinfo(5)) # 選擇退出
            login_exit_button_loc = (By.XPATH,testData.
            get_elementinfo(6))def login_phone(self,phone):
            """
            登錄手機號
            :param username:
            :return:
            """
            self.find_element(*self.login_phone_loc).
            send_keys(phone)def login_password(self,password):
            """
            登錄密碼
            :param password:
            :return:
            """
            self.find_element(*self.login_password_loc).
            send_keys(password) def keeplogin(self):
            """
            取消單選自動(dòng)登錄
            :return:
            """
            self.find_element(*self.keeplogin_button_loc).
            click()def login_button(self):
            """
            登錄按鈕
            :return:
            """
            self.find_element(*self.login_user_loc).click()
            def login_exit(self):
            """
            退出系統
            :return:
            """
            above = self.find_element(*self.login_exit_loc)
            ActionChains(self.driver).move_to_element(above).
            perform() sleep(2)
            self.find_element(*self.login_exit_button_loc)
            .click()def user_login(self,phone,password):
            """
            登錄入口
            :param username: 用戶(hù)名
            :param password: 密碼
            :return:
            """
            self.open()
            self.dig_login()
            self.login_phone(phone)
            self.login_password(password)
            sleep(1)
            self.keeplogin()
            sleep(1)
            self.login_button()
            sleep(1)
            phone_pawd_error_hint_loc = (By.XPATH,testData.
            get_CheckElementinfo(0))
            user_login_success_loc = (By.ID,testData.
            get_CheckElementinfo(1))
            exit_login_success_loc = (By.ID,testData.
            get_CheckElementinfo(2))
            # 手機號或密碼錯誤提示
            def phone_pawd_error_hint(self):
            return self.find_element(*self.phone_pawd_error_
            hint_loc).text# 登錄成功用戶(hù)名
            def user_login_success_hint(self):
            return self.find_element(*self.user_login_
            success_loc).text # 退出登錄
            def exit_login_success_hint(self):
            return self.find_element(*self.exit_login_
            success_loc).textloginPage.py

            然后,在page_obj目錄下新增一個(gè)loginPage.py文件,是用來(lái)封裝登錄頁(yè)面對象類(lèi),執行登錄測試流程操作。

            #!/usr/bin/env python
            # _*_ coding:utf-8 _*_
            __author__ = 'YinJia'
            import os,sys
            sys.path.append(os.path.dirname(os.path.
            dirname(__file__)))
            import unittest,ddt,yaml
            from config import setting
            from public.models import myunit,screenshot
            from public.page_obj.loginPage import login
            from public.models.log import Log
            try:
            f =open(setting.TEST_DATA_YAML + '/' + 'login_data.yaml',encoding='utf-8')
            testData = yaml.load(f)
            except FileNotFoundError as file:
            log = Log()
            log.error("文件不存在:{0}".format(file))
            @ddt.ddt
            class Demo_UI(myunit.MyTest):
            """抽屜新熱榜登錄測試"""
            def user_login_verify(self,phone,password):
            """
            用戶(hù)登錄
            :param phone: 手機號
            :param password: 密碼
            :return:
            """
            login(self.driver).user_login(phone,password)
            def exit_login_check(self):
            """
            退出登錄
            :return:
            """
            login(self.driver).login_exit()
            @ddt.data(*testData)
            def test_login(self,datayaml):
            """
            登錄測試
            :param datayaml: 加載login_data登錄測試數據
            :return:
            """
            log = Log()
            log.info("當前執行測試用例ID-> {0} ; 測試點(diǎn)-> {1}".format(datayaml['id'],datayaml['detail']))
            # 調用登錄方法
            self.user_login_verify(datayaml['data']['phone'],
            datayaml['data']['password'])
            po = login(self.driver)
            if datayaml['screenshot'] == 'phone_pawd_success':
            log.info("檢查點(diǎn)-> {0}".format
            (po.user_login_success_hint()))
            self.assertEqual(po.user_login_success_hint(), datayaml['check'][0], "成功登錄,返回實(shí)際結果是->: {0}".format(po.user_login_success_hint()))
            log.info("成功登錄,返回實(shí)際結果是->: {0}".format(po.user_login_success_hint()))
            screenshot.insert_img(self.driver, datayaml
            ['screenshot'] + '.jpg')
            log.info("-----> 開(kāi)始執行退出流程操作")
            self.exit_login_check()
            po_exit = login(self.driver)
            log.info("檢查點(diǎn)-> 找到{0}元素,表示退出成功!".format(po_exit.exit_login_success_hint()))
            self.assertEqual(po_exit.exit_login_success_hint(),
            '注冊',"退出登錄,返回實(shí)際結果是->: {0}".format(po_exit.exit_login_success_hint()))
            log.info("退出登錄,返回實(shí)際結果是->: {0}".format(po_exit.exit_login_success_hint()))
            else:
            log.info("檢查點(diǎn)-> {0}".format(po.phone
            _pawd_error_hint()))
            self.assertEqual(po.phone_pawd_error_hint(),
            datayaml['check'][0] , "異常登錄,返回實(shí)際結果是->: {0}".format(po.phone_pawd_error_hint()))
            log.info("異常登錄,返回實(shí)際結果是->: {0}".format(po.phone_pawd_error_hint()))
            screenshot.insert_img(self.driver,datayaml
            ['screenshot'] + '.jpg')
            if __name__=='__main__':
            unittest.main()
            login_sta.py

            最后,在testcase目錄下創(chuàng )建測試用例文件login_sta.py,采用ddt數據驅動(dòng)讀取yaml測試數據文件

            綜上所述,編寫(xiě)用例方法只需要按以上四個(gè)步驟創(chuàng )建->編寫(xiě)即可。

            執行如下主程序,可看輸出的實(shí)際結果。

            #!/usr/bin/env python
            # _*_ coding:utf-8 _*_
            __author__ = 'YinJia' import os,sys
            sys.path.append(os.path.dirname(__file__))
            from config import setting
            import unittest,time
            from package.HTMLTestRunner import HTMLTestRunner
            from public.models.newReport import new_report
            from public.models.sendmail import send_mail
            # 測試報告存放文件夾,如不存在,則自動(dòng)創(chuàng )建
            一個(gè)report目錄 if not os.path.exists(setting.TEST_REPORT):os.makedirs
            (setting.TEST_REPORT + '/' + "screenshot")
            def add_case(test_path=setting.TEST_DIR):
            """加載所有的測試用例"""
            discover = unittest.defaultTestLoader.discover
            (test_path, pattern='*_sta.py')
            return discover
            def run_case(all_case,result_path=setting.TEST_REPORT):
            """執行所有的測試用例"""
            now = time.strftime("%Y-%m-%d %H_%M_%S")
            filename = result_path + '/' + now + 'result.html'
            fp = open(filename,'wb')
            runner = HTMLTestRunner(stream=fp,title='
            抽屜新熱榜UI自動(dòng)化測試報告',
            description='環(huán)境:windows 7 瀏覽器:chrome',
            tester='Jason')
            runner.run(all_case)
            fp.close()
            report = new_report(setting.TEST_REPORT)
            #調用模塊生成最新的報告
            send_mail(report) #調用發(fā)送郵件模塊
            if __name__ =="__main__":
            cases = add_case()
            run_case(cases)

             

            測試結果展示

            HTML報告日志

            HTML報告點(diǎn)擊截圖,彈出截圖

            測試報告通過(guò)的日志

            自動(dòng)截圖存放指定的目錄

            郵件測試報告

            原文轉自:https://www.cnblogs.com/yinjia/p/9503407.html

            ...
            老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月
              <ruby id="h6500"><table id="h6500"></table></ruby>
              1. <ruby id="h6500"><video id="h6500"></video></ruby>
                    1. <progress id="h6500"><u id="h6500"><form id="h6500"></form></u></progress>