python selenium自動化測試框架搭建的方法步驟
python selenium自動化測試框架搭建的方法步驟
更新時間:2020年06月14日 22:28:01 轉載作者:YinJia
這篇文章主要介紹了python selenium自動化測試框架搭建的方法步驟,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
目錄
設計思路
本文整理歸納以往的工作中用到的東西,現彙總成基礎測試框架提供分享。
框架採用python3 + selenium3 + PO + yaml + ddt + unittest等技術編寫成基礎測試框架,能適應日常測試工作需要。
1、使用Page Object模式將頁面定位和業務操作分開,分離測試物件(元素物件)和測試指令碼(用例指令碼),一個頁面建一個物件類,提高用例的可維護性;
2、使用yaml管理頁面控制元件元素資料和測試用例資料。例如元素ID等發生變化時,不需要去修改測試程式碼,只需要在對應的頁面元素yaml檔案中修改即可;
3、分模組管理,互不影響,隨時組裝,即拿即用。
GitHub專案地址:https://github.com/yingoja/DemoUI
測試框架分層設計
- 把常見的操作和查詢封裝成基礎類,不管是什麼產品,可直接拿來複用
- 業務層主要是封裝物件頁面類,一個頁面建一個類,業務層頁面繼承基礎層
- 用例層針對產品頁面功能進行構造摸擬執行測試
- 框架層提供基礎元件,支撐整個流程執行及功能擴充套件,給用例層提供各頁面的元素資料、用例測試資料,測試報告輸出等
測試框架目錄結構
如下思維導圖目錄結構介紹:
編寫用例方法
login.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | testinfo: - id: test_login001 title: 登入測試 info: 開啟抽屜首頁 testcase: - element_info: login-link-a find_type: ID operate_type: click info: 開啟登入對話方塊 - 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: 單擊取消自動登入單選框 - element_info: //span[text()='登入'] find_type: XPATH operate_type: click info: 單擊登入按鈕 - element_info: userProNick find_type: ID operate_type: perform info: 滑鼠懸停賬戶選單 - 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: 檢查退出登入是否成功 |
例如,我們要新增登入功能測試用例:
首先,只需在testyaml目錄下新增一個頁面物件yaml檔案,參考login.yaml格式編寫即可。這些檔案是提供給封裝頁面物件類呼叫並執行定位識別操作。
login_data.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | - 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目錄下新增一個login_data.yaml檔案提供給登入介面傳參的測試資料,編寫格式參考login_data.yaml檔案。
loginPage.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #!/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): """ 使用者登入頁面 """ url = '/' dig_login_button_loc = (By.ID, testData.get_elementinfo(0)) def dig_login(self): """ 首頁登入 :return: """ self.find_element(*self.dig_login_button_loc).click() sleep(1) # 定位器,通過元素屬性定位元素物件 # 手機號輸入框 login_phone_loc = (By.ID,testData.get_elementinfo()) # 密碼輸入框 login_password_loc = (By.ID,testData.get_elementinfo()) # 取消自動登入 keeplogin_button_loc = (By.XPATH,testData.get_elementinfo()) # 單擊登入 login_user_loc = (By.XPATH,testData.get_elementinfo()) # 退出登入 login_exit_loc = (By.ID, testData.get_elementinfo()) # 選擇退出 login_exit_button_loc = (By.XPATH,testData.get_elementinfo()) 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): """ 取消單選自動登入 :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: 使用者名稱 :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 # 登入成功使用者名稱 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).text |
然後,在page_obj目錄下新增一個loginPage.py檔案,是用來封裝登入頁面物件類,執行登入測試流程操作。
login_sta.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #!/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): """ 使用者登入 :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} ; 測試點-> {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("檢查點-> {0}".format(po.user_login_success_hint())) self.assertEqual(po.user_login_success_hint(), datayaml['check'][0], "成功登入,返回實際結果是->: {0}".format(po.user_login_success_hint())) log.info("成功登入,返回實際結果是->: {0}".format(po.user_login_success_hint())) screenshot.insert_img(self.driver, datayaml['screenshot'] + '.jpg') log.info("-----> 開始執行退出流程操作") self.exit_login_check() po_exit = login(self.driver) log.info("檢查點-> 找到{}元素,表示退出成功!".format(po_exit.exit_login_success_hint())) self.assertEqual(po_exit.exit_login_success_hint(), '註冊',"退出登入,返回實際結果是->: {0}".format(po_exit.exit_login_success_hint())) log.info("退出登入,返回實際結果是->: {0}".format(po_exit.exit_login_success_hint())) else: log.info("檢查點-> {0}".format(po.phone_pawd_error_hint())) self.assertEqual(po.phone_pawd_error_hint(),datayaml['check'][] , "異常登入,返回實際結果是->: {}".format(po.phone_pawd_error_hint())) log.info("異常登入,返回實際結果是->: {0}".format(po.phone_pawd_error_hint())) screenshot.insert_img(self.driver,datayaml['screenshot'] + '.jpg') if __name__=='__main__': unittest.main() |
最後,在testcase目錄下建立測試用例檔案login_sta.py,採用ddt資料驅動讀取yaml測試資料檔案
綜上所述,編寫用例方法只需要按以上四個步驟建立->編寫即可。
執行如下主程式,可看輸出的實際結果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #!/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 # 測試報告存放資料夾,如不存在,則自動建立一個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自動化測試報告', description='環境:windows 7 瀏覽器:chrome', tester='Jason') runner.run(all_case) fp.close() report = new_report(setting.TEST_REPORT) #呼叫模組生成最新的報告 send_mail(report) #呼叫傳送郵件模組 if __name__ =="__main__": cases = add_case() run_case(cases) |
測試結果展示
HTML報告日誌
HTML報告點選截圖,彈出截圖
測試報告通過的日誌
自動截圖存放指定的目錄
郵件測試報告
到此這篇關於python selenium自動化測試框架搭建的方法步驟的文章就介紹到這了,更多相關python selenium自動化測試框架搭建內容請搜尋指令碼之家以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援指令碼之家!
作者:YinJia
出處:http://www.cnblogs.com/yinjia/
您可能感興趣的文章:
- selenium+python自動化測試環境搭建步驟
- selenium+python自動化測試之環境搭建
- selenium3+python3環境搭建教程圖解
- Python + selenium自動化環境搭建的完整步驟
- python+selenium開發環境搭建圖文教程
如您對本文有所疑義或者有任何需求,請點選訪問指令碼社群,百萬網友為您解惑!