『IT女神-仅以此文致敬 女性开发者』
她们在不同领域熠熠生辉,创造了无限的可能,成为了IT发展进步中不可或缺的角色。
她们在AI代码的世界里让梦想生根、开花。
不分年龄、无论背景,她们释放着热情与专业,用技术为自己代言。
致敬每一位IT女神,愿她们永远美丽、永远绽放自己独特的魅力之花。
同时,在这特殊的节日祝愿所有的女性朋友梦想成真!
注意事项:
【本文字数包含代码38175字,建议慢慢看~哈哈哈】
1、以下仅为举例,具体以自身实际项目为准;
2、以下内容重点是介绍HTMLReport的应用,并不是说明如何搭建框架;
3、如果想了解框架内容,可移步博主有关测试框架的系列文章;
4、写了一个用例,用例中没有加断言,只是为了生成测试报告,可以忽略;
5、用例故意写错了3个,1个通过,是为了生成测试报告数据。
目录/脚本 | 说明 |
---|---|
common/reportOut.py | 这是是用HTMLReport 生成报告的 |
common/sendMain.py | 这个是用来发邮件,本次演示可要可不要 |
report | 是存放测试报告的,里边有3个文件,由HTMLReport 自动生成 |
testcase | 存放测试用例的 |
main.py | 框架主入口 |
技术 | 版本及说明 |
---|---|
Python | V3.x(本文为3.7)===编程语言支撑 |
Selenium | V3.141.0 ===UI元素、控件的识别、定位,以及浏览器控制等 |
HTMLReport | 生成Html测试报告 |
Unittest | Python 自带===自动化测试框架 |
Smtplib | Python 自带===邮件服务 |
email | Python 自带===邮件服务 |
os | Python 自带===系统模块 |
PyCharm | Community 2020.2汉化版 |
操作系统 | Windows10 旗舰版64位 |
其它 | 后续补充 |
HtmlTestRunner
改为HTMLReport
;# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2022/3/7
# 文件名称:reportOut.py
# 作用:封装测试报告功能import time
import unittest
from common import HTMLTestRunner # 引入导入的报告模板def report_out(test_dir, report_dir, name_project):''':test_dir: 用例路径:report_dir : 报告路径:name_project : 项目名称=>用于报告命名及描述:return: 无'''now = time.strftime("%Y_%m_%d %H_%M_%S")discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py') # 加载测试用例report_name = report_dir + now + '-' + name_project+'_test_report.html' # 报告名称with open(report_name,'wb') as f: # 运行用例生成测试报告runner = HTMLTestRunner.HTMLTestRunner(stream=f,title=name_project + 'WebUI Auto Testing Report',description=(name_project + U"美多商城UI自动化功能回归测试"),verbosity=2)runner.run(discover)f.close()"""stream:要操作的文件;title:测试报告标题;description:报告描述;verbosity:报告级别。"""
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/7
# 文件名称:reportOut.py
# 作用:封装测试报告功能import time
import unittest
from HTMLReport import ddt, TestRunner, add_image, no_retry, retrydef report_out(test_dir, report_dir, name_project):''':test_dir: 用例路径:report_dir : 报告路径:name_project : 项目名称=>用于报告命名及描述:return: 无'''now = time.strftime("%Y_%m_%d %H_%M_%S")discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py') # 加载测试用例# report_name = now + '-' + name_project + '_test_report.html' # 报告名称test_runner = TestRunner(report_file_name=now,output_path=report_dir,title=name_project,description="关于HTMLReport的实际项目应用",thread_count=1,thread_start_wait=0.1,tries=0,delay=0,back_off=1,retry=True,sequential_execution=True,lang="cn")test_runner.run(discover)
class TestRunner(TemplateMixin, TestSuite):"""测试执行器"""def __init__(self, report_file_name: str = None, log_file_name: str = None, output_path: str = None,title: str = None, description: str = None, tries: int = 0, delay: float = 1, back_off: float = 1,max_delay: float = 120, retry: bool = True, thread_count: int = 1, thread_start_wait: float = 0,sequential_execution: bool = False, lang: str = "cn", image: bool = True, failed_image: bool = False):
参数 | 说明 |
---|---|
report_file_name | 报告文件名,如果未赋值,将采用“test +时间戳” |
log_file_name | 日志文件名,如果未赋值,将采用报告文件名,如果报告文件名也没有,将采用“test +时间戳” |
output_path | 报告保存文件夹名,默认“report ” |
title | 报告标题,默认“测试报告” |
description | 报告描述,默认“无测试描述” |
tries | 重试次数 |
delay | 重试延迟间隔,单位为 秒 |
back_off | 扩展每次重试等待时间的乘数 |
max_delay | 最大重试等待时间长度,单位为 秒 |
retry | 如果为 True 表示所有用例遵循重试规则,False 只针对添加了 @retry 用例有效 |
thread_count | 并发线程数量(无序执行测试),默认数量 1 |
thread_start_wait | 各线程启动延迟,默认 0 s |
sequential_execution | 是否按照套件添加(addTests) 顺序执行, 会等待一个addTests 执行完成,再执行下一个,默认 False 。如果用例中存在 tearDownClass ,建议设置为True, 否则 tearDownClass 将会在所有用例线程执行完后才会执行。 |
lang | ("cn", "en") 支持中文与英文报告输出,默认采用中文 |
image | 默认支持添加图片,False 放弃所有图片添加 |
failed_image | true 只有失败才添加图片,成功用例添加的图片会被删除 |
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/7
# 文件名称:reportOut.py
# 作用:封装测试报告功能import time
import unittest
from HTMLReport import ddt, TestRunner, add_image, no_retry, retrydef report_out(test_dir, report_dir, name_project):''':test_dir: 用例路径:report_dir : 报告路径:name_project : 项目名称=>用于报告命名及描述:return: 无'''now = time.strftime("%Y_%m_%d %H_%M_%S")discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py') # 加载测试用例# report_name = now + '-' + name_project + '_test_report.html' # 报告名称test_runner = TestRunner(report_file_name=now,output_path=report_dir,title=name_project,description="关于HTMLReport的实际项目应用",thread_count=1,thread_start_wait=0.1,tries=0,delay=0,back_off=1,retry=True,sequential_execution=True,lang="cn")test_runner.run(discover)
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/7
# 文件名称:sendMain.py
# 作用:封装邮件服务模块import time
import smtplib
import getpass
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import email
import osdef send_main(file_path, mail_to='xxx@126.com'):mail_from = 'xxxx@126.com'f = open(file_path, 'rb')mail_body = f.read()f.close()# msg = email.MIMEMultipart.MIMEMultipart()msg = MIMEMultipart()# 构造MIMEBase对象做为文件附件内容并附加到根容器contype = 'application/octet-stream'maintype, subtype = contype.split('/', 1)# 读入文件内容并格式化data = open(file_path, 'rb')# file_msg = email.MIMEBase.MIMEBase(maintype, subtype)file_msg = MIMEBase(maintype, subtype)file_msg.set_payload(data.read())data.close()# email.Encoders.encode_base64(file_msg)encoders.encode_base64(file_msg)# 设置附件头basename = os.path.basename(file_path)file_msg.add_header('Content-Disposition', 'attachment', filename=basename)msg.attach(file_msg)print(u'msg 附件添加成功')msg1 = MIMEText(mail_body, "html", 'utf-8')msg.attach(msg1)if isinstance(mail_to, str):msg['To'] = mail_toelse:msg['To'] = ','.join(mail_to)msg['From'] = mail_frommsg['Subject'] = u'美多商城UI自动化功能回归测试'msg['date'] = time.strftime('%Y-%m-%d-%H_%M_%S')print(msg['date'])smtp = smtplib.SMTP()smtp.connect('smtp.126.com')smtp.login('xxx@126.com', 'xxx') # 登录账号和密码(密码为之前申请的授权码)smtp.sendmail(mail_from, mail_to, msg.as_string())smtp.quit()print('email has send out !')# if __name__=='__main__':
# sendmain('../report/2017-08-18-10_18_57_result.html')
关于HTMLReport的实际项目应用
关于HTMLReport的实际项目应用
启动时间:Start Time: 2023-03-07 17:15:55
结束时间:End Time: 2023-03-07 17:16:09
运行时长:Duration: 0:00:13.857089
结果:Status:合计:Total:4 通过:Passed:1 失败:Failed:0 错误:Error:3 跳过:Skipped:0
关于HTMLReport的实际项目应用
下载日志文件Download log file
筛选摘要Summary通过Pass失败FAIL异常Error跳过Skip全部All
序号NO 测试组/测试用例Test Group/Test case 计数Count 通过Passed 失败Failed 错误Erroneous 跳过Skipped 统计Statistics 重试Tries 查看View c1 test_baidu.TestCase 4 1 0 3 0 25.00% 0 细节Detail
et1.1 test_back_refresh 0 错误error
et1.1.1:
2023-03-07 17:15:58,071 23748 INFO result.py(70) - 测试延迟启动:0.1s
2023-03-07 17:15:58,183 23748 INFO result.py(73) - 开始测试: test_back_refresh (test_baidu.TestCase)
2023-03-07 17:15:58,329 23748 ERROR result.py(191) - 测试产生错误: test_back_refresh (test_baidu.TestCase)
Traceback (most recent call last):File "F:\Automated-UITest-demo-update - htmlreport\testcase\test_baidu.py", line 43, in test_back_refreshself.driver.find_element_by_id("kw").send_keys("csdn") # 输入csdnFile "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_idreturn self.find_element(by=By.ID, value=id_)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element'value': value})['value']File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in executeself.error_handler.check_response(response)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_responseraise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="kw"]"}(Session info: chrome=110.0.5481.178)2023-03-07 17:15:58,330 23748 INFO result.py(96) - 测试结束: test_back_refresh (test_baidu.TestCase)
2023-03-07 17:15:58,330 23748 INFO result.py(97) - 耗时: 0.14696788787841797
et1.2 test_search 0 错误error
et1.2.1:
2023-03-07 17:15:58,330 23748 INFO result.py(70) - 测试延迟启动:0.1s
2023-03-07 17:15:58,434 23748 INFO result.py(73) - 开始测试: test_search (test_baidu.TestCase)
2023-03-07 17:16:00,445 23748 ERROR result.py(191) - 测试产生错误: test_search (test_baidu.TestCase)
Traceback (most recent call last):File "F:\Automated-UITest-demo-update - htmlreport\testcase\test_baidu.py", line 26, in test_searchself.driver.find_element_by_id("kw").send_keys("helloworld") # 输入“helloworld”File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_idreturn self.find_element(by=By.ID, value=id_)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element'value': value})['value']File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in executeself.error_handler.check_response(response)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_responseraise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="kw"]"}(Session info: chrome=110.0.5481.178)2023-03-07 17:16:00,445 23748 INFO result.py(96) - 测试结束: test_search (test_baidu.TestCase)
2023-03-07 17:16:00,445 23748 INFO result.py(97) - 耗时: 2.0103440284729004
et1.3 test_serach_clear 0 错误error
et1.3.1:
2023-03-07 17:16:00,445 23748 INFO result.py(70) - 测试延迟启动:0.1s
2023-03-07 17:16:00,561 23748 INFO result.py(73) - 开始测试: test_serach_clear (test_baidu.TestCase)
2023-03-07 17:16:00,566 23748 ERROR result.py(191) - 测试产生错误: test_serach_clear (test_baidu.TestCase)
Traceback (most recent call last):File "F:\Automated-UITest-demo-update - htmlreport\testcase\test_baidu.py", line 49, in test_serach_clearself.driver.find_element_by_id("kw").send_keys("csdn") # 输入csdnFile "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_idreturn self.find_element(by=By.ID, value=id_)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element'value': value})['value']File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in executeself.error_handler.check_response(response)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_responseraise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="kw"]"}(Session info: chrome=110.0.5481.178)2023-03-07 17:16:00,567 23748 INFO result.py(96) - 测试结束: test_serach_clear (test_baidu.TestCase)
2023-03-07 17:16:00,567 23748 INFO result.py(97) - 耗时: 0.0060002803802490234
pt1.4 test_windows_size 0 通过pass
pt1.4.1:
2023-03-07 17:16:00,567 23748 INFO result.py(70) - 测试延迟启动:0.1s
2023-03-07 17:16:00,670 23748 INFO result.py(73) - 开始测试: test_windows_size (test_baidu.TestCase)
2023-03-07 17:16:05,441 23748 INFO result.py(172) - 测试执行通过: test_windows_size (test_baidu.TestCase)
2023-03-07 17:16:05,442 23748 INFO result.py(96) - 测试结束: test_windows_size (test_baidu.TestCase)
2023-03-07 17:16:05,442 23748 INFO result.py(97) - 耗时: 4.770826101303101
合计Total 4 1 0 3 0 25.00% 0
2023-03-07 17:15:55,952 25872 INFO test_runner.py(162) - 预计并发线程数:1
2023-03-07 17:15:58,071 23748 INFO result.py(70) - 测试延迟启动:0.1s
2023-03-07 17:15:58,183 23748 INFO result.py(73) - 开始测试: test_back_refresh (test_baidu.TestCase)
2023-03-07 17:15:58,329 23748 ERROR result.py(191) - 测试产生错误: test_back_refresh (test_baidu.TestCase)
Traceback (most recent call last):File "F:\Automated-UITest-demo-update - htmlreport\testcase\test_baidu.py", line 43, in test_back_refreshself.driver.find_element_by_id("kw").send_keys("csdn") # 输入csdnFile "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_idreturn self.find_element(by=By.ID, value=id_)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element'value': value})['value']File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in executeself.error_handler.check_response(response)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_responseraise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="kw"]"}(Session info: chrome=110.0.5481.178)2023-03-07 17:15:58,330 23748 INFO result.py(96) - 测试结束: test_back_refresh (test_baidu.TestCase)
2023-03-07 17:15:58,330 23748 INFO result.py(97) - 耗时: 0.14696788787841797
2023-03-07 17:15:58,330 23748 INFO result.py(70) - 测试延迟启动:0.1s
2023-03-07 17:15:58,434 23748 INFO result.py(73) - 开始测试: test_search (test_baidu.TestCase)
2023-03-07 17:16:00,445 23748 ERROR result.py(191) - 测试产生错误: test_search (test_baidu.TestCase)
Traceback (most recent call last):File "F:\Automated-UITest-demo-update - htmlreport\testcase\test_baidu.py", line 26, in test_searchself.driver.find_element_by_id("kw").send_keys("helloworld") # 输入“helloworld”File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_idreturn self.find_element(by=By.ID, value=id_)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element'value': value})['value']File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in executeself.error_handler.check_response(response)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_responseraise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="kw"]"}(Session info: chrome=110.0.5481.178)2023-03-07 17:16:00,445 23748 INFO result.py(96) - 测试结束: test_search (test_baidu.TestCase)
2023-03-07 17:16:00,445 23748 INFO result.py(97) - 耗时: 2.0103440284729004
2023-03-07 17:16:00,445 23748 INFO result.py(70) - 测试延迟启动:0.1s
2023-03-07 17:16:00,561 23748 INFO result.py(73) - 开始测试: test_serach_clear (test_baidu.TestCase)
2023-03-07 17:16:00,566 23748 ERROR result.py(191) - 测试产生错误: test_serach_clear (test_baidu.TestCase)
Traceback (most recent call last):File "F:\Automated-UITest-demo-update - htmlreport\testcase\test_baidu.py", line 49, in test_serach_clearself.driver.find_element_by_id("kw").send_keys("csdn") # 输入csdnFile "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_idreturn self.find_element(by=By.ID, value=id_)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element'value': value})['value']File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in executeself.error_handler.check_response(response)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_responseraise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="kw"]"}(Session info: chrome=110.0.5481.178)2023-03-07 17:16:00,567 23748 INFO result.py(96) - 测试结束: test_serach_clear (test_baidu.TestCase)
2023-03-07 17:16:00,567 23748 INFO result.py(97) - 耗时: 0.0060002803802490234
2023-03-07 17:16:00,567 23748 INFO result.py(70) - 测试延迟启动:0.1s
2023-03-07 17:16:00,670 23748 INFO result.py(73) - 开始测试: test_windows_size (test_baidu.TestCase)
2023-03-07 17:16:05,441 23748 INFO result.py(172) - 测试执行通过: test_windows_size (test_baidu.TestCase)
2023-03-07 17:16:05,442 23748 INFO result.py(96) - 测试结束: test_windows_size (test_baidu.TestCase)
2023-03-07 17:16:05,442 23748 INFO result.py(97) - 耗时: 4.770826101303101
2023-03-07 17:16:09,611 25872 INFO test_runner.py(199) -
Pass test_windows_size (test_baidu.TestCase)2023-03-07 17:16:09,612 25872 ERROR test_runner.py(201) -
Error test_back_refresh (test_baidu.TestCase)
Error test_search (test_baidu.TestCase)
Error test_serach_clear (test_baidu.TestCase)2023-03-07 17:16:09,614 25872 INFO test_runner.py(219) -
测试结束!
运行时间: 0:00:13.857089
共计执行用例数量:4
执行成功用例数量:1
执行失败用例数量:0
跳过执行用例数量:0
产生异常用例数量:3
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/7
# 文件名称:test_baidu.py
# Function:打开百度网主页,在搜索栏输入“helloworld”from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import unittestclass TestCase(unittest.TestCase):@classmethoddef setUpClass(cls) -> None:cls.driver = webdriver.Chrome() # 打开Chrome浏览器cls.driver.get("http://www.baidu.com") # 输入百度网址print("============验证浏览器的基本控制==========")@classmethoddef tearDownClass(cls) -> None:cls.driver.quit() # 关闭浏览器def test_search(self):print("1、搜索helloworld.并回车......")time.sleep(2)self.driver.find_element_by_id("kw").send_keys("helloworld") # 输入“helloworld”time.sleep(2)self.driver.find_element_by_id("kw").send_keys(Keys.ENTER) # 回车进行搜索time.sleep(2)self.driver.maximize_window() # 最大化当前窗口def test_windows_size(self):print("2、浏览器窗口大小缩小为640*480......")time.sleep(2)self.driver.set_window_size(640, 480) # 控制浏览器显示尺寸为640*480time.sleep(0.5)self.driver.maximize_window() # 最大化当前窗口time.sleep(2)def test_back_refresh(self):print("3、先进行浏览器后退,再次输入csdn进行搜索")self.driver.back()self.driver.find_element_by_id("kw").send_keys("csdn") # 输入csdntime.sleep(1)self.driver.refresh() # 刷新def test_serach_clear(self):print("4、清空输入的内容......")self.driver.find_element_by_id("kw").send_keys("csdn") # 输入csdntime.sleep(2)self.driver.find_element_by_id("kw").clear()time.sleep(0.5)def csdn(self):print("5、进入csdn官网")self.driver.find_element_by_id("kw").send_keys("csdn") # 输入csdntime.sleep(2)self.driver.find_element_by_id("kw").send_keys(Keys.ENTER) # 回车进行搜索time.sleep(2)self.driver.find_element_by_xpath("//*[@id='1']/h3/a[1]").click()time.sleep(2)windows = self.driver.window_handlesself.driver.switch_to.window(windows[-1])now_url = self.driver.current_urlm_get_url = "https://www.csdn.net/"if now_url == m_get_url:print("经过判断,已经进入csdn官网!!")else:print("未进入到csdn官网,请检查代码!")if __name__ == "__main__":unittest.main()
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/3/7
# 文件名称:main.py
# 作用:框架的主入口函数import time
from common.reportOut import report_out
from common.sendMain import send_main
import osdef acquire_report_address(reports_address):#这里方法略获取最新的测试报告,作为邮件的附件def run_case():print("======开始执行!!!======")curpath = os.path.dirname(os.path.realpath(__file__))report_dir = os.path.join(curpath, "report/") # 测试报告存放目录test_dir = os.path.join(curpath, "testcase/") # 测试用例读取目录name_project = "关于HTMLReport的实际项目应用"report_out(test_dir, report_dir, name_project)time.sleep(5)# 这里方法略,调用邮件方法即可print("======执行结束!!!======")if __name__ == '__main__':run_case()
会在report
目录下生成三个文件;
命令行输出:
测试报告:
『《假如生活欺骗了你》俄·普希金』
假如生活欺骗了你,
不要悲伤,不要心急!
忧郁的日子里须要镇静:
相信吧,快乐的日子将会来临!
心儿永远向往着未来;
现在却常是忧郁。
一切都是瞬息,一切都将会过去;
而那过去了的,就会成为亲切的怀恋。