Jmeter-How to sent the variable value to the response of particular Request - http

I am working in Jmeter.
I need to sent the value for the variable name 'Reference' in the response of the particular request.
I am able to get the response using Bean shell Post processor using the string "vars.put("response", new String(data));"
I need the get the variable 'Reference' which is referred by id as id="reference"
I need to pass value to the variable 'Reference'.
Can anyone help?

Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for any form of scripting so if you want to store the response into the Reference JMeter Variable you can do it as follows:
Add JSR223 PostProcessor as a child of the request which response you want to store
Put the following code into "Script" area:
vars.put("Reference", prev.getResponseDataAsString());
where:
vars is a shorthand for JMeterVariables class instance
prev stands for the parent SampleResult
see JavaDoc for the above shorthands for all available functions/properties description and Top 8 JMeter Java Classes You Should Be Using with Groovy for comprehensive explanation with examples for the above and other JMeter API shortcuts available for Groovy scripts

Related

How to make async http request from jmeter while changing path dynamically before each call

Following are the steps that I need to peform
Make http request call to a sevice which returns a json that has many urls.
Extract all the urls using regular expression extractor
Make http request call to all the exctracted urls asynchronously.
Is there a way we can achieve this? I tried parallel controller but, if I am not wrong, it requires all the request to be mentioned as its child sampler. I don't want to write each and every request manually. Is there a way we can change urls dynamically after running the test plan?
It's better to use JSON Extractor if the server returns URLs in JSON format
Once you have the URLs in form of JMeter Variables like:
url_1=http://example.com
url_2=http://example.org
........
........
url_matchNr=X
add Parallel Sampler to your Test Plan
add JSR223 PreProcessor as a child of the Parallel Sampler
Put the following code into "Script" area:
1.upto(vars.get('url_matchNr') as int, { index ->
sampler.addURL(vars.get('url_' + index))
})

How to Write Response Assertion Using Groovy in JMeter

I want to write a code for Response assertion using groovy for one of the Request Giving Response data like this
{
"value":"200"
"value_description":"pass"
"value_code":"pass"
"data_encode":"uyt-09-0nbv"
}
after google Search i am getting only with Response Assertion SOAP-UI tools and i also checked with Blaze meter blog i am not understating about what they are saying. simple way i want demonstrate that.write code for Response Data Assert value for 200 is this possible. please help me to this stuff
The relevant Groovy code to check whether value attribute in the response equals 200 would be something like:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def value = json.value
if (value != '200') {
AssertionResult.setFailure(true)
AssertionResult.setFauilreMessage('Expected 200, but got ' + value)
}
Add JSR223 Assertion as a child of the request which returns the aforementioned JSON (it is not valid JSON by the way)
Put the above code into "Script" area (make sure you tick Cache compiled script if available box and choose groovy from the "Language" dropdown)
More information:
Groovy: Parsing and producing JSON
Scripting JMeter Assertions in Groovy - A Tutorial
You can add 4 JSON Extractors, each with different Path Expressions:
$.value, $.value_description, $.value_code, $.data_encode
It will assert that JSON parameter returned.
You can add Regular Expression Extractors to check each variable you got using Apply to JMeter Variable.

Request Variable on each run

I'm using paw to test our API as well as run customer support for various problems that occur. Right now we're using request variables to manage the parts of the API call both URL or Body that are variable. However, its common for us to forget to update a value. Is there a type of request variable we can use that would require a new value for each run? Something like a pop up with a dynamic form for the values required by the request?
I using Paw too and need to send every request with new value. I'm using request variable and fill it with JS Script that contains return +new Date();
In that way If i sending requests not more than once per microsecond it contains new value.

JMeter "forgets" variable value defined via Regular Expressioin Extractor

I did create a simple testcase in JMeter.
Open a form and all it's content (css, images etc) :
GET /
GET /css/site.css
GET /favicon.ico
GET /fonts/specific-fonts.woff
GET /images/banner.png
Wait a little...
Post the values
POST /
Receive the "Thank You" page.
- GET /thanks
In the response on the first GET is a hidden input field which contains a token. This token needs to be included in the POST as well.
Now I use the "Regular Expression Extractor" of JMeter to get the token from the response. So far, so good.
Then, after retreiving all the other contents I create the POST message, using the variable name in the RegExp-Extractor in the value field of the token parameter.
But... when executing the testcase it fills in the default value given and not the actual value of the token.
So... first step in debugging this issue was to add a dummy-HTTP-GET request directly after I get the token. In this GET request I also add the token parameter with the token variable as value, but now I can easily check the parameter by looking at the access-log on my webserver.
In this case... the URL looks promising. It contains the actual token value in the GET, but it still uses the default value in the POST.
Second step in debugging was to use the "Debug Sampler" and the "View Results Tree".
By moving the Debug Sampler between the different steps I found out the value of the token-variable is back to the default value after I receive the CSS.
So... now the big question is...
How can I make JMeter to remember my variable value until the end of my test-script ?
JMeter doesn't "forget" variables. However variables scope is limited to the current Thread Group. You can convert JMeter variable to JMeter Property which have "global" scope by i.e. using Beanshell Post Processor with the following code:
props.put("myVar", vars.get("myVar"));
Or by using __setProperty() function. See How to Use Variables in Different Thread Groups guide for details.
As you found it your problem comes from a misunderstanding of scoping rules in jmeter.
https://jmeter.apache.org/usermanual/test_plan.html#scoping_rules
In your case, just put the post processor of the request that will give you the response containing the child node.
Also I think you don't need to share this token with other threads so don't use properties as proposed in the alternate answer.

Http call parameters SoapUI

How can I Parameterize an http call parameter in soapui to read parameters from a txt file for each iteration.
If needed can the parameters be encoded(url or gzip) before the call was sent?
Any help (pointers/links/code) is greatly appreciated? Thank You
Use groovy script test step to read data from txt file and store the data in TestCase property .
Something like this would work:
String fileContents = new File('/path/to/file').text;
testRunner.testCase.setPropertyValue(property_name, fileContents);
More information about groovy script steps here.
You can access this property as ${#TestCase#property_name} in your requests. Then you can use template parameters for your request url - I've already answered about it here.
If i'm not wrong you are asking about parametrization of URL which you send as HTTP Request for your Rest call. Let me explain you with an example :
Suppose you are looking for a resource and invoking the WebService using the GET method by making use of the ResourceID already present in the DB...Parametrize it as below :
http://${#Project#HOST}:${#Project#PORT}/rest/${#Project#WebApplicationName}/Resource/${#TestCase#ResourceID}
where HOST, PORT, WebApplicationName are the Project Level properties and ResourceID is a Test Case Level property(as it may change with the test cases i.e., dynamic in nature).
This is my approach of parametrization instead of taking it from a local file. Hope this helps!

Resources