python selenium自動(dòng)化測(cè)試框架搭建的方法步驟
設(shè)計(jì)思路
本文整理歸納以往的工作中用到的東西,現(xiàn)匯總成基礎(chǔ)測(cè)試框架提供分享。
框架采用python3 + selenium3 + PO + yaml + ddt + unittest等技術(shù)編寫成基礎(chǔ)測(cè)試框架,能適應(yīng)日常測(cè)試工作需要。
1、使用Page Object模式將頁面定位和業(yè)務(wù)操作分開,分離測(cè)試對(duì)象(元素對(duì)象)和測(cè)試腳本(用例腳本),一個(gè)頁面建一個(gè)對(duì)象類,提高用例的可維護(hù)性;
2、使用yaml管理頁面控件元素?cái)?shù)據(jù)和測(cè)試用例數(shù)據(jù)。例如元素ID等發(fā)生變化時(shí),不需要去修改測(cè)試代碼,只需要在對(duì)應(yīng)的頁面元素yaml文件中修改即可;
3、分模塊管理,互不影響,隨時(shí)組裝,即拿即用。
GitHub項(xiàng)目地址:https://github.com/yingoja/DemoUI
測(cè)試框架分層設(shè)計(jì)
- 把常見的操作和查找封裝成基礎(chǔ)類,不管是什么產(chǎn)品,可直接拿來復(fù)用
- 業(yè)務(wù)層主要是封裝對(duì)象頁面類,一個(gè)頁面建一個(gè)類,業(yè)務(wù)層頁面繼承基礎(chǔ)層
- 用例層針對(duì)產(chǎn)品頁面功能進(jìn)行構(gòu)造摸擬執(zhí)行測(cè)試
- 框架層提供基礎(chǔ)組件,支撐整個(gè)流程執(zhí)行及功能擴(kuò)展,給用例層提供各頁面的元素?cái)?shù)據(jù)、用例測(cè)試數(shù)據(jù),測(cè)試報(bào)告輸出等
測(cè)試框架目錄結(jié)構(gòu)
如下思維導(dǎo)圖目錄結(jié)構(gòu)介紹:
編寫用例方法
login.yaml
testinfo: - id: test_login001 title: 登錄測(cè)試 info: 打開抽屜首頁 testcase: - element_info: login-link-a find_type: ID operate_type: click info: 打開登錄對(duì)話框 - element_info: mobile find_type: ID operate_type: send_keys info: 輸入手機(jī)號(hào) - 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: 鼠標(biāo)懸停賬戶菜單 - 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: 檢查輸入手機(jī)號(hào)或密碼,登錄異常提示 - element_info: userProNick find_type: ID info: 成功登錄 - element_info: reg-link-a find_type: ID info: 檢查退出登錄是否成功
例如,我們要新增登錄功能測(cè)試用例:
首先,只需在testyaml目錄下新增一個(gè)頁面對(duì)象yaml文件,參考login.yaml格式編寫即可。這些文件是提供給封裝頁面對(duì)象類調(diào)用并執(zhí)行定位識(shí)別操作。
login_data.yaml
- id: test_login001.1 detail : 手機(jī)號(hào)和密碼為空登錄 screenshot : phone_pawd_empty data: phone: "" password: "" check : - 手機(jī)號(hào)不能為空 - id: test_login001.2 detail : 手機(jī)號(hào)為空登錄 screenshot : phone_empty data : phone: "" password : aa check : - 手機(jī)號(hào)不能為空 - id: test_login001.3 detail : 密碼為空登錄 screenshot : pawd_empty data : phone : 13511112222 password: "" check : - 密碼不能為空 - id: test_login001.4 detail : 非法手機(jī)號(hào)登錄 screenshot : phone_error data : phone : abc password: aa check : - 手機(jī)號(hào)格式不對(duì) - id: test_login001.5 detail : 手機(jī)號(hào)或密碼不匹配 screenshot : pawd_error data : phone : 13511112222 password: aa check : - 賬號(hào)密碼錯(cuò)誤 - id: test_login001.6 detail : 手機(jī)號(hào)和密碼正確 screenshot : phone_pawd_success data : phone : 13865439800 password: ******** check : - yingoja login_data.yaml login_data.yaml
其次,在testdata目錄下新增一個(gè)login_data.yaml文件提供給登錄接口傳參的測(cè)試數(shù)據(jù),編寫格式參考login_data.yaml文件。
loginPage.py
#!/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) # 定位器,通過元素屬性定位元素對(duì)象 # 手機(jī)號(hào)輸入框 login_phone_loc = (By.ID,testData.get_elementinfo()) # 密碼輸入框 login_password_loc = (By.ID,testData.get_elementinfo()) # 取消自動(dòng)登錄 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): """ 登錄手機(jī)號(hào) :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): """ 退出系統(tǒng) :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)) # 手機(jī)號(hào)或密碼錯(cuò)誤提示 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目錄下新增一個(gè)loginPage.py文件,是用來封裝登錄頁面對(duì)象類,執(zhí)行登錄測(cè)試流程操作。
login_sta.py
#!/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): """抽屜新熱榜登錄測(cè)試""" def user_login_verify(self,phone,password): """ 用戶登錄 :param phone: 手機(jī)號(hào) :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): """ 登錄測(cè)試 :param datayaml: 加載login_data登錄測(cè)試數(shù)據(jù) :return: """ log = Log() log.info("當(dāng)前執(zhí)行測(cè)試用例ID-> {0} ; 測(cè)試點(diǎn)-> {1}".format(datayaml['id'],datayaml['detail'])) # 調(diào)用登錄方法 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í)際結(jié)果是->: {0}".format(po.user_login_success_hint())) log.info("成功登錄,返回實(shí)際結(jié)果是->: {0}".format(po.user_login_success_hint())) screenshot.insert_img(self.driver, datayaml['screenshot'] + '.jpg') log.info("-----> 開始執(zhí)行退出流程操作") self.exit_login_check() po_exit = login(self.driver) log.info("檢查點(diǎn)-> 找到{}元素,表示退出成功!".format(po_exit.exit_login_success_hint())) self.assertEqual(po_exit.exit_login_success_hint(), '注冊(cè)',"退出登錄,返回實(shí)際結(jié)果是->: {0}".format(po_exit.exit_login_success_hint())) log.info("退出登錄,返回實(shí)際結(jié)果是->: {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'][] , "異常登錄,返回實(shí)際結(jié)果是->: {}".format(po.phone_pawd_error_hint())) log.info("異常登錄,返回實(shí)際結(jié)果是->: {0}".format(po.phone_pawd_error_hint())) screenshot.insert_img(self.driver,datayaml['screenshot'] + '.jpg') if __name__=='__main__': unittest.main()
最后,在testcase目錄下創(chuàng)建測(cè)試用例文件login_sta.py,采用ddt數(shù)據(jù)驅(qū)動(dòng)讀取yaml測(cè)試數(shù)據(jù)文件
綜上所述,編寫用例方法只需要按以上四個(gè)步驟創(chuàng)建->編寫即可。
執(zhí)行如下主程序,可看輸出的實(shí)際結(jié)果。
#!/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 # 測(cè)試報(bào)告存放文件夾,如不存在,則自動(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): """加載所有的測(cè)試用例""" discover = unittest.defaultTestLoader.discover(test_path, pattern='*_sta.py') return discover def run_case(all_case,result_path=setting.TEST_REPORT): """執(zhí)行所有的測(cè)試用例""" 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)化測(cè)試報(bào)告', description='環(huán)境:windows 7 瀏覽器:chrome', tester='Jason') runner.run(all_case) fp.close() report = new_report(setting.TEST_REPORT) #調(diào)用模塊生成最新的報(bào)告 send_mail(report) #調(diào)用發(fā)送郵件模塊 if __name__ =="__main__": cases = add_case() run_case(cases)
測(cè)試結(jié)果展示
HTML報(bào)告日志
HTML報(bào)告點(diǎn)擊截圖,彈出截圖
測(cè)試報(bào)告通過的日志
自動(dòng)截圖存放指定的目錄
郵件測(cè)試報(bào)告
到此這篇關(guān)于python selenium自動(dòng)化測(cè)試框架搭建的方法步驟的文章就介紹到這了,更多相關(guān)python selenium自動(dòng)化測(cè)試框架搭建內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
作者:YinJia
出處:http://www.cnblogs.com/yinjia/
- python3+selenium自動(dòng)化測(cè)試框架詳解
- selenium+python自動(dòng)化測(cè)試之使用webdriver操作瀏覽器的方法
- selenium python 實(shí)現(xiàn)基本自動(dòng)化測(cè)試的示例代碼
- selenium+python自動(dòng)化測(cè)試之頁面元素定位
- selenium+python自動(dòng)化測(cè)試之環(huán)境搭建
- python3 selenium自動(dòng)化測(cè)試 強(qiáng)大的CSS定位方法
- 使用Python+selenium實(shí)現(xiàn)第一個(gè)自動(dòng)化測(cè)試腳本
- python+django+selenium搭建簡易自動(dòng)化測(cè)試
- selenium自動(dòng)化測(cè)試簡單準(zhǔn)備
相關(guān)文章
pytorch 實(shí)現(xiàn)凍結(jié)部分參數(shù)訓(xùn)練另一部分
這篇文章主要介紹了pytorch 實(shí)現(xiàn)凍結(jié)部分參數(shù)訓(xùn)練另一部分,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03Python字符串和字典相關(guān)操作的實(shí)例詳解
這篇文章主要介紹了Python字符串和字典相關(guān)操作的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-09-09使用OpenCV實(shí)現(xiàn)道路車輛計(jì)數(shù)的使用方法
這篇文章主要介紹了使用OpenCV實(shí)現(xiàn)道路車輛計(jì)數(shù)的使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07一個(gè)基于flask的web應(yīng)用誕生 使用模板引擎和表單插件(2)
一個(gè)基于flask的web應(yīng)用誕生第二篇,這篇文章主要介紹了如何使用jinja2模板引擎和wtf表單插件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04pytest解讀一次請(qǐng)求多個(gè)fixtures及多次請(qǐng)求
這篇文章主要為大家介紹了一次請(qǐng)求多個(gè)fixtures,以及fixtures被多次請(qǐng)求的pytest官方解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06