In JMeter, I have a JSR223 Sampler which eventually sends a request to publish a message on a message queue. Before I send the message, I define a variable with the message payload, like this.
def msg=
"""<?xml version="1.0" encoding="UTF-8"?>
<Sel xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/nsmessages">
<Header>
<SomeID xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/com">asd</SomeID>
<SomeOtherID xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/com">fghfg</SomeOtherID>
<Time xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/com">sdgfdgfs</Time>
<Trasdhjkl xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/com">sdggfsdfdsfdssd</Trasdhjkl>
<Grtra xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/com">dfsdffds</Grtra>
</Header>
<Request>
<Sel>
<qwertyuioID xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/Sel">
<Rgttyuiugg xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/com">12355.678</Rgttyuiugg>
<Itthfdfdsfsd xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/com">
<Nafvdgrdgfsdsfsdfra>dfgsdfd</Nafvdgrdgfsdsfsdfra>
<Nafvdgrdgfsdsfsdfrartyhffgfds>ghfdgfdgdf</Nafvdgrdgfsdsfsdfrartyhffgfds>
</Itthfdfdsfsd>
<SomeType xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/com">dfgsdfsd</SomeType>
</qwertyuioID>
<Rhdtdfbfgdsvdfvfd xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/Sel">1234.11</Rhdtdfbfgdsvdfvfd>
<Aregbfdgdfgdfgrh xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/Sel">1234.11</Aregbfdgdfgdfgrh>
<EretdfTytr xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/Sel">267840528</EretdfTytr>
<YtrewrTyresrsfd xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/Sel">KRP</YtrewrTyresrsfd>
<DrtyyrrtTyertergf xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/Sel">2400/R/2/07</DrtyyrrtTyertergf>
<Tewrwerewfvfddf xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/Sel">
<Tregfdgddsggs>2222.678</Tregfdgddsggs>
</Tewrwerewfvfddf>
<Data xmlss="http://www.qazxsw.com/qwertyuio/ytr/xsd/v1/tresty/Sel">
<Trans>
<Mij>
<Mars>0115.11</Mars>
<Pers>
<Rem>1234.11</Rem>
<Rem>67822.11</Rem>
<Rem>11233.678</Rem>
</Pers>
<Com>
<asdg>67813.11</asdg>
</Com>
<Fddfgsfds>
<Trasdafdsd>
<Trasdafdsdtra>1234.11</Trasdafdsdtra>
<Tarasdas>0040.11</Tarasdas>
<Gtrasad>
<Yhtrgdfdsgsd>8796.1123</Yhtrgdfdsgsd>
<Yhtrgdfdsgsd>12334.656546</Yhtrgdfdsgsd>
</Gtrasad>
<Ftyuoioyioityh prop="1234.11">fdgfdgfd</Ftyuoioyioityh>
<Ptergfdgfgdsgsdgdfbf prop="1234.11">8pt0gla0ywkc7aw1g808</Ptergfdgfgdsgsdgdfbf>
<Transreg>
<TransTher>
<Cytgfhfghfdhfdhbdhrt>0040.11</Cytgfhfghfdhfdhbdhrt>
<Ther prop="1234.11">2d4l6nvkenz9c70ys1dw</Ther>
</TransTher>
</Transreg>
</Trasdafdsd>
</Fddfgsfds>
</Mij>
<Objs>
<Obj>
<ObjMid>
<Sel>
<Tra>3333.11</Tra>
<Kjasd>2222.11</Kjasd>
<Nr>
<ConfStat>1234.11</ConfStat>
<Nr prop="1234.11">12345678911234999</Nr>
</Nr>
<Reg>
<Ther prop="1234.11">12345678911234569999</Ther>
</Reg>
</Sel>
</ObjMid>
</Obj>
</Objs>
</Trans>
</Data>
<Check>false</Check>
</Sel>
</Request>
</Sel>"""
What is the best way to replace the values inside many different tags with different random string values of different lengths between 3 and 225 characters?
I noticed that for example, this works and gives me the result I want:
<Check>${__RandomString(${__Random(3,255)},abcdefghijklmnopqrstuvwxyz)}</Check>
(Even though the JSR223 Sampler colors the code differently just up until the first encountered "}" character)
But I read from another stackoverflow answer that is not recommended because caching of compiled scripts won't be available. So I guess this will impact the script's resource consumption.
If I use the expression above to define a JMeter User Defined variable and use vars.get("varName") to bring the value into the groovy script into a groovy variable, it will get just one random value which will be the same in all referenced places and that's not what I want.
So, what's the best approach? should I write a custom randomString function implementation inside the JSR223 Sampler or import some helping library to achieve this in the groovy code or is there a better/simpler way?
Thanks in advance!
You can use i.e. RandomStringUtils.randomAlphabetic() function like:
def msg = "<Check>" + org.apache.commons.lang3.RandomStringUtils.randomAlphabetic(3, 255) + "</Check>"
See The Groovy Templates Cheat Sheet for JMeter for more details.
Alternatively you can put your payload into a file and use __FileToString() and eval() functions combination to get the payload directly in the Sampler body date like:
${__eval(${__FileToString(payload.txt,,)})}