JMeter jp#gc Parametrized controller - construct object from variables - jmeter

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>

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.

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);

Related

JMeter class javax.faces.el.EvaluationException<

Can some one help me fix this issue in JMeter?
I already use my Regular Expression Extractor after editing javax.face
<?xml version='1.0' encoding='UTF-8'?>
<partial-response><error><error-name>class javax.faces.el.EvaluationException</error-name><error-message><![CDATA[See your server log [enter image description here][1]for more information]]></error-message></error></partial-response>
Most probably your regular expression extractor fails somewhere somehow, double check that it is returning the expected value using Debug Sampler and View Results Tree listener combination.
Also using regular expressions might not be the best idea, if you're extracting Viewstate from the HTML response - it might be a better idea to consider using CSS Selector Extractor instead

Jmeter code to read response xml attributes in beanshell post processor without using Xpath Extractor?

Below mentioned XML is a sample xml, like this there are 'N' number of nodes available and I want to read XML attribute in Beanshell post processor. I get this XML from the HTTP request. How to read that XML (HTTP Response) in Jmeter beanshell post processor like we read in java?
<Order OrderNo="Y1101001">
<OrderLines>
<OrderLine LineNo="1" LineKey="20171100011">
<OrderLine LineNo="2" LineKey="20171100012">
</OrderLines>
</Order>
Beanshell is more or less Java compatible, therefore you can use plain Java code to parse XML.
However I would recommend considering JSR223 PostProcessor and Groovy language instead, Groovy is better from performance point of view, moreover it has built-in XML support so you will be able to get the count of your OrderLine elements as simple as:
def response = new XmlParser().parseText(prev.getResponseDataAsString())
def size = response.OrderLines.OrderLine.size()
log.info("Size: " + size)
Demo:
References:
Groovy - Processing XML
Groovy Is the New Black
Here you can find beanshell/java code to extract values from xml:
Looping through data in JMeter and storing data to use in other sampler

Parameterize tag value inside XML contained in the parameter at runtime in JMeter

I saved the whole XML in a DB table and am fetching the XML in JDBC sampler and using it in HTTP sampler. I want to parameterize a value inside a particular tag value inside this fetched xml at runtime. Could someone tell me how to do that. Thanks in advance
In http sampler ==> post body
xmldoc = ${xmlfromdb}
Here am able to fetch the whole XML and I can submit it successfully. How to parameterize a tag value inside this fetched xml at runtime.
You can do it via scripting like:
Add Beanshell PreProcessor as a child of the HTTP Request sampler
Put the following code into the PreProcessor's "Script" area
String body = sampler.getArguments().getArgument(0).getValue();
body = body.replace("Original Tag Value", "New Tag Value");
sampler.getArguments().removeArgument(0);
sampler.addNonEncodedArgument("", body, "");
sampler is a pre-defined variable available for Beanshell (and some other Test Elements) which stands for parent Sampler instance and in case of HTTP Request it's HTTPSamplerProxy. See JavaDoc for more information on available methods and fields and How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using JMeter and Java API from Beanshell test elements inside JMeter script.

How to extract a response value when doing http post using Jmeter

Can anyone please help me here? I'm trying to do an http post in Jmeter, http works fine, but I'd like to extract the LastName value from the response to use in next http request. I've tried several methods using Xpath Extractor but the Debug sampler shows nothing. I've added XPath_Extractor as a child of HTTP Sampler.
what am I doing wrong here?
Here is what I setup in the XPath Extractor
Reference Name = lstname (which is the variable I carry to next http request)
XPath Query = //*[local-name()='LastName']/text()
also tried
/Reply/CustomerData/#LastName
Main Sample checked
Use NameSpaces- checked
Ingnore whitepspaces checked
Here is my http response
<?xml version="1.0" encoding="UTF-8"?>
<dm:reply xmlns:dm="http://www.xx.com/dm" version="1.0">
<Session>
<TimeDate CurrentDateTime="2015-12-16T08:57:21" CurrentMilliseconds="2881062362"/>
<Reply type="Connection">
<ErrorMessage/>
<ErrorCode>0</ErrorCode>
</Reply>
<TimeDate CurrentDateTime="2015-12-16T08:57:21" CurrentMilliseconds="2881062504"/>
<Reply type="Execute">
<CustomerData FirstName="" LastName="Moni" Address="SD" Chassis="AWD" CountryOfBirth="" CountryOfOriginFullName= Year="2010">
<RecordSet>
</RecordSet>
<ErrorMessage/>
<ErrorCode>0</ErrorCode>
</CustomerData>
</Reply>
<TimeDate CurrentDateTime="2015-12-16T08:57:21" CurrentMilliseconds="2881062590"/>
</Session>
</dm:reply>
You can use regular expression extractor - Post Processor to achieve this.
You need to fill in following parameters
Reference Name: LastName
The regular expression (would look like): LastName="(.*?)"
Template: $1$
Match : 1
Default Value: NotFound
Use ${LastName} in next request to access extracted value of LastName.
Add debug sampler to check if you are extracting correct value.
Why do you need all this namespaces stuff?
Don't check any boxes in the extractor
Use //CustomerData/#LastName as XPath expression
That's it
By the way, you can evaluate XPath Expressions directly against response using XPath Tester mode of the View Results Tree listener. See How to debug your Apache JMeter script article for more tips on getting to the bottom of your JMeter test issue.

Xpath extractor

Can someone help me with this:
I am using xpath extractor of jmeter to retrieve sessionId value from the below response.
I need the value of the sessionId to be stored in some file, so that i can use that value in succeeding calls..My basic response is as below:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:createUserResponse xmlns:ns2="http://www.musicthp.com"
isNewUser="false"
profileId="32109"
sessionId="ryIlb+E5yj7FReA2w96uag=="
success="true">
<duration>316</duration>
</ns2:createUserResponse>`
In order to use the results in a future call, you simply need to use the "Reference Name" as a variable.
If you configure the Xpath Extractor reference name as sessionID, subsequent calls would use ${sessionID}

Resources