Python+selenium之测试报告
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python+selenium之测试报告相关的知识,希望对你有一定的参考价值。
1 # -*- coding: utf-8 -*- 2 import HTMLTestReport 3 import HTMLTestRunner 4 import os 5 import sys 6 import time 7 import unittest 8 from selenium import webdriver 9 10 11 class Baidu(unittest.TestCase): 12 def setUp(self): 13 self.driver = webdriver.Firefox() 14 self.driver.implicitly_wait(30) 15 self.driver.maximize_window() 16 # self.base_url = "https://www.baidu.com" 17 # self.driver.get(self.base_url) 18 self.driver.get("https://www.baidu.com") 19 20 def test_case1(self): 21 """设计测试失败case""" # *****效果是在测试报告中显示显示出测试名称***** 22 print("========【case_0001】打开百度搜索 =============") 23 # current_time = time.strftime("%Y-%M-%D-%H-%M-%S", time.localtime(time.time())) 24 # "."表示创建的路径为当.py文件所处的地址,\\\\是用\\将“\\”转义 25 # pic_path = ‘.\\\\result\\\\image\\\\‘ + current_time + ‘.png‘ 26 current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) 27 pic_path = ‘.\\\\result\\\\image\\\\‘ + ‘2017-07-17\\\\‘ + current_time + ‘.png‘ 28 print(pic_path) # 打印图片的地址 29 time.sleep(2) 30 self.driver.save_screenshot(pic_path) # 截图,获取测试结果 31 self.assertEqual(‘百度一下,你就知道‘, self.driver.title) # 断言判断测试是否成功,判断标题是否为百度(设计失败的case) 32 33 def test_case2(self): 34 """设计测试过程中报错的case""" 35 print("========【case_0002】搜索selenium =============") 36 self.driver.find_element_by_id("kw").clear() 37 self.driver.find_element_by_id("kw").send_keys(u"selenium") 38 self.driver.find_element_by_id(‘su‘).click() 39 time.sleep(2) 40 # current_time = time.strftime("%Y-%M-%D-%H-%M-%S", time.localtime(time.time())) 41 current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) 42 # "."表示创建的路径为当.py文件所处的地址,\\\\是用\\将“\\”转义 43 pic_path = ‘.\\\\result\\\\image\\\\‘+‘2017-07-17\\\\‘ + current_time + ‘.png‘ 44 print(pic_path) # 打印图片的地址 45 time.sleep(2) 46 self.driver.save_screenshot(pic_path) # 截图,获取测试结果 47 self.assertIn(‘selenium‘, self.driver.title) # 断言书写错误,导致case出错 48 49 def test_case3(self): 50 """设计测试成功的case""" 51 print("========【case_0003】 搜索梦雨情殇博客=============") 52 self.driver.find_element_by_id("kw").clear() 53 self.driver.find_element_by_id("kw").send_keys(u"梦雨情殇") 54 self.driver.find_element_by_id(‘su‘).click() 55 # current_time = time.strftime("%Y-%M-%D-%H-%M-%S", time.localtime(time.time())) 56 current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) 57 # "."表示创建的路径为当.py文件所处的地址,\\\\是用\\将“\\”转义 58 pic_path = ‘.\\\\result\\\\image\\\\2017-07-17\\\\‘ + current_time + ‘.png‘ 59 print(pic_path) # 打印图片的地址 60 time.sleep(2) 61 self.driver.save_screenshot(pic_path) # 截图,获取测试结果 62 63 self.assertIn(‘梦雨情殇‘, self.driver.title) 64 65 def tearDown(self): 66 self.driver.quit() 67 68 69 if __name__ == "__main__": 70 ‘‘‘生成测试报告‘‘‘ 71 current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) 72 testunit = unittest.TestSuite() 73 testunit.addTest(Baidu("test_case1")) 74 testunit.addTest(Baidu("test_case2")) 75 testunit.addTest(Baidu("test_case3")) 76 report_path = ".\\\\result\\\\SoftTestReport_" + current_time + ‘.html‘ # 生成测试报告的路径 77 fp = open(report_path, "wb") 78 runner = HTMLTestReport.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description=‘自动化测试演示报告‘, tester=‘fyr‘) 79 # runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description=‘自动化测试演示报告‘) 80 runner.run(testunit) 81 fp.close()
注意事项:
1.获取当前时间的格式为:
%Y-%m-%d-%H_%M_%S 而不是 %Y-%M-%D-%H-%M-%S
2.填写的截图存放地址“\\”,要用转义字符“\\”进行转义,变为“\\\\”
3.runner = HTMLTestReport.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description=‘自动化测试演示报告‘, tester=‘fyr‘)的测试报告如图所示:
4.runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description=‘自动化测试演示报告‘) 的效果图如图所示:
以上是关于Python+selenium之测试报告的主要内容,如果未能解决你的问题,请参考以下文章
selenium + python之BSTestRunner生成测试报告时报错:NameError: name 'unicode' is not defined
selenium + python之BSTestRunner生成测试报告时报错:NameError: name 'unicode' is not defined