I'm having trouble with the If Controller in jMeter.
I have a thread group. Inside the thread group is a loop controller. Each time the loop executes, it runs an HTTP sampler.
I want to trigger the If Controller based on the response of this sampler.
However, the If Controller only seems to trigger if it contains another sampler.
But I don't want it to contain a sampler. The steps to be executed when it triggers are a JSON Path Extractor and a Beanshell PostProcessor. These are used to extract part of the JSON in the response from the existing sampler and then use this value to update a shared hashmap in the Beanshell PostProcessor. But they won't run without a sampler present inside the If Controller. And if I put a sampler in there, of any description - even a dummy one, the response data from the first sampler is lost and I can't get to the JSON response from it any more - and the test is then broken.
Any idea how to get round it?
I've tried adding blank listeners and things like that but they don't cause the If Controller to trigger. The only thing that does is a sampler which then breaks my test sequence.
An If Controller is not allowed as a child of the first sampler.
Yes, the jmeter internals work only with samplers. So to run a post-processor in some scope (your IF controller creates the scope), you need a sampler inside it. All the post-processors/timers/listeners etc are actually attached to the samplers of their scope. It is important thing to know about JMeter.
To achieve your goal, you can do a trick where you attach the Regexp extractor to the main sampler and extract all response body to some variable. Then add a dummy sampler and put that variable as the response body. Now you will have the information passed to your extractors within the scope.
Related
I am trying to clear cookie and cache in the middle of a thread group, so that I could login to the test application with another user. I wrote the following beanshell in jmeter.
import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookir_manager").getObjectValue();
ObjectValue();
clear cache.clear();
I got the below error in my jmeter logs.
Error invoking bsh method:eval
Sourced file: inline evaluation of: "import org.apache.jmeter.protocol.http.control.CookirManager; ..." Token Parsing error; Lexical error at line 3, column 59. Encountered: "\u201c" (8220), after: ""
I tried to use the JSR223 post processor to clear cache using the below code:
sampler.getCacheManager().clear();
sampler.getCookieManager().clear();
But the above piece of code is acting as a global one, and starts clearing the cache and cookie for every sampler for the reminder of the script, which is causing authentication issues.
Please let me know if there's any other method to clear cache and cookie at a particular instance/sampler alone.
Is there any particular .jar to be imported to Jmeter to make this work?
There is a syntax error in your script, it's not possible currently to say where exactly because your code is not full. I don't see any problems with it apart from a typo in the property name which should look like HTTPSampler.cookie_manager
Since JMeter 3.1 you're supposed to use JSR223 Test Elements and Groovy language for scripting
JSR223 PostProcessor obeys JMeter Scoping Rules so if you place it as a child of a particular Sampler - it will be applied to that Sampler only.
If you put it at the same level as several Samplers - it will be applied to all of them.
Wouldn't it be easier to untick "Same user on each iteration" box in the Thread Group?
In client-side script 1st user creates events in a calendar for 2nd user, 2nd user accepts/declines them.
I would like to exclude possible errors during event creation and check if event request was created before 2nd user tries to accept it using xpath built on event request template like:
EventRequest_SubjectN
(N is iteration number of event request in a cycle, so if event request N wasn't created by 1st user, 2nd user action with event N fails).
Between these actions are another actions of both users, by this reason I can't use ${JMeterThread.last_sample_ok}.
Is there any analog of this function, but for necessary/specific action several steps ago, not for last sample?
What is the best way to organize IF-condition is that case?
I think about to add BeanShell PostProcessor after 1st user Event request N with a code like:
var rc = prev.getResponseCode();
if(rc.equals("200")){
vars.put('EventRequest_Subject_${N}', 'EventRequest_Subject_${N}');
}
and use not null condition for EventRequest_Subject_${N} in IF-controller for 2nd user.
But it doesn't work in this way. Where am I wrong?
UPD. Solution:
Tried 3 ways:
if Event_RequestN created - put into variable specific value, then use it in IF-condition before 2nd user action with Event_RequestN;
add action with specific assertion before 2nd user action with Event_RequestN and use ${JMeterThread.last_sample_ok} in IF-condition;
add action with xpath extractor before 2nd user action with Event_RequestN and use ${_isVarDefined(EventRequest_Subject${N}_FOUND)} in IF-condition;
and ended up with 3rd variant as the best on practice.
Don't use Beanshell, since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting
Don't inline JMeter Functions or Variables into scripts, go for code-based equivalents instead
As per JMeter Documentation
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads
so if you want to share a piece of data between 2 threads (virtual users) you need to use props, not vars
It's easier to implement your scenario using Inter-Thread Communication Plugin
I have a task where I need to perform two tasks based on the previous Sampler result.
If Assertion fails
{
send email with the error message.
}else
{
perform the tasks in while controller.
}
I have achieved the first part easily by providing the condition in If controller ${JMeterThread.last_sample_ok} == false
I have problems going to else block. I have tried two IF controllers but with no success.
Case here is need to perform one task only either IF block or Else block. But In my case it goes to both the blocks and perform both the tasks.
Can anyone help how to achieve this?
There is no "Else" block in JMeter, you have only "If" therefore if you need "Else" behavior you need to use 2 IF Controllers with opposite conditions.
But, for 2nd If Controller you won't be able to use this as this ${JMeterThread.last_sample_ok} variable will be overwritten with the result of your SMTP Sampler so if your SMTP Sampler will be successful ${JMeterThread.last_sample_ok} variable will become true therefore second If Controller will be executed as well.
So I would recommend the following approach:
If your Assertion Fails
In Beanshell PreProcessor define a custom variable like:
vars.put("run_while_controller", "false");
Put your While Controller under another If Controller and use ${run_while_controller} as a condition
Alternative option is stopping the test after sending an email, this can be done using i.e. Test Action sampler
Going forward I would suggest using Debug Sampler and View Results Tree Listener to double check all the JMeter Variables values, this is most convenient way to see all JMeter variables and properties values. See How to Debug your Apache JMeter Script article for more information on getting to the bottom of your JMeter test unexpected behavior.
I've got simple test plan:
Jdbs Conection
JDBC request
For Each Loop on jdbc response
|-Regular Expression Extractor (Post processor)
|-Soap Request where I need extracted value from Regular Expression Extractor
|-Response assertion
The problem is when the first loop is executed. SOAP request is filled with name of variable from Regular Expression Extractor not the value. Can anyone tell me how to get around this problem?
[Edit] test plan screen
As you can see on the first image is showing jdbc response data . On first position is value '22;1'. Second one is showing first soap request failed not a values but names of variables... Third one is showing second request filled correctly
http://postimg.org/image/owl5kue4v/
Below is screen when I add Debug Sampler to for each loop. In this situation first SOAP request is filled correctly.
http://postimg.org/image/aad3c0ngt/
My problem is how to get the same behavior like in second situation but without adding Debug Sampler?
BTW, Sorry for the images on an external server, but my rank is too low to add them here
Your test plan should be in the format mentioned in the attached snapshot:
Hope this will help
Regex Extractor is working fine at first. I can get it to pull the correct value - a groupID - from the response in the previous HTTP request. And, I turn around and use the variable in the HTTP request immediately after the Extractor. That works great as well. I can see the value in the response was the same as the one placed in the post for the next request.
Few requests later I try to use the variable again when I need the groupID passed again. But, when I look I see the default value I put in the Extractor instead of the value from the response.
My question is two fold: is this extractor only expected to generate a temporary variable?
And, if so, is there a way to plug this variable into something else for later reuse?
Or, did I just miss something?
Well, I found my mistake. It would have helped if I read the information under the post-processor section before I read about the extractor...
I did not have the extractor as a child of the HTTP request. I had it as the next step.
Once I nested it as a child all was right with the world. The variable stays as it was and is not changing after every step.
Lesson learned - RTFM. Or, at least the online documentation a bit more thoroughly!
Jamie