1

enter image description here

Here in My HTTP Request I extracted some variables using JSON Path Extractor. I want to create a Java Request it will validate the above variables. i.e I want to check the value of reqVar1 is equal to resVar1 or not and reqVar2 is equal to resVar2 or not like that.

uı6ʎɹnɯ ꞁəıuɐp
  • 3,431
  • 3
  • 40
  • 49
KCS
  • 442
  • 5
  • 20

2 Answers2

0

You are making things over complicated, you could achieve the same using normal Response Assertion.

For example, if you have a JMeter Variable ${var1} and you need to compare it to ${var2}, ${var3} and ${var4} you can just configure the Response Assertion like:

Response Assertion Example configuration

More information on conditionally marking sampler results as successful or failed: How to Use JMeter Assertions in Three Easy Steps

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • If we use the above answer, it is same as adding 25 Response Assertions as he has 25 variables to compare. Response Assertions are not the answer either. – Naveen Kumar R B Nov 15 '16 at 10:28
  • Dmitri, @Naveen is right, by going through your answer I need to write 25 Response Assertions. But I need to write a Request or any other available approach to make this process easy so that in future if I need to check more variables it will become easy to me. – KCS Nov 15 '16 at 11:16
-1

Instead of Java Request sampler, you can add BeanShell Assertion to compare the values. following is the code:

if(vars.get("reqVar1").equals(vars.get("resVar1")))
{
    if(vars.get("reqVar2").equals(vars.get("resVar2")))
    {
        SampleResult.setResponseCode("200");
        SampleResult.setResponseMessage("SUCESS");
    }
    else{
        SampleResult.setResponseCode("403"); // keep error code as per your wish
        SampleResult.setResponseMessage("reqVar2 and reqVar2 are NOT same" + vars.get("reqVar2") + vars.get("resVar2"));
    }
}
else{
    SampleResult.setResponseCode("403"); // keep error code as per your wish
    SampleResult.setResponseMessage("reqVar1 and reqVar1 are NOT same" + vars.get("reqVar1") + vars.get("resVar1"));
}

Add the BeanShell Assertion as a child element to the HTTP Sampler in which you are getting the values for reqVar1, reqVar2, resVar1, resVar2

Once the decision is taken based on If condition, you can change the response code and message using SampleResult

Check the following reference that shows all methods available to you:

  1. https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html

Image reference: enter image description here

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • Thanks for the reply. Initially I thought the same. But for some requests I need to compare 25 variables. At that time it is not an efficient way. So that I thought is there anyway to create a Java Request it will compare the values and return result. – KCS Nov 15 '16 at 08:52
  • can you please share how you are comparing the values using Java Request? Unless you implement/extend Java Request, I don't think so, there is a way to compare the values (variables). please refer following answer on how to build your own Java Request http://stackoverflow.com/questions/23286893/how-to-write-a-jmeter-test-for-java-request – Naveen Kumar R B Nov 15 '16 at 09:08
  • one more reference https://newspaint.wordpress.com/2012/11/28/creating-a-java-sampler-for-jmeter/ – Naveen Kumar R B Nov 15 '16 at 09:13
  • I gone through the provided links but I am unable to create a Java Request for my Requirements. Is there any better way to compare lot of variables. So that I am not going to write a BeanShell Code. – KCS Nov 15 '16 at 11:08