Python生成JMeter测试脚本----HTTP信息头管理器和用户定义的变量

前言

之前的文章写了怎么用python生成JMeter脚本,生成了测试计划、线程组、Http监听器组件、响应断言、查看结果树、汇总报告、聚合报告、响应时间图。本篇内容接着讲怎么用python生成HTTP信息头管理器和用户定义的变量组件。

HTTP信息头管理器

信息头管理器的内容比较简单,主要包括了名称,注释和请求头参数。使用set方法设置好参数后,用get方法生成组件。

python 复制代码
import xml.etree.ElementTree as ET
from base import General


class JMeterHeaderManager:
    def __init__(self):
        self.__name = 'HTTP信息头管理器'
        self.__comments = ''
        self.__headers = {}

    def get_name(self):
        return self.__name

    def set_name(self, name):
        self.__name = name

    def get_comments(self):
        return self.__comments

    def set_comments(self, comments):
        self.__comments = comments

    def get_headers(self):
        return self.__headers

    def set_headers(self, headers):
        self.__headers = headers

    def get(self, parent):
        header_manager_prop = ET.SubElement(parent, 'HeaderManager')
        header_manager_prop.set('guiclass', 'HeaderPanel')
        header_manager_prop.set('testclass', 'HeaderManager')
        header_manager_prop.set('testname', self.__name)
        if self.__comments != '':
            comments_prop = General.add_str_prop(self, header_manager_prop, 'TestPlan.comments', self.__comments)
        collection_prop = ET.SubElement(header_manager_prop, 'collectionProp')
        collection_prop.set('name', 'HeaderManager.headers')
        if self.__headers:
            for key, value in self.__headers.items():
                element_prop = ET.SubElement(collection_prop, 'elementProp')
                element_prop.set('name', '')
                element_prop.set('elementType', 'Header')
                header_value_prop = General.add_str_prop(self, element_prop, 'Header.name', key)
                header_value_prop = General.add_str_prop(self, element_prop, 'Header.value', value)
        hashtree = ET.SubElement(parent, 'hashTree')
        return header_manager_prop

用户定义的变量

用户定义的变量格式和信息头管理器一致,使用方法也一致,对于变量的描述,因为没有实际的用途,所以就不去实现了。

python 复制代码
import xml.etree.ElementTree as ET
from base import General


class JMeterArguments:
    def __init__(self):
        self.__name = '用户定义的变量'
        self.__comments = ''
        self.__arguments = {}

    def get_name(self):
        return self.__name

    def set_name(self, name):
        self.__name = name

    def get_comments(self):
        return self.__comments

    def set_comments(self, comments):
        self.__comments = comments

    def get_arguments(self):
        return self.__arguments

    def set_arguments(self, arguments):
        self.__arguments = arguments

    def get(self, parent):
        arguments_prop = ET.SubElement(parent, 'Arguments')
        arguments_prop.set('guiclass', 'ArgumentsPanel')
        arguments_prop.set('testclass', 'Arguments')
        arguments_prop.set('testname', self.__name)
        arguments_prop.set('enabled', 'true')
        if self.__comments != '':
            comments_prop = General.add_str_prop(self, arguments_prop, 'TestPlan.comments', self.__comments)
        collection_prop = ET.SubElement(arguments_prop, 'collectionProp')
        collection_prop.set('name', 'Arguments.arguments')
        if self.__arguments:
            for key, value in self.__arguments.items():
                element_prop = ET.SubElement(collection_prop, 'elementProp')
                element_prop.set('name', key)
                element_prop.set('elementType', 'Argument')
                arguments_value_prop = General.add_str_prop(self, element_prop, 'Argument.name', key)
                arguments_value_prop = General.add_str_prop(self, element_prop, 'Argument.value', value)
                arguments_value_prop = General.add_str_prop(self, element_prop, 'Argument.desc', '')
                arguments_value_prop = General.add_str_prop(self, element_prop, 'Argument.metadata', '=')
        hashtree = ET.SubElement(parent, 'hashTree')
        return arguments_prop

生成jmeter脚本demo

python 复制代码
import xml.etree.ElementTree as ET
import JMeter
from JMeterThreadGroup import JMeterThreadGroup
from JMeterTestPlan import JMeterTestPlan
from JMeterHttpSampler import JMeterHttpSampler
from JMeterResponseAssert import JMeterResponseAssert
from JMeterViewResultsFullVisualizer import JMeterViewResultsFullVisualizer
from JMeterSummaryReport import JMeterSummaryReport
from JMeterStatVisualizer import JMeterStatVisualizer
from JMeterRespTimeGraphVisualizer import JMeterRespTimeGraphVisualizer
from JMeterHeaderManager import JMeterHeaderManager
from JMeterArguments import JMeterArguments
import base


class GenJMeter:
    jmeter_script = JMeter.get()
    hash_tree = ET.SubElement(jmeter_script, 'hashTree')
    test_plan = JMeterTestPlan()
    test_plan = test_plan.get(hash_tree)
    thread_group_tree = ET.SubElement(hash_tree, 'hashTree')
    thread_group = JMeterThreadGroup().get(thread_group_tree)
    test_hashtree = ET.SubElement(thread_group_tree, 'hashTree')
    http_sampler = JMeterHttpSampler()
    http_sampler.set_params({'test': 123})
    http_sampler.set_files({'file': 'test.txt'})
    http_sampler.get(test_hashtree)
    response_assert = JMeterResponseAssert()
    response_assert.set_assert_name('断言')
    response_assert.set_assert_text(['test', '123'])
    response_assert.set_test_type('2')
    response_assert.get(test_hashtree)
    result_collector_prop = JMeterViewResultsFullVisualizer()
    result_collector_prop.get(test_hashtree)
    summary_report = JMeterSummaryReport()
    summary_report.get(test_hashtree)
    stat_visualizer = JMeterStatVisualizer()
    stat_visualizer.get(test_hashtree)
    resp_time_graph = JMeterRespTimeGraphVisualizer()
    resp_time_graph.get(test_hashtree)
    header_manager = JMeterHeaderManager()
    header_manager.set_headers({'Content-Type': 'application/json'})
    header_manager.get(test_hashtree)
    arguments = JMeterArguments()
    arguments.set_arguments({'test': '1111'})
    arguments.get(thread_group_tree)
    bases = base.General()
    tree = ET.ElementTree(jmeter_script)
    bases.indent(jmeter_script)
    with open('file.jmx', "wb") as f:
        tree.write(f, encoding="utf-8", xml_declaration=True, method="xml")

其他组件实现方式待续,后续还有执行测试和生产测试报告

相关推荐
秃头佛爷41 分钟前
Python学习大纲总结及注意事项
开发语言·python·学习
深度学习lover2 小时前
<项目代码>YOLOv8 苹果腐烂识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果腐烂识别
API快乐传递者3 小时前
淘宝反爬虫机制的主要手段有哪些?
爬虫·python
阡之尘埃5 小时前
Python数据分析案例61——信贷风控评分卡模型(A卡)(scorecardpy 全面解析)
人工智能·python·机器学习·数据分析·智能风控·信贷风控
茶馆大橘7 小时前
微服务系列五:避免雪崩问题的限流、隔离、熔断措施
java·jmeter·spring cloud·微服务·云原生·架构·sentinel
丕羽8 小时前
【Pytorch】基本语法
人工智能·pytorch·python
bryant_meng8 小时前
【python】Distribution
开发语言·python·分布函数·常用分布
m0_594526309 小时前
Python批量合并多个PDF
java·python·pdf
工业互联网专业10 小时前
Python毕业设计选题:基于Hadoop的租房数据分析系统的设计与实现
vue.js·hadoop·python·flask·毕业设计·源码·课程设计
钱钱钱端10 小时前
【压力测试】如何确定系统最大并发用户数?
自动化测试·软件测试·python·职场和发展·压力测试·postman