|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import json |
| 4 | +from lxml import etree |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +def jmeter_test_plan(root_xml): |
| 9 | + JmeterTestPlan = root_xml |
| 10 | + JmeterTestPlan.set('version', '1.2') |
| 11 | + JmeterTestPlan.set('properties', '2.3') |
| 12 | + JmeterTestPlan.set('jmeter', '5.1.1 r1855137') |
| 13 | + return etree.SubElement(JmeterTestPlan, 'hashTree') |
| 14 | + |
| 15 | + |
| 16 | +def test_plan(parent_xml): |
| 17 | + testPlan = etree.SubElement(parent_xml, 'TestPlan') |
| 18 | + testPlan.set("guiclass", "TestPlanGui") |
| 19 | + testPlan.set("testclass", "TestPlan") |
| 20 | + testPlan.set("testname", "Test Plan") |
| 21 | + testPlan.set("enabled", "true") |
| 22 | + stringProp = etree.SubElement(testPlan, "stringProp") |
| 23 | + stringProp.set("name", "TestPlan.comments") |
| 24 | + stringProp.text = '' |
| 25 | + boolProp1 = etree.SubElement(testPlan, "boolProp") |
| 26 | + boolProp1.set("name", "TestPlan.functional_mode") |
| 27 | + boolProp1.text = "false" |
| 28 | + boolProp2 = etree.SubElement(testPlan, "boolProp") |
| 29 | + boolProp2.set("name", "TestPlan.tearDown_on_shutdown") |
| 30 | + boolProp2.text = "true" |
| 31 | + boolProp3 = etree.SubElement(testPlan, "boolProp") |
| 32 | + boolProp3.set("name", "TestPlan.serialize_threadgroups") |
| 33 | + boolProp3.text = "false" |
| 34 | + elementProp = etree.SubElement(testPlan, "elementProp") |
| 35 | + elementProp.set("name", "TestPlan.user_defined_variables") |
| 36 | + elementProp.set("elementType", "Arguments") |
| 37 | + elementProp.set("guiclass", "ArgumentsPanel") |
| 38 | + elementProp.set("testclass", "Arguments") |
| 39 | + elementProp.set("testname", "User Defined Variables") |
| 40 | + elementProp.set("enabled", "true") |
| 41 | + collectionProp = etree.SubElement(elementProp, "collectionProp") |
| 42 | + collectionProp.set("name", "Arguments.arguments") |
| 43 | + stringProp2 = etree.SubElement(testPlan, "stringProp") |
| 44 | + stringProp2.set("name", "TestPlan.user_define_classpath") |
| 45 | + stringProp2.text = '' |
| 46 | + return etree.SubElement(parent_xml, 'hashTree') |
| 47 | + |
| 48 | + |
| 49 | +def thread_group(parent_xml): |
| 50 | + theadGroup = etree.SubElement(parent_xml, "ThreadGroup") |
| 51 | + theadGroup.set("guiclass", "ThreadGroupGui") |
| 52 | + theadGroup.set("testclass", "ThreadGroup") |
| 53 | + theadGroup.set("testname", "Thread Group") |
| 54 | + theadGroup.set("enabled", "true") |
| 55 | + stringProp = etree.SubElement(theadGroup, "stringProp") |
| 56 | + stringProp.set("name", "ThreadGroup.on_sample_error") |
| 57 | + stringProp.text = "continue" |
| 58 | + elementProp = etree.SubElement(theadGroup, "elementProp") |
| 59 | + common_api(elementProp, |
| 60 | + {"name": "ThreadGroup.main_controller", "elementType": "LoopController", "guiclass": "LoopControlPanel", |
| 61 | + "testclass": "LoopController", "testname": "Loop Controller", "enabled": "true"}) |
| 62 | + boolProp = etree.SubElement(elementProp, "boolProp") |
| 63 | + boolProp.set("name", "LoopController.continue_forever") |
| 64 | + boolProp.text = "false" |
| 65 | + stringProp = etree.SubElement(elementProp, "stringProp") |
| 66 | + stringProp.set("name", "LoopController.loops") |
| 67 | + stringProp.text = "1" |
| 68 | + stringProp = etree.SubElement(theadGroup, "stringProp") |
| 69 | + stringProp.set("name", "ThreadGroup.num_threads") |
| 70 | + stringProp.text = "1" |
| 71 | + boolProp = etree.SubElement(theadGroup, "boolProp") |
| 72 | + boolProp.set("name", "ThreadGroup.scheduler") |
| 73 | + boolProp.text = "false" |
| 74 | + stringProp = etree.SubElement(theadGroup, "stringProp") |
| 75 | + stringProp.set("name", "ThreadGroup.duration") |
| 76 | + stringProp.text = '' |
| 77 | + stringProp = etree.SubElement(theadGroup, "stringProp") |
| 78 | + stringProp.set("name", "ThreadGroup.delay") |
| 79 | + stringProp.text = '' |
| 80 | + return etree.SubElement(parent_xml, "hashTree") |
| 81 | + |
| 82 | + |
| 83 | +def controller(parent_xml, result): |
| 84 | + for data in result: |
| 85 | + GenericController = etree.SubElement(parent_xml, "GenericController") |
| 86 | + GenericController.set("guiclass", "LogicControllerGui") |
| 87 | + GenericController.set("testclass", "GenericController") |
| 88 | + GenericController.set("testname", data.get("name")) |
| 89 | + GenericController.set("enabled", "true") |
| 90 | + stringProp = etree.SubElement(GenericController, "stringProp") |
| 91 | + stringProp.set("name", "TestPlan.comments") |
| 92 | + stringProp.text = data.get("description") |
| 93 | + shashTree = etree.SubElement(parent_xml, "hashTree") |
| 94 | + for sample in data.get("sample"): |
| 95 | + HTTPSamplerProxy = etree.SubElement(shashTree, "HTTPSamplerProxy") |
| 96 | + common_api(HTTPSamplerProxy, |
| 97 | + {"guiclass": "HttpTestSampleGui", "testclass": "HTTPSamplerProxy", |
| 98 | + "testname": sample.get('path'), |
| 99 | + "enabled": "true"}) |
| 100 | + if sample.get("params").__len__() < 1: |
| 101 | + elementProp = etree.SubElement(HTTPSamplerProxy, "elementProp") |
| 102 | + common_api(elementProp, {"name": "HTTPsampler.Arguments", "elementType": "Arguments", |
| 103 | + "guiclass": "HTTPArgumentsPanel", "testclass": "Arguments", |
| 104 | + "testname": "User Defined Variables", "enabled": "true"}) |
| 105 | + collectionProp = etree.SubElement(elementProp, "collectionProp") |
| 106 | + collectionProp.set("name", "Arguments.arguments") |
| 107 | + else: |
| 108 | + boolProp = etree.SubElement(HTTPSamplerProxy, "boolProp") |
| 109 | + boolProp.set("name", "HTTPSampler.postBodyRaw") |
| 110 | + boolProp.text = "true" |
| 111 | + elementProp = etree.SubElement(HTTPSamplerProxy, "elementProp") |
| 112 | + common_api(elementProp, {"name": "HTTPsampler.Arguments", "elementType": "Arguments"}) |
| 113 | + collectionProp = etree.SubElement(elementProp, "collectionProp") |
| 114 | + collectionProp.set("name", "Arguments.arguments") |
| 115 | + elementProp = etree.SubElement(collectionProp, "elementProp") |
| 116 | + elementProp.set("name", "") |
| 117 | + elementProp.set("elementType", "HTTPArgument") |
| 118 | + boolProp = etree.SubElement(elementProp, "boolProp") |
| 119 | + boolProp.set("name", "HTTPArgument.always_encode") |
| 120 | + boolProp.text = "false" |
| 121 | + stringProp = etree.SubElement(elementProp, "stringProp") |
| 122 | + stringProp.set("name", "Argument.value") |
| 123 | + # stringProp.text = json.dumps(sample.get("params")).replace("\"", """) |
| 124 | + stringProp.text = json.dumps(sample.get("params")) |
| 125 | + stringProp = etree.SubElement(elementProp, "stringProp") |
| 126 | + stringProp.set("name", "Argument.metadata") |
| 127 | + stringProp.text = "=" |
| 128 | + ###host |
| 129 | + stringProp = etree.SubElement(HTTPSamplerProxy, "stringProp") |
| 130 | + stringProp.set("name", "HTTPSampler.domain") |
| 131 | + stringProp.text = data.get("host") |
| 132 | + |
| 133 | + ###port |
| 134 | + stringProp = etree.SubElement(HTTPSamplerProxy, "stringProp") |
| 135 | + stringProp.set("name", "HTTPSampler.port") |
| 136 | + stringProp.text = data.get("port") |
| 137 | + |
| 138 | + ##protocol |
| 139 | + stringProp = etree.SubElement(HTTPSamplerProxy, "stringProp") |
| 140 | + stringProp.set("name", "HTTPSampler.protocol") |
| 141 | + stringProp.text = "http" |
| 142 | + |
| 143 | + # encoding |
| 144 | + stringProp = etree.SubElement(HTTPSamplerProxy, "stringProp") |
| 145 | + stringProp.set("name", "HTTPSampler.contentEncoding") |
| 146 | + stringProp.text = "utf8" |
| 147 | + |
| 148 | + # path |
| 149 | + stringProp = etree.SubElement(HTTPSamplerProxy, "stringProp") |
| 150 | + stringProp.set("name", "HTTPSampler.path") |
| 151 | + stringProp.text = sample.get("path") |
| 152 | + |
| 153 | + # method |
| 154 | + stringProp = etree.SubElement(HTTPSamplerProxy, "stringProp") |
| 155 | + stringProp.set("name", "HTTPSampler.method") |
| 156 | + stringProp.text = sample.get("method") |
| 157 | + |
| 158 | + # follow redirects |
| 159 | + boolProp = etree.SubElement(HTTPSamplerProxy, "boolProp") |
| 160 | + boolProp.set("name", "HTTPSampler.follow_redirects") |
| 161 | + boolProp.text = "true" |
| 162 | + |
| 163 | + # auto redirects |
| 164 | + boolProp = etree.SubElement(HTTPSamplerProxy, "boolProp") |
| 165 | + boolProp.set("name", "HTTPSampler.auto_redirects") |
| 166 | + boolProp.text = "false" |
| 167 | + |
| 168 | + # use keepalive |
| 169 | + boolProp = etree.SubElement(HTTPSamplerProxy, "boolProp") |
| 170 | + boolProp.set("name", "HTTPSampler.use_keepalive") |
| 171 | + boolProp.text = "true" |
| 172 | + |
| 173 | + # DO MULTIPART POST |
| 174 | + boolProp = etree.SubElement(HTTPSamplerProxy, "boolProp") |
| 175 | + boolProp.set("name", "HTTPSampler.DO_MULTIPART_POST") |
| 176 | + boolProp.text = "false" |
| 177 | + |
| 178 | + # embedded url re |
| 179 | + stringProp = etree.SubElement(HTTPSamplerProxy, "stringProp") |
| 180 | + stringProp.set("name", "HTTPSampler.embedded_url_re") |
| 181 | + stringProp.text = "" |
| 182 | + |
| 183 | + # connect timeout |
| 184 | + stringProp = etree.SubElement(HTTPSamplerProxy, "stringProp") |
| 185 | + stringProp.set("name", "HTTPSampler.connect_timeout") |
| 186 | + stringProp.text = "" |
| 187 | + |
| 188 | + # response timeout |
| 189 | + stringProp = etree.SubElement(HTTPSamplerProxy, "stringProp") |
| 190 | + stringProp.set("name", "HTTPSampler.response_timeout") |
| 191 | + stringProp.text = "" |
| 192 | + |
| 193 | + # comments |
| 194 | + stringProp = etree.SubElement(HTTPSamplerProxy, "stringProp") |
| 195 | + stringProp.set("name", "TestPlan.comments") |
| 196 | + stringProp.text = sample.get("sampler_comments") |
| 197 | + hash_tree = etree.SubElement(shashTree, "hashTree") |
| 198 | + |
| 199 | + |
| 200 | +def common_api(obj_xml, datas): |
| 201 | + for key, value in datas.items(): |
| 202 | + obj_xml.set(key, value) |
| 203 | + |
0 commit comments