简体   繁体   中英

JMeter jp@gc Parametrized controller - construct object from variables

How to construct an object with attributes taken only from jp@gc-Parametrized controller user defined variables list, that can be used for building dynamically xml/json data structures for HTTP Sampler later on?

Controller variables: 在此处输入图片说明

XML structure for HTTP request body:

Example 1:

 <?xml version="1.0" encoding="UTF-8"?>
    <login>
        <username>someUser</username>
        <password>1234</password>
    </login>

It's saving JMeter variables, so you can use ${} syntax

<userName>${username}</userName>
<password>${password}</password>

To get dynamically variables in XML Body use the following sample

import java.util.stream.StreamSupport;
String xmlBody="<?xml version=\"1.0\" encoding=\"UTF-8\"?><login>";
StreamSupport.stream(Spliterators.spliteratorUnknownSize(vars.getIterator(), Spliterator.ORDERED), false).forEach(
    e -> xmlBody+="<" +e.getKey() + ">" +e.getValue() + "</" +e.getKey() + ">");
xmlBody += "</login>";
log.info(xmlBody);

Assuming your above Parameterized Controller setup you can refer the declared variables like:

<?xml version="1.0" encoding="UTF-8"?>
<login>
    <username>${username}</username>
    <password>${password}</password>
</login>

在此处输入图片说明

Which will result into variables substitution in the runtime with their respective values which can be checked using i..e View Results Tree listener :

在此处输入图片说明

Check out JMeter Parameterization - The Complete Guide article for more information on various approaches to JMeter test parameterization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM