pyhton3+selenium的web頁面自動化測試框架
web自動化測試框架
pyhton3+selenium3+unittest+HTMLTestRunner
環境部署:
python3+Selenium+unittest+HTMLTestRunner+pageObject Web自動化測試框架
(Page Object設計模式)
環境部署: python3、selenium3
開發工具: Pycharm
整合工具: Jenkins
測試程式碼託管平臺:GitHub
通過主從伺服器執行測試
測試框架結構:
-
common資料夾存放公有元素,如url,測試報告發送郵件地址,使用資訊等;
-
file資料夾存放測試過程中需要使用的檔案,如圖片,txt,zip檔案等
-
page資料夾用於存放測試過程中需要使用的頁面元素
-
report資料夾用於存放測試生成的測試報告
-
testcase資料夾中存放測試用例
-
HTMLTestRunner.py是將測試結果生成為html版的測試報告的檔案
-
run.py 執行run.py檔案可以執行全部測試用例
Page Object 設計模式:
由於被測頁面會更新,為保持測試指令碼的健壯性和可修改性,我們使用了Page Object設計模式,將被測頁面的測試指令碼與指令碼中所使用到的頁面元素解耦, 將被測頁面的測試指令碼檔案放在testcase資料夾中,將測試指令碼使用到的被測頁面元素放置在page資料夾中。
為了測試用例寫起來更加方便,我們在page資料夾中放置了BasePage.py檔案,BasePage.py檔案將查詢元素進一步簡化,page資料夾中的其他Page檔案可以呼叫BasePage.py中的方法將程式碼進一步簡化。
下面以百度首頁作為示例,演示本框架中Page Object模式的使用。
test_baiduSearch.py
# coding=utf-8 from HTMLTestRunner import HTMLTestRunner from selenium import webdriver from page.searchPage import SearchPage import time import unittest from page.searchPage import * class TestLogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.url = SearchPage.url self.driver.maximize_window() self.page = SearchPage(self.driver) self.page.get(self.url) def tearDown(self): self.driver.close() def test_search(self): # 使用pageObject模式時的web頁面自動化測試程式碼 self.page.search = self.page.search_content self.page.search_btn.click() time.sleep(2) # 斷言 self.assertIn(self.page.search_content_assert, self.driver.page_source) # # 未使用pageObject模式時的web頁面自動化測試程式碼 # self.driver.find_element_by_id("kw").send_keys("hello") # self.driver.find_element_by_id("su").click() # time.sleep(2) # self.assertIn("hello", self.driver.page_source) def test_search1(self): # 錯誤的斷言導致測試用例failed self.page.search = self.page.search_content self.page.search_btn.click() time.sleep(2) self.assertIn(self.page.search_content_assert_wrong, self.driver.page_source) # # 未使用pageObject模式的web頁面自動化測試程式碼 # self.driver.find_element_by_id("kw").send_keys("hello") # self.driver.find_element_by_id("su").click() # time.sleep(2) # self.assertIn("hello1232323", self.driver.page_source) def test_search2(self): # 元素值錯誤,導致的自動化測試用例error self.page.wrong_search = self.page.search_content self.page.search_btn.click() time.sleep(2) self.assertIn(self.page.search_content_assert, self.driver.page_source) # # 未使用pageObject模式的web頁面自動化測試程式碼 # self.driver.find_element_by_id("k").send_keys("hello") # self.driver.find_element_by_id("su").click() # time.sleep(2) # self.assertIn("hello", self.driver.page_source) if __name__ == '__main__': # 使用以下語句生成本頁面的測試報告 # now = time.strftime("%Y-%m-%d-%H-%M-%S") # suite = unittest.TestSuite() # suite.addTest(TestLogin("test_search")) # suite.addTest(TestLogin("test_search1")) # suite.addTest(TestLogin("test_search2")) # path = "../report/" + now + "result.html" # fp = open(path, 'wb') # # runner = HTMLTestRunner(stream=fp, title=u"Web頁面自動化測試", description=u"測試查詢功能") # runner.run(suite) # fp.close() unittest.main()
searchPage.py
from common.pageObject import PageObject, PageElement
from common.url import *
class SearchPage(PageObject):
# 當前測試頁面的測試網址url
base_url = Url.base_url
url = base_url+'/'
# 查詢元素
search = PageElement(id="kw")
wrong_search = PageElement(id="k")
search_btn = PageElement(id='su')
# 查詢內容
search_content = "hello"
# 斷言
search_content_assert = "hello"
search_content_assert_wrong = "hello12232423423"
執行所有測試用例:
執行run.py檔案即可執行所有測試用例,將所有的測試指令碼檔案統一使用test開頭的檔案命進行命名。
run.py
import unittest
import HTMLTestRunner
import time
from common.sendEmail import SendEmail
def get_test_cases(dirpath):
# dirpath是存放測試用例的檔案路徑
test_cases = unittest.TestSuite()
# 測試用例均使用"test_"開頭命名
suites = unittest.defaultTestLoader.discover(dirpath, 'test_*.py', top_level_dir=dirpath)
for suite in suites:
test_cases.addTests(suite)
return test_cases
if __name__ == '__main__':
cases = get_test_cases('../testcase')
now = time.strftime("%Y-%m-%d %H_%M_%S") # 報告生成時間
test_reports_address = '../report' # 測試報告存放位置
filename = '../report/' + now + 'report.html' # 設定報告檔名
fp = open(filename, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'Web自動化測試', description=u'詳細測試結果如下:')
runner.run(cases)
fp.close()
# 向指定郵箱傳送測試報告的html檔案
time.sleep(6)
# 查詢最新生成的測試報告地址
new_report_addr = SendEmail().acquire_report_address(test_reports_address)
# 自動傳送郵件
SendEmail().send_email(new_report_addr)
執行完成後會在report資料夾中生成html版本的測試報告,如下所示:
這裡能夠生成HTML版本的測試報告主要是引用了HTMLTestRunner.py檔案,由於官網給出的HTMLTestRunner.py檔案是python2版本的,所以需要進行改動,以下是改動過的HTMLTestRunner.py檔案生成的測試報告。
點贊關注~~加入我們,瞭解更多。642830685。群內免費領取最新軟體測試大廠面試資料和Python自動化、介面、框架搭建學習資料!技術大牛解惑答疑,同行一起交流。
向指定郵箱傳送郵件:
生成測試報告後可以向指定郵箱傳送測試報告的html檔案,注意向指定郵箱傳送郵件是,需要對發郵件的郵箱進行設定,獲取發件箱的授權碼,具體操作請百度。
sendEmail.py
# email:***@163.com password:***
"""
使用一個郵箱向另一個郵箱傳送測試報告的html檔案,這裡需要對傳送郵件的郵箱進行設定,獲取郵箱授權碼。
username=“傳送郵件的郵箱”, password=“郵箱授權碼”
這裡要特別注意password不是郵箱密碼而是郵箱授權碼。
mail_server = "傳送郵箱的伺服器地址"
這裡常用的有 qq郵箱——"stmp.qq.com", 163郵箱——"stmp.163.com"
其他郵箱伺服器地址可自行百度
"""
import os
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time
# 自動傳送郵件
class SendEmail():
def send_email(self, new_report):
# 讀取測試報告中的內容作為郵件的內容
with open(new_report, 'r', encoding='utf8') as f:
mail_body = f.read()
# 發件人地址
send_addr = '***@163.com'
# 收件人地址
reciver_addr = '***@163.com'
# 傳送郵箱的伺服器地址 qq郵箱是'smtp.qq.com', 163郵箱是'smtp.163.com'
mail_server = 'smtp.163.com'
now = time.strftime("%Y-%m-%d %H_%M_%S")
# 郵件標題
subject = 'web自動化測試報告測試報告' + now
# 發件人的郵箱及郵箱授權碼
username = '***@163.com'
password = '***' # 注意這裡是郵箱的授權碼而不是郵箱密碼
# 郵箱的內容和標題
message = MIMEText(mail_body, 'html', 'utf8')
message['Subject'] = Header(subject, charset='utf8')
# 傳送郵件,使用的使smtp協議
smtp = smtplib.SMTP()
smtp.connect(mail_server)
smtp.login(username, password)
smtp.sendmail(send_addr, reciver_addr.split(','), message.as_string())
smtp.quit()
# 獲取最新的測試報告地址
def acquire_report_address(self, reports_address):
# 測試報告資料夾中的所有檔案加入到列表
test_reports_list = os.listdir(reports_address)
# 按照升序排序生成新的列表
new_test_reports_list = sorted(test_reports_list)
# 獲取最新的測試報告
the_last_report = new_test_reports_list[-1]
# 最新的測試報告地址
the_last_report_address = os.path.join(reports_address, the_last_report)
return the_last_report_address
Jenkins部署:
測試指令碼編寫完成之後可以將自動化測試指令碼託管到git上,然後整合到主站上的Jenkins,配置主從伺服器,設定構建時間,讓Jenkins可以自動去git下載測試程式碼,在從伺服器中執行,並將測試結果反饋給主站。
點贊關注~~加入我們,瞭解更多。642830685。群內免費領取最新軟體測試大廠面試資料和Python自動化、介面、框架搭建學習資料!技術大牛解惑答疑,同行一起交流。