前言
之前的文章写了怎么用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")
其他组件实现方式待续,后续还有执行测试和生产测试报告