最近博主完成了一個web端UI自動化平台測試環境搭建,包括常用的操作流程組合成方法,組織測試用例,利用框架及報告文件生成,最后通過郵件添加附件的格式發送。
首先UI自動化平台的核心是selenium+python、里面一些元素定位的操作博主也就不做介紹了,有很多大神都有詳細的文檔來輔助、博主這里想總結的是如何通過模塊的方式來寫通用的方法來簡化測試工作。
就拿最簡單的登錄注冊頁面來舉例,可能需要設計的用例:不存在的用戶名、正確的用戶名加錯誤的密碼、正確的用戶名加空密碼、正確的用戶名加正確的密碼。 博主這里就簡單的列一些常用的用例(別吐槽用例覆蓋不全),
然后來通過 selenium的 定位API來寫用例代碼,每一個用例都包括:進入登錄頁、用戶名的操作、密碼的操作、確認登錄,博主想說的就是類似這樣同一個頁面、不同的操作可以通過定義方法的形式來整合代碼,
把相應元素但不同操作用定義的變量來區別。
下面是一個類似的例子:
1 # ERP自動化登錄接口 2 @classmethod 3 def erp_login(cls, username, password): 4 driver = cls.driver 5 driver.find_element_by_id("btn-front").click() 6 7 # 登錄名稱 8 driver.find_element_by_id("DloginName").send_keys(username) 9 # 登錄密碼 10 driver.find_element_by_id("Dpassword").send_keys(password) 11 12 driver.find_element_by_id("Flogin").click()
同樣比如一個表單新增功能,涉及的字段多了,同樣可以通過定義方法來整合,如此可以大大優化代碼,否則相似的用例會出現大量重復的操作步驟代碼,而且隨着方法的不斷增加,測試用例也變成不同方法的組合
例如:
1 # ERP自動化前台測試用例 2 def test_case_1(self): 3 u"""菜餚條件:時價主菜稱重備注(535465);單獨下單""" 4 TestErp.erp_login(self, "237nj03", "000000") 5 TestErp.erp_front(self) 6 TestErp.erp_front_open_dish_Desk(self, "512788") 7 TestErp.erp_front_dish_pwfb(self, "10", "1.5", "1", "2") 8 TestErp.erp_front_dish_order(self) 9 TestErp.erp_front_account_clearDesk(self, "512788")
這是一個用例集,是在單元測試框架unittest里的,之前博主也介紹過,博主負責的項目是一個智能餐飲系統
TestErp.erp_login 這個是登錄的方法
TestErp.erp_front 這個是進入前台的方法
TestErp.erp_front_open_dish_Desk 這個是餐飲系統中選擇變量(desk_id)來開台
TestErp.erp_front_dish_pwfb 這個是選擇其中一種菜餚信息,輸入 price 價格、weight 稱重、 配菜信息
TestErp.erp_front_dish_order 這個是下單操作
TestErp.erp_front_account_clearDesk 這個是進入結賬頁、選擇現金結賬打印並清台
之后就是通過構建用例集來執行用例,並發送郵件
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 from unittest_erp import TestErp 5 import HTMLTestRunner 6 import unittest 7 from datetime import datetime 8 from time import sleep 9 from unittest_smtp import unittest_smtp 10 11 # 定義測試報告的文件名稱 12 reportname = datetime.today().strftime("%Y%m%d%H%M%S") + ".html" 13 # 定義測試任務集 14 testreport = unittest.TestSuite() 15 16 # 給測試任務集添加測試用例任務的名稱 17 testreport.addTest(TestErp("test_case_1")) 18 testreport.addTest(TestErp("test_case_2")) 19 testreport.addTest(TestErp("test_case_3")) 20 testreport.addTest(TestErp("test_case_4")) 21 testreport.addTest(TestErp("test_case_5")) 22 testreport.addTest(TestErp("test_case_6")) 23 testreport.addTest(TestErp("test_case_7")) 24 testreport.addTest(TestErp("test_case_8")) 25 testreport.addTest(TestErp("test_case_9")) 26 testreport.addTest(TestErp("test_case_10")) 27 testreport.addTest(TestErp("test_case_11")) 28 testreport.addTest(TestErp("test_case_12")) 29 testreport.addTest(TestErp("test_case_13")) 30 testreport.addTest(TestErp("test_case_14")) 31 testreport.addTest(TestErp("test_case_15")) 32 testreport.addTest(TestErp("test_case_16")) 33 34 # 定義測試報告的文件路徑 35 file_path = "D:\\pythonproject\\Wito_erp\\wito_selenium\\" + reportname 36 fp = file(file_path, "wb") 37 runner = HTMLTestRunner.HTMLTestRunner(stream=fp, 38 title=u"測試報告", 39 description=u"用例執行情況:" 40 ) 41 # 執行測試任務集 42 runner.run(testreport) 43 44 fp.close() # 關閉文件 45 46 # 根據文件路徑、定義附件名稱發送郵件 47 unittest_smtp(file_path, reportname)
1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 4 from email.mime.text import MIMEText 5 from email.mime.multipart import MIMEMultipart 6 from email.header import Header 7 from email.utils import parseaddr, formataddr 8 import smtplib 9 10 11 def _format_address(text): 12 name, address = parseaddr(text) 13 return formataddr((Header(name, "utf-8").encode(), address)) 14 15 16 def unittest_smtp(file, filename): 17 from_address = "xx@xx.com" 18 # 發件箱地址 19 password = "123456!a" 20 # 授權密碼 不是郵箱登錄密碼 21 # to_address = "xx@xx.com" 22 to_address = "xx@xx.com" 23 # 收件箱地址 24 smtp_server = "smtp.ym.163.com" 25 # 發件箱服務器地址 26 27 # message = MIMEText(mail_msg, "html", "utf-8") 28 29 message = MIMEMultipart() 30 # msg = MIMEText("測試smtp郵件發送功能", "plain", "utf-8") 31 # 第一個參數:郵件正文 32 # 第二個參數:郵件類型 純文本 33 # 第三個參數:編碼 34 35 message["From"] = _format_address("自動化測試報告 <%s>" % from_address) 36 # 發件人姓名與地址 37 message["To"] = _format_address("xx <%s>" % to_address) 38 # 收件人姓名與地址 39 message["Subject"] = Header("xx自動化測試平台郵箱服務", "utf-8").encode() 40 # 郵件標題 41 message.attach(MIMEText("xx自動化測試任務執行完畢,附測試報告文件", "plain", "utf-8")) 42 # 接收html格式文件 43 44 att1 = MIMEText(open(file, "rb").read(), "base64", "utf-8") 45 # 文件地址 46 att1["Content-Type"] = "application/octet-stream" 47 att1["Content-Disposition"] = "attachment; filename=%s" % filename 48 # 定義附件名稱 49 50 message.attach(att1) 51 # 添加附件 52 try: 53 server = smtplib.SMTP(smtp_server, 25) 54 # 構造smtp服務器連接 55 server.set_debuglevel(1) 56 # 打開debug輸出模式 57 server.login(from_address, password) 58 # 登錄smtp服務器 59 server.sendmail(from_address, to_address, message.as_string()) 60 # 發送郵件 61 62 print "郵件發送成功" 63 server.quit() 64 except smtplib.SMTPException: 65 print "Error: 無法發送郵件"
最后附上郵件圖片