I am trying to fetch the parent url of the current failing request (for eg consider a travel website ,parent url will be search creteria and current url is selecting flights page).How can we do it .I tried it with putting a if controller after failing url and a beanshell so that it fetches the parent url of all the failings flights request. But what is happening is ,if the current flight request is falining it wont go to the next if controller at all. Execution stops. Can someone guide me with the better way?
First of all forget about Beanshell, you should be using JSR223 Test Elements and Groovy language for scripting starting at least from JMeter version 3.1 (for earlier versions it was also recommended, however Groovy engine wasn't included in JMeter distribution)
Add JSR223 PostProcessor as a child of the 2nd request
Put the following code into "Script" area:
if (!prev.isSuccessful()) {
log.info('Previous sampler name: ' + ctx.getPreviousSampler().getName())
log.info('Previous sampler URL: ' + ctx.getPreviousSampler().getUrl().toString())
}
That's it, you should see the name of the previous sampler and its URL in the Log Viewer panel and in jmeter.log file
You can save the URL into a JMeter Variable like:
vars.put('url', ctx.getPreviousSampler().getUrl().toString())
once done you will be able to access the value as ${url} where required.
References:
prev stands for current SampleResult
ctx is a shorthand for JMeterContext
vars is a shorthand for JMeterVariables
See JavaDoc for the above classes for description of used functions and Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter.
Related
I am trying to let JMeter crowl my website to ensure a realistic stress test. I was able to extract the URLs from the home page and iterate on them. So I have a regular expression feeding a ForEach loop.
Now I am not able to let an HTTP Request take the output of the loop (Defined as a variable with a name) as its path.
Is there a general approach to setting the path of such a request. JMeter is taking something like:
${MyVar}
set in the path of the request as a string and is not replacing it with the actual value.
Given your Regular Expression Extractor and ForEach Controller configurations are correct everything should work fine. If you need any assistance with this provide the following screenshots:
Regular Expression Extractor configuration
Debug PostProcessor or Debug Sampler output in the View Results Tree listener showing several generated JMeter Variables
ForEach Controller configuration
HTTP Request sampler configuration (i.e. where do you put the variable)
Be aware that you can mimic crawling the site more easily using HTML Link Parser the relevant configuration would be as simple as
See How to Spider a Site with JMeter - A Tutorial to learn more about simulating websites crawling.
I have test case in my csv file. The request URL has a custom variable.
Sample URL : .../abc/$id
I need to replace this id by the id that we get in response from the previous request. I used json extractor to fetch the id from the response. Now I need to update this id in the next test case request. Fetched the Request URL from jmeter context using below code:
String path = ctx.getCurrentSampler().toString();
path.replaceAll("$id", id);
I am not able to set this updated URL in jmeter context (ctx)
You need to assign new path value to path variable
You need to set sampler path to the new value using sampler.setPath() method
So you need to amend your code like:
String path = ctx.getCurrentSampler().toString();
path = path.replaceAll("$id", id);
sampler.setPath(path);
Demo:
Also consider switching to JSR223 PreProcessor and Groovy language as Groovy performance is much higher, it better supports new Java features and provides some extra "syntax sugar" on top. See Groovy is the New Black article for details.
Try to avoid the pre / post processors if possible.
Your requirement is very simple and straight forward.
Directly use this in the path - assuming id is the name of variable which has the value.
/abc/${id}
I am trying to use beanshell post processor so I have started with simple http request where I am hitting the google home page. "${url}"=="https://www.google.co.in". After http request page I am adding the BeanShellPostProcessor where in Script area I am using ctx variable and some other code which is like :
int threadNum = ctx.getThreadNum();
String code = prev.getResponseCode();
String message = prev.getResponseMessage();
log.info(threadNum);
log.info("This line has been written by Beanshell Post Processor");
so I have two concerns -
i.The way I am using beanshell is this right ?
ii.Where is the console for beanshell processor in jmeter?
like sample request result can be viewed in listener. I tried with BeanShellListner, but it doesn't show any data.
Also I have kept the "log viewer" on.
You are using it almost right, the only error is that you cannot write an integer to the log file, you need to cast it to String first like:
log.info(String.valueOf(threadNum));
You won't be able to see the result of pre and post processors anywhere apart from jmeter.log file. In case of PostProcessor you can modify parent sampler response data via prev.setResponseData() method where prev is a shorthand to SampleResult class instance
I would also recommend considering switching to JSR223 Elements and Groovy language as this way you will get better performance, out of box support of XML, JSON, etc., and other Groovy's "syntax sugar". See Groovy Is the New Black article for more detailed explanation.
I saved the whole XML in a DB table and am fetching the XML in JDBC sampler and using it in HTTP sampler. I want to parameterize a value inside a particular tag value inside this fetched xml at runtime. Could someone tell me how to do that. Thanks in advance
In http sampler ==> post body
xmldoc = ${xmlfromdb}
Here am able to fetch the whole XML and I can submit it successfully. How to parameterize a tag value inside this fetched xml at runtime.
You can do it via scripting like:
Add Beanshell PreProcessor as a child of the HTTP Request sampler
Put the following code into the PreProcessor's "Script" area
String body = sampler.getArguments().getArgument(0).getValue();
body = body.replace("Original Tag Value", "New Tag Value");
sampler.getArguments().removeArgument(0);
sampler.addNonEncodedArgument("", body, "");
sampler is a pre-defined variable available for Beanshell (and some other Test Elements) which stands for parent Sampler instance and in case of HTTP Request it's HTTPSamplerProxy. See JavaDoc for more information on available methods and fields and How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using JMeter and Java API from Beanshell test elements inside JMeter script.
In JMeter, how do I save a thread variable in Javascript and reference it from Beanshell on the very next step OR the next threadgroup?
I am using the WebDriver sampler plugin and the WebDriver steps seem to be required to be Javascript commands. I wrote a WebDriver code block in Javascript that goes to a website and gets a cookie value.
How do I set the thread-group variable at the end of the Javascript WebDriver step.
How do I retrieve this value from a Beanshell step.
I have tried various things and haven't got anything to work. For example vars.put works in the WebDriver sampler but props.put says 'props' is not defined. The debug sampler shows that my JMeter property was set wonky like this:
"varName"= varName
After setting it in Javascript like this:
props.put( 'varName', varName )
And that doesn't look right AT ALL.
Tricky thing is that vars isn't accessible to WebDriver Sampler as well, you'll need to be more creative. First of all you need to return value as Sampler result as follows:
WDS.sampleResult.setResponseData("My var value is:" + varName);
After all you need to extract this from response with i.e. Regular Expression Extractor using regular expression like:
My var value is: (.+?)
and varName as Reference Name
And finally add Beanshell Post Processor to your WebDriver Sampler with the code like:
props.put("varName", vars.get("varName"));
to convert JMeter Variable to JMeter Property which has "global" scope and can be re-used in other Thread Groups.
The solution I ultimately found was not the same. Using 'WDS.sampleResult.setResponseData' does not work since the WebDriver Sampler code seems to not allow this.
Turns out that I was able to find a workaround using "headers". Using a Beanshell Sampler step right after the WebDriver Sampler, I am able to modify the headers of the previous result (even though I cannot modify the response data itself) and so from the WebDriver Sampler I am able to store the header using 'WDS.sampleResult.setResponseHeaders( "iCookie:" + iCookie )' . Then, from a subsequent Beanshell Sampler (not a post-processor) I am able to get to that value using 'String header = ctx.getPreviousResult().getResponseHeaders();' as long as I stored the header in this format: "HeaderName:HeaderValue" from WebDriver Sampler.
It is really annoying that I cannot do a similar thing using the .setResponseData method. But, I am happy there is a workaround of some kind since I cannot find any documentation anywhere on what the normal process is for this kind of thing.