本文整理概括以往的工做中用到的東西,現彙總成基礎測試框架提供分享。html
框架採用python3 + selenium3 + PO + yaml + ddt + unittest等技術編寫成基礎測試框架,能適應平常測試工做須要。python
一、使用Page Object模式將頁面定位和業務操做分開,分離測試對象(元素對象)和測試腳本(用例腳本),一個頁面建一個對象類,提升用例的可維護性;git
二、使用yaml管理頁面控件元素數據和測試用例數據。例如元素ID等發生變化時,不須要去修改測試代碼,只須要在對應的頁面元素yaml文件中修改便可;github
三、分模塊管理,互不影響,隨時組裝,即拿即用。web
GitHub項目地址:https://github.com/yingoja/DemoUIchrome
以下思惟導圖目錄結構介紹:windows
1 testinfo: 2 - id: test_login001 3 title: 登陸測試 4 info: 打開抽屜首頁 5 testcase: 6 - element_info: login-link-a 7 find_type: ID 8 operate_type: click 9 info: 打開登陸對話框 10 - element_info: mobile 11 find_type: ID 12 operate_type: send_keys 13 info: 輸入手機號 14 - element_info: mbpwd 15 find_type: ID 16 operate_type: send_keys 17 info: 輸入密碼 18 - element_info: //input[@class='keeplogin'] 19 find_type: XPATH 20 operate_type: click 21 info: 單擊取消自動登陸單選框 22 - element_info: //span[text()='登陸'] 23 find_type: XPATH 24 operate_type: click 25 info: 單擊登陸按鈕 26 - element_info: userProNick 27 find_type: ID 28 operate_type: perform 29 info: 鼠標懸停帳戶菜單 30 - element_info: //a[@class='logout'] 31 find_type: XPATH 32 operate_type: click 33 info: 選擇退出 34 check: 35 - element_info: //div[@class='box-mobilelogin']/div[1]/span 36 find_type: XPATH 37 info: 檢查輸入手機號或密碼,登陸異常提示 38 - element_info: userProNick 39 find_type: ID 40 info: 成功登陸 41 - element_info: reg-link-a 42 find_type: ID 43 info: 檢查退出登陸是否成功
例如,咱們要新增登陸功能測試用例:瀏覽器
首先,只需在testyaml目錄下新增一個頁面對象yaml文件,參考login.yaml格式編寫便可。這些文件是提供給封裝頁面對象類調用並執行定位識別操做。app
1 - 2 id: test_login001.1 3 detail : 手機號和密碼爲空登陸 4 screenshot : phone_pawd_empty 5 data: 6 phone: "" 7 password: "" 8 check : 9 - 手機號不能爲空 10 - 11 id: test_login001.2 12 detail : 手機號爲空登陸 13 screenshot : phone_empty 14 data : 15 phone: "" 16 password : aa 17 check : 18 - 手機號不能爲空 19 - 20 id: test_login001.3 21 detail : 密碼爲空登陸 22 screenshot : pawd_empty 23 data : 24 phone : 13511112222 25 password: "" 26 check : 27 - 密碼不能爲空 28 - 29 id: test_login001.4 30 detail : 非法手機號登陸 31 screenshot : phone_error 32 data : 33 phone : abc 34 password: aa 35 check : 36 - 手機號格式不對 37 - 38 id: test_login001.5 39 detail : 手機號或密碼不匹配 40 screenshot : pawd_error 41 data : 42 phone : 13511112222 43 password: aa 44 check : 45 - 帳號密碼錯誤 46 - 47 id: test_login001.6 48 detail : 手機號和密碼正確 49 screenshot : phone_pawd_success 50 data : 51 phone : 13865439800 52 password: ******** 53 check : 54 - yingoja 55 56 login_data.yaml
其次,在testdata目錄下新增一個login_data.yaml文件提供給登陸接口傳參的測試數據,編寫格式參考login_data.yaml文件。框架
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 __author__ = 'YinJia' 4 5 import os,sys 6 sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) 7 from config import setting 8 from selenium.webdriver.support.select import Select 9 from selenium.webdriver.common.action_chains import ActionChains 10 from selenium.webdriver.common.by import By 11 from public.page_obj.base import Page 12 from time import sleep 13 from public.models.GetYaml import getyaml 14 15 testData = getyaml(setting.TEST_Element_YAML + '/' + 'login.yaml') 16 17 class login(Page): 18 """ 19 用戶登陸頁面 20 """ 21 url = '/' 22 dig_login_button_loc = (By.ID, testData.get_elementinfo(0)) 23 def dig_login(self): 24 """ 25 首頁登陸 26 :return: 27 """ 28 self.find_element(*self.dig_login_button_loc).click() 29 sleep(1) 30 31 # 定位器,經過元素屬性定位元素對象 32 # 手機號輸入框 33 login_phone_loc = (By.ID,testData.get_elementinfo(1)) 34 # 密碼輸入框 35 login_password_loc = (By.ID,testData.get_elementinfo(2)) 36 # 取消自動登陸 37 keeplogin_button_loc = (By.XPATH,testData.get_elementinfo(3)) 38 # 單擊登陸 39 login_user_loc = (By.XPATH,testData.get_elementinfo(4)) 40 # 退出登陸 41 login_exit_loc = (By.ID, testData.get_elementinfo(5)) 42 # 選擇退出 43 login_exit_button_loc = (By.XPATH,testData.get_elementinfo(6)) 44 45 def login_phone(self,phone): 46 """ 47 登陸手機號 48 :param username: 49 :return: 50 """ 51 self.find_element(*self.login_phone_loc).send_keys(phone) 52 53 def login_password(self,password): 54 """ 55 登陸密碼 56 :param password: 57 :return: 58 """ 59 self.find_element(*self.login_password_loc).send_keys(password) 60 61 def keeplogin(self): 62 """ 63 取消單選自動登陸 64 :return: 65 """ 66 self.find_element(*self.keeplogin_button_loc).click() 67 68 def login_button(self): 69 """ 70 登陸按鈕 71 :return: 72 """ 73 self.find_element(*self.login_user_loc).click() 74 75 def login_exit(self): 76 """ 77 退出系統 78 :return: 79 """ 80 above = self.find_element(*self.login_exit_loc) 81 ActionChains(self.driver).move_to_element(above).perform() 82 sleep(2) 83 self.find_element(*self.login_exit_button_loc).click() 84 85 def user_login(self,phone,password): 86 """ 87 登陸入口 88 :param username: 用戶名 89 :param password: 密碼 90 :return: 91 """ 92 self.open() 93 self.dig_login() 94 self.login_phone(phone) 95 self.login_password(password) 96 sleep(1) 97 self.keeplogin() 98 sleep(1) 99 self.login_button() 100 sleep(1) 101 102 phone_pawd_error_hint_loc = (By.XPATH,testData.get_CheckElementinfo(0)) 103 user_login_success_loc = (By.ID,testData.get_CheckElementinfo(1)) 104 exit_login_success_loc = (By.ID,testData.get_CheckElementinfo(2)) 105 106 # 手機號或密碼錯誤提示 107 def phone_pawd_error_hint(self): 108 return self.find_element(*self.phone_pawd_error_hint_loc).text 109 110 # 登陸成功用戶名 111 def user_login_success_hint(self): 112 return self.find_element(*self.user_login_success_loc).text 113 114 # 退出登陸 115 def exit_login_success_hint(self): 116 return self.find_element(*self.exit_login_success_loc).text
而後,在page_obj目錄下新增一個loginPage.py文件,是用來封裝登陸頁面對象類,執行登陸測試流程操做。
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 __author__ = 'YinJia' 4 5 6 import os,sys 7 sys.path.append(os.path.dirname(os.path.dirname(__file__))) 8 import unittest,ddt,yaml 9 from config import setting 10 from public.models import myunit,screenshot 11 from public.page_obj.loginPage import login 12 from public.models.log import Log 13 14 try: 15 f =open(setting.TEST_DATA_YAML + '/' + 'login_data.yaml',encoding='utf-8') 16 testData = yaml.load(f) 17 except FileNotFoundError as file: 18 log = Log() 19 log.error("文件不存在:{0}".format(file)) 20 21 @ddt.ddt 22 class Demo_UI(myunit.MyTest): 23 """抽屜新熱榜登陸測試""" 24 def user_login_verify(self,phone,password): 25 """ 26 用戶登陸 27 :param phone: 手機號 28 :param password: 密碼 29 :return: 30 """ 31 login(self.driver).user_login(phone,password) 32 33 def exit_login_check(self): 34 """ 35 退出登陸 36 :return: 37 """ 38 login(self.driver).login_exit() 39 40 @ddt.data(*testData) 41 def test_login(self,datayaml): 42 """ 43 登陸測試 44 :param datayaml: 加載login_data登陸測試數據 45 :return: 46 """ 47 log = Log() 48 log.info("當前執行測試用例ID-> {0} ; 測試點-> {1}".format(datayaml['id'],datayaml['detail'])) 49 # 調用登陸方法 50 self.user_login_verify(datayaml['data']['phone'],datayaml['data']['password']) 51 po = login(self.driver) 52 if datayaml['screenshot'] == 'phone_pawd_success': 53 log.info("檢查點-> {0}".format(po.user_login_success_hint())) 54 self.assertEqual(po.user_login_success_hint(), datayaml['check'][0], "成功登陸,返回實際結果是->: {0}".format(po.user_login_success_hint())) 55 log.info("成功登陸,返回實際結果是->: {0}".format(po.user_login_success_hint())) 56 screenshot.insert_img(self.driver, datayaml['screenshot'] + '.jpg') 57 log.info("-----> 開始執行退出流程操做") 58 self.exit_login_check() 59 po_exit = login(self.driver) 60 log.info("檢查點-> 找到{0}元素,表示退出成功!".format(po_exit.exit_login_success_hint())) 61 self.assertEqual(po_exit.exit_login_success_hint(), '註冊',"退出登陸,返回實際結果是->: {0}".format(po_exit.exit_login_success_hint())) 62 log.info("退出登陸,返回實際結果是->: {0}".format(po_exit.exit_login_success_hint())) 63 else: 64 log.info("檢查點-> {0}".format(po.phone_pawd_error_hint())) 65 self.assertEqual(po.phone_pawd_error_hint(),datayaml['check'][0] , "異常登陸,返回實際結果是->: {0}".format(po.phone_pawd_error_hint())) 66 log.info("異常登陸,返回實際結果是->: {0}".format(po.phone_pawd_error_hint())) 67 screenshot.insert_img(self.driver,datayaml['screenshot'] + '.jpg') 68 69 if __name__=='__main__': 70 unittest.main()
最後,在testcase目錄下建立測試用例文件login_sta.py,採用ddt數據驅動讀取yaml測試數據文件
綜上所述,編寫用例方法只須要按以上四個步驟建立->編寫便可。
執行以下主程序,可看輸出的實際結果。
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 __author__ = 'YinJia' 4 5 import os,sys 6 sys.path.append(os.path.dirname(__file__)) 7 from config import setting 8 import unittest,time 9 from package.HTMLTestRunner import HTMLTestRunner 10 from public.models.newReport import new_report 11 from public.models.sendmail import send_mail 12 13 # 測試報告存放文件夾,如不存在,則自動建立一個report目錄 14 if not os.path.exists(setting.TEST_REPORT):os.makedirs(setting.TEST_REPORT + '/' + "screenshot") 15 16 def add_case(test_path=setting.TEST_DIR): 17 """加載全部的測試用例""" 18 discover = unittest.defaultTestLoader.discover(test_path, pattern='*_sta.py') 19 return discover 20 21 def run_case(all_case,result_path=setting.TEST_REPORT): 22 """執行全部的測試用例""" 23 now = time.strftime("%Y-%m-%d %H_%M_%S") 24 filename = result_path + '/' + now + 'result.html' 25 fp = open(filename,'wb') 26 runner = HTMLTestRunner(stream=fp,title='抽屜新熱榜UI自動化測試報告', 27 description='環境:windows 7 瀏覽器:chrome', 28 tester='Jason') 29 runner.run(all_case) 30 fp.close() 31 report = new_report(setting.TEST_REPORT) #調用模塊生成最新的報告 32 send_mail(report) #調用發送郵件模塊 33 34 if __name__ =="__main__": 35 cases = add_case() 36 run_case(cases)