1. 程式人生 > >python3+selenium+unitest+HtmlTestRunner實現網頁功能自動化流程

python3+selenium+unitest+HtmlTestRunner實現網頁功能自動化流程

1.selenium+HtmlTestRunner模組安裝匯入

2.對應版本     谷歌瀏覽器和谷歌驅動chromedriver.exe的下載(不適用對應版本會報錯,無法呼叫瀏覽器)

3.程式碼執行成功後,生成Html功能自動化測試報告

import os
import sys
import unittest
import time
from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner


sys.path.append('E:/Py-workspace/test/testUI.py')
ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))


class 
MyTest(unittest.TestCase): driver = webdriver.Chrome() def setUp(self): pass def test_case1(self): """開啟百度網頁""" self.driver.get("https://www.baidu.com/") assert "百度一下,你就知道" in self.driver.title def test_case2(self): """搜尋功能""" self.driver.get("https://www.baidu.com/"
) self.driver.find_element_by_id("kw").send_keys("taozhen") self.driver.find_element_by_id("su").click() def test_quit(self): """關閉瀏覽器""" self.driver.quit() def tearDown(self): # self.driver.quit() # self.driver.refresh() # 將退出瀏覽器的操作變成重新整理瀏覽器,用於不同用例之間的接洽操作
pass if __name__ == '__main__': # 構造測試集 suite = unittest.TestSuite() # 構造測試集 suite.addTest(MyTest("test_case1")) # 加入測試用例 suite.addTest(MyTest("test_case2")) suite.addTest(MyTest("test_quit")) # 執行測試 date = time.strftime("%Y%m%d") # 定義date為日期,time為時間 time = time.strftime("%Y%m%d_%H%M%S") path = "./report/ui/" # 判斷是否定義的路徑目錄存在,不能存在則建立 if not os.path.exists(path): os.makedirs(path) else: pass report_path = path+time+"UIreport.html" # 將執行結果儲存到report,名字為定義的路徑和檔名,執行指令碼 report_title = u"測試報告" desc = u'功能自動化測試報告詳情:' with open(report_path, 'wb') as report: runner = HTMLTestRunner(stream=report, title=report_title, description=desc) runner.run(suite) # 關閉report,指令碼結束 report.close()

生成的Html測試報告如下