Execute JMeter If controller only for the first true condition - websocket

I have a Web socket Single Read sampler as a child of While Controller. This sampler loops for a certain (dynamic) number of times based on other input conditions. I have to extract a value from one of these responses and I have designed the test plan looks like this:
Thread Group
\_ .. other components
\_ While controller
\_ Web socket single read sampler
\_ Regular expression extractor to save entire response body
\_ If controller (condition: occurrence of specific text in response body)
\_ Dummy sampler (pass saved response body)
\_ Regular expression extractor to save required value
\_ .. other components
Now, the problem is the condition used in the If controller is not unique. So, the required value gets overwritten to the last occurrence of the condition. Is there a way to save the response body only during the first occurrence of the condition?
Thanks in advance!

Add __isVarDefined() function to the If Controller's condition, this way the Dummy Sampler will be executed only if the variable set by its child Regular Expression Extractor doesn't exist:
If you just want to exit the while loop after the first occurrence of the data you're looking for - add Flow Control Action sampler after the Dummy Sampler and configure it to break the current loop:

Related

JMeter waitfor page to load fully

is it possible to wait for XPath or CSS selector to display in Jmeter?
I am using HTTP request to send API call and I have assertion as CSS selector path but due to API slowness , I would like to implement a waitfor method for the specific element in UI.
JMeter doesn't execute any Post-Processors or Assertions or Listeners until it gets response from the server therefore you don't need to do anything. See Execution order user manual chapter for more details.
However if I misunderstood your requirement and you want to repeat a HTTP request until XPath or CSS extractor return the value you're looking for you can put the request under the While Controller and put the condition of your choice there:
In the above case:
On iteration 0 of the While Controller ${myVariable} doesn't have any value (CSS Selector Extractor hasn't been executed yet)
Iterations from 1 to 5 - ${myVariable} has value of foo which doesn't match While Controller's condition so it loops over
Iteration 6 - ${myVariable} value becomes bar and the While Controller exits the loop.
Just in case, textual representation of the jexl3() function used to compare variable with some value:
${__jexl3("${myVariable}" != "bar",)}

JMeter Http request not working as expected in loop controller

When i use a loop controller to go through my results from regex extractor, it stops working when i include a http request inside the loop controller.
Regex extractor format (will output multiple results with multiple groups each)
name of created variable - pageDetails
Template: blank
Match no -1
After that i initialized a loop controller (with a counter) to go through all the results of this regex
The counter is as follows
Starting value =1
Increment =1
Maximum value =${pageDetails_matchNr}
Reference Name =pageDetailsIndex
I have a debug sampler in the loop thats using the counter
${__evalVar(pageDetails_${pageDetailsIndex}_g1)} ${__evalVar(pageDetails_${pageDetailsIndex}_g2)}
Also have http request in the loop thats using the counter
If i disable the httprequest in the loop controller, the debug sampler works, and prints out all the values
However, if i enable the http request, BOTH the debug sampler and http request only work in the first iteration works i.e. when ${pageDetailsIndex} = 1. When its above 1 then ${__evalVar(pageDetails_${pageDetailsIndex}_g1)} etc. all return blanks...
Most probably your Regular Expression Extractor scope is not correct, if you want to apply it to one Sampler only - you need to make it a child of that particular sampler
From your explanation it seems that the HTTP Request sampler which is under the Loop Controller is overwriting the previous values which should not be the case.
Also be aware that your Counter is not necessary, there is ${__jm__Loop Controller__idx} special JMeter Variable which holds the value of the current iteration of the Loop Controller.

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

How to Handle Dynamic Requests in JMeter - Which may/may not occur with each run

In Web Application :
There is Single Page having Different Sections/Partitions
Each section Retrieves the Data with the Help of Filter Query.
If there is no matching Result, Section will Send below Request only :
Request 1: domain/search/jobs/csuser__search2_1413357426.1559
If the Query returns any matching Result, Section will Send below Two Requests:
Request 1: domain/search/jobs/csuser__search2_1413357426.1559
Request 2: domain/search/jobs/csuser__search2_1413357426.1559/results_preview
How can I manage Request 2, which may or may not occur with each run.
Currently I am manually Recording entire Network calls, Removing unnecessary ones & running it for 'N' Loop Count. How can I make sure while test is runing if any section has matching results Request 2 Should also be taken care which I might not have benn recorded on my first execution.
You can use combination of Beanshell PostProcessor and If Controller to work it around as follows:
Add a Beanshell PostProcessor as a child of the Query request
Put the following code into the PostProcessor's "Script" area:
int length = prev.getSubResults().length;
if (length > 1) {
String path = prev.getSubResults()[length - 1].getURL().getPath();
if (path.contains("results_preview")) {
vars.put("resultPresent", "true");
}
} else {
vars.put("resultPresent", "false");
}
Add an If Controller after the request
Depending on your scenario put on of the following conditions to If Controller's "Condition" input
${resultPresent}==true - children will be executed if the query returns results
${resultPresent}==false - children will be executed if the query doesn't return anything matching
Beanshell code does the following:
check how many requests were executed
if there were more than 1 requests, path of the last request is extracted
if path contains "results_preview" variable resultPresent is being set with the value of "true"
if there was only 1 request then resultPresent variable is false
References:
How to use BeanShell: JMeter's favorite built-in component
JMeter “if controller” with parameters
SampleResult class JavaDoc
As Dimitri said. The processor should run after (post) request 1. The if controller will then act on the results of the processor. Section two is only run when the condition is not "false".

Modify a Property on JMeter

I have created a Test Plan that has two thread groups. Each thread group has a SOAP/XML-RPC Request Sampler. Thread Group A also has a Regular Expression Extractor that contains:
Reference Name : ABC
Regular Expression :<response>([A-Z 0-9]+)</response>
Template: $1$
Moreover, Thread Group A has a BeanShellAssertion with
Name: Extract value
Script: ${__setProperty(ABC, ${ABC})};
What I want to do is modify the ABC variable and then pass it on the SOAP Sampler of the second Thread Group.
So, if ABC equals 1000 (response tag holds an int) I want to get that value divided it by two and then pass it on the second sampler like :
<abcValue>${__P(modifiedABC)}</abcValue>
Any ideas?
EDIT:
I have tried preProcessors(on the second thread group) and postProccessors (on the first thread group) but whatever I tried gave me back errors like:
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``String value = Integer.toString(Integer.parseInt(72295) /2); props.put("modifiedABC", v . . . '' : Typed variable declaration : Error in method invocation: Static method parseInt( int ) not found in class'java.lang.Integer'
If your response contains anything other than A-Z, ' ', or 0-9, the regex extractor will fail. It may be better to set the match group to (.+), so it collects whatever is in the response and use a separate regex assertion to check the contents are what you expect. That way you will get a sample fail when the results are bad, rather than a subsequent fail, when your next sample is badly formed through bad input.
In the Thread Group A assertion, you need some "s..
${__setProperty("ABC", "${ABC}")}
This sets a property called ABC to the value of variable called ABC, which is what I think you intend.
Easy way to divide your value is with __javaScript() function..
${__javaScript(${ABC}/2)}
You can use this anywhere in jmeter and it will substitute the value you require. Make sure you have retrieved the property at the start of Thread Group B, as the variable (ABC) is in different scope.
Thread Group 1
Please use 'Beanshell Post Processor' for your request. Add this post processor under the sampler where you extract ABC.
Below link will you give you an idea.
http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PostProcessor
Note that by default everything is String in Jmeter. So you might want to convert it to an Integer before dividing it by 2. You have to add something like this in the Beanshell Post processor.
modifiedABC = Integer.toString(Integer.parseInt(props.get("ABC"))/2);
props.put("modifiedABC",modifiedABC);
Thread Group 2
Now you want to access the modifiedABC in the second thread group.
Simply access it using
${__P(modifiedABC)}

Resources