get a udv from include controller to test plan - jmeter

I need to get a variable created in a test fragment through JSON extractor into a test plan. Thrown with 'MissingPropertyException' in the test plan.
javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: Invoices for class: Script117
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_211]
I have tried to use the jsr223 processor in the test plan to access the variable.
The groovy script in jsr223 is like
if (vars.get(Invoices.size() == 0))
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage("no Invoice present");

I am assuming that you need to check if the invoices is blank then fail.
Please check the below plan if that helps:-
Below is json extractor to fetch invoices.
Below fetched values are used.
Assertion to check if invoices is not blank.
Below is the output. If the invoices is blank then it will fail the request.
Hope this helps.
Update:-
Below is a plan with test fragment. To pass variable use like ${varInvoices}
Update2:-
Json config:-
First test fragment:-
2nd Test Fragment:-
Output:-

Related

Jmeter Response Assertion is failing upon getting from Reg Ex Value

Hello I have a scenario where I need to validate the extracted value from my regex, unfortunately I'm encountering an error upon using Response Assertion. I would like to seek assistance to any one of you. Your response is highly appreciated. Thank you so much in advance
I am able to extract my regex upon running, but I encounter upon using that on my Response Assertion.
Screenshot:
Response Screenshot
Response Assertion Failure
Response Assertion Configuration
Expected Result: I just want to validate "userId = 14534"
If you want to just check the presence of 14534 text in the response - just configure the Response Assertion as follows:
In case you want to check whether 1st row of the user_id column of your SQL query result set is equal to 14534:
In the JDBC Request sampler define a variable name, i.e. user_id
Amend your Response Assertion configuration to Apply to a JMeter Variable called user_id_1:
More information: How to Use JMeter Assertions in Three Easy Steps

Using Extracted JSON Value in Another JMeter Thread

First, let me preface this question that I've only been using JMeter for 36 hours.
I've been able to successfully create a thread that performs a POST (json body) to generate a new record.
{
"id":1257697771,
"displayName":"TERM2",
"functionName":"f_1257697771",
"displayableSourceExpression":"TRUE",
"typeId":200,
"groupId":300,
"clobObjId":1257697772,
"typeCode":5,
..........
}
I need to take the new record's ID (1257697771) value returned in order to perform updates, get by ID, delete, etc. on this record in other threads.
After much reading, I've created a Regular Expression Extractor where:
Apply to: Main Sample Only
Field to Check: Body as Document
Reference Name: newRecord
Regular Expression: "id":(.+?)\,"displayName"
Template: $1$
Match No: 1
Default Value: NONE
At this point, I'm not sure if my Regular Expression is formatted correctly where (.+?) is valid.
Also, I'm confused if I can either just specify the new reference (newRecord) in another thread's HTTP request's Parameters or use a BeanShell Post-Processor, or a Response Assertion, etc....
There a lot of answers for the same function of "Passing". Not being a programmer, I've tried to follow the discussion "how to extract json response data in jmeter using regular expression extractor?", but I'm still not clear.
Any insight is appreciated. Thanks.
JMeter Variables are local to Thread Group, you need to convert your variable to JMeter Property.
Use:
__setProperty() function in the Thread Group where you define your newRecord variable like:
${__setProperty(newRecord,${newRecord},)}
__P() function to access property value like:
${__P(newRecord,)}
See Knit One Pearl Two: How to Use Variables in Different Thread Groups article for more detailed explanation.
Also be aware of the Function Helper Dialog as it looks like JMeter functions syntax was developed by aliens.
To pass a value between threads you need to use the jmeter property function.
In a jsr223 postprocessor using groovy the code to get the value is as follows:
def userProperty = props.get('propertyToGet')
vars.put('userProperty', String.valueOf(userProperty))
You would then access the variable in your thread using:
${userProperty}
Or you can use shorthand directly:
${__P('propertyToGet')}
Variables in jmeter are thread specific.
Thanks everyone. I was able to resolve it with your help!
In the first thread:
set the Reg Expression Extractor Regular Expression = "id":(.+?)\,"displayName"
added a Bean Assertion where Parameters = ${__setProperty(newRecord,${newRecord},)}
In the second thread:
appended the Path url with ${__P(newRecord,)}
Executing the first thread (POST) resulted an new record with a unique ID. (1257698108)
Executing the 2nd thread (GET) shows
GET http://server/.../.../.../.../1257698108
And returns the exact data generated in the first thread.
Thanks everyone for your help!

Manipulating the request body of HTTP thread based on the data extracted from the previous HTTP response

I want to manipulate the request body of HTTP thread based on the data extracted (using 'Regular Expression Extractor') from the previous HTTP response.
Here is the scenario:-
I have extracted the statusFlag and statusId from 'HTTP request 1' as:
Ref name: status
Reg. Exp: "statusFlag":"(\w+)","statusId":"(\w+)"
So, first I want to check that the value of statusFlag is 'New' or not.
If it is New then I have to proceed and feed statusId in next HTTP request or else display statusFlag mismatch.
Need help. Got stuck badly.
I believe Response Assertion is what you're looking for. Add it after the Regular Expression Extractor and configure it as follows:
Apply to: JMeter Variable -> statusFlag (or your reference name)
Pattern Matching Rules: Equals
Add New as a "Pattern to Test"
The assertion will check whether "statusFlag" is "New" and if not - it will fail the sampler and report the expected and the actual value.
Optionally you can add If Controller after the Response Assertion, use ${JMeterThread.last_sample_ok} as a condition and place 2nd request as a child of the If Controller - it will be executed only if "statusFlag" is new.
See How to Use JMeter Assertions in Three Easy Steps guide for more information on conditionally setting pass or fail criteria to requests in your JMeter test.
That's how your Jmeter project should look like.
Regular Expression Extractor stores extracted value in ct variable that can be accessed in If Controller as "${ct}" == "yourvalue" and, if true, can be also sent as a part of Request 2 body using the same ${ct} reference.
Jmeter project structure

Jmeter If controller condition statement

I am trying to built a test plan in jmeter where i want to run test for specific HTTP request based on their names. I used if controller but I dont know what condition to write.
I am writing ${__samplerName()}=="HTTPRequestName" in the condition but it isn't executing.
kindly help me as soon as possible.
You need to surround ${__samplerName()} with quotation marks as follows:
"${__samplerName()}"=="HTTPRequestName"
See How to use JMeter's 'IF' Controller and get Pie. guide for more details on If Controller use cases and clauses.
In case if you need to run samplers basing on some condition, you can use JMeter Properties as follows:
Launch JMeter providing sampler name property like jmeter -Jrunsomesampler=true
Add If Controller with the following condition: ${__P(runsomesampler,)} == true
Add desired HTTP Requests as a children of the IF Controller

How to log an error when a JDBC request returns no data from DB in Jmeter?

I need to know how to log an error when a JDBC request returns no data from DB in Jmeter.
I have got:
JDBC request
Query:
SELECT names
FROM customers
Variable names:
namesCustomers
Then I have got a BeanShell postProcessor with script:
import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("namesCustomer", vars.get("namesCustomer_1"));
And finally a simple controller called "set variables" which calls an external file with the following code:
vars.put("namesCustomer",JMeterUtils.getProperty("namesCustomer"));
The problem is that in another SOAP request I am using the variable namesCustomer_1, and if no data is returned from DB this request fails. I need to log an error when the JDBC returns no data.
If I add to the post processor:
log.error("Error example");
I see the error logged in jmeter.log when this request is ran. I need something like:
if(JMeterUtils.setProperty("namesCustomer", vars.get("namesCustomer_1")).toString().length()==0){
log.error("DB returned no results")
}
Any Ideas on how to log an error when the JDBC request returns no data?
It can be done a little bit easier. If JMeter Variable is not set, it'll be null, not empty string.
So I would suggest amending your code as follows:
String namesCustomer = vars.get("namesCustomer_1");
if (namesCustomer == null) {
log.error("namesCustomer_1 variable is not set");
} else {
props.put("namesCustomer", namesCustomer);
}
props is a shorthand to access current set of JMeter Properties, there is no need to use JMeterUtils.setProperty() method.
See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting in Apache JMeter and a kind of Beanshell cookbook.

Resources