• Python+selenium之测试报告(3)


    较测试报告(2),该文章将测试报告和测试截图存放在随机变动的文件夹下面,去除了要存放在指定文件夹下面的限制。

    注:遇到问题有:

    1.创建由时间自动拼接的多级文件夹

    2.

     1 import os
     2 import time
     3 
     4 current_time = time.strftime("%Y-%m-%d-%H_%M", time.localtime(time.time()))
     5 current_time1 = time.strftime("%H_%M_%S", time.localtime(time.time()))
     6 pic_path1 = '.\' + current_time + "\result\"
     7 pic_path = pic_path1 + "image"+"\" + current_time1 + '.png'
     8 os.makedirs(pic_path)
     9 print(pic_path)

    拼接好的路径要使用os.makedirs()创建对应的文件夹目录

    path = pic_path1 + current_time1 + '.png'  
    创建好的文件夹无法再使用该路径继续创建下级路径,只能存放对应的内容
    3.测试报告必须得放在测图文件的外层目录下,否者在测试报告中查找图片会报如下的错误

    整个代码如下:

     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 current_time = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))
    11 pic_path = '.\' + current_time + "\result\"
    12 pic_path1 = '.\' + current_time + "\result\image\"
    13 os.makedirs(pic_path)
    14 os.makedirs(pic_path1)
    15 
    16 class Baidu(unittest.TestCase):
    17     def setUp(self):
    18         self.driver = webdriver.Firefox()
    19         self.driver.implicitly_wait(30)
    20         self.driver.maximize_window()
    21         self.driver.get("https://www.baidu.com")
    22         time.sleep(5)
    23 
    24     def test_case1(self):
    25         """设计测试case"""  # *****效果是在测试报告中显示显示出测试名称*****
    26         print("========【case_0001】打开百度搜索 =============")
    27         # "."表示创建的路径为当.py文件所处的地址,\是用将“”转义
    28         current_time1 = time.strftime("%H_%M_%S", time.localtime(time.time()))
    29         path = pic_path1 + current_time1 + '.png'
    30         print(path)  # 打印图片的地址
    31         time.sleep(5)
    32         self.driver.save_screenshot(path)  # 截图,获取测试结果
    33         time.sleep(2)
    34         self.assertEqual('百度一下,你就知道', self.driver.title)  # 断言判断测试是否成功,判断标题是否为百度(设计失败的case)
    35 
    36     def test_case2(self):
    37         """设计测试过程中报错的case"""
    38         global pic_path
    39         print("========【case_0002】搜索selenium =============")
    40         self.driver.find_element_by_id("kw").clear()
    41         self.driver.find_element_by_id("kw").send_keys(u"selenium")
    42         self.driver.find_element_by_id('su').click()
    43         time.sleep(2)
    44         # "."表示创建的路径为当.py文件所处的地址,\是用将“”转义
    45         current_time1 = time.strftime("%H_%M_%S", time.localtime(time.time()))
    46         path = pic_path1 + current_time1 + '.png'
    47         print(path)  # 打印图片的地址
    48         time.sleep(5)
    49         self.driver.save_screenshot(path)  # 截图,获取测试结果
    50         time.sleep(2)
    51         self.assertIn('selenium', self.driver.title)  # 断言书写错误,导致case出错
    52 
    53     def test_case3(self):
    54         """设计测试成功的case"""
    55         print("========【case_0003】 搜索梦雨情殇博客=============")
    56         self.driver.find_element_by_id("kw").clear()
    57         self.driver.find_element_by_id("kw").send_keys(u"明道")
    58         self.driver.find_element_by_id('su').click()
    59         # "."表示创建的路径为当.py文件所处的地址,\是用将“”转义
    60         current_time1 = time.strftime("%H_%M_%S", time.localtime(time.time()))
    61         path = pic_path1 + current_time1 + '.png'
    62         print(path)  # 打印图片的地址
    63         time.sleep(5)
    64         self.driver.save_screenshot(path)  # 截图,获取测试结果
    65         time.sleep(3)
    66         print(self.driver.title)
    67 
    68         self.assertIn('明道', self.driver.title)
    69 
    70     def tearDown(self):
    71         self.driver.quit()
    72 
    73 
    74 if __name__ == "__main__":
    75     '''生成测试报告'''
    76     current_time1 = time.strftime("%H_%M_%S", time.localtime(time.time()))
    77     testunit = unittest.TestSuite()
    78     testunit.addTest(Baidu("test_case1"))
    79     testunit.addTest(Baidu("test_case2"))
    80     testunit.addTest(Baidu("test_case3"))
    81     report_path = pic_path + "SoftTestReport_" + current_time1 + '.html'  # 生成测试报告的路径
    82     fp = open(report_path, "wb")
    83     runner = HTMLTestReport.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description='自动化测试演示报告', tester='fyr')
    84     # runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description='自动化测试演示报告')
    85     runner.run(testunit)
    86     fp.close()
    
    
    
    
    
    
     
  • 相关阅读:
    在模板生成页面的时候,页面里的标签可能会生成多个id,这时候使用id选择器,往往只能取到第一个id的元素。
    后台返回model里的时间格式,用@JsonFormat是没用的,它只有在返回JSON数据的时候生效,我脑抽了
    thymeleaf关于 Error resolving template “index”, template might not exist or might not be accessible by any of the configured Template Resolvers
    thymeleaf 配合 Spring Security 权限判断时,sec:authentication无法取到值(null)
    MySQL 常用30种SQL查询语句优化方法
    Linux常用命令
    APP微信登录---第三方登录
    关于文件的工具类例子
    Java时间戳与日期格式字符串的互转
    Java字符串与文件的互转操作
  • 原文地址:https://www.cnblogs.com/fengyiru6369/p/7453641.html
Copyright © 2020-2023  润新知