Continue Session from UI to API in JMeter - jmeter

Problem Statement: In my application there is a token which is unable to generate it from the api call directly and other way because the it is implemented with OAUTH. So I took a detour and automate it through API. by using Webdriver sampler. with following script.
WDS.sampleResult.sampleStart()
WDS.browser.get('appurl');
java.lang.Thread.sleep(10000);
WDS.browser.findElement(org.openqa.selenium.By.id("details-button")).click();
WDS.browser.findElement(org.openqa.selenium.By.id("proceed-link")).click();
WDS.browser.findElement(org.openqa.selenium.By.id("i0116")).sendKeys("username"); //enter user name and password in login.microsoft.com
WDS.browser.findElement(org.openqa.selenium.By.id("idSIButton9")).click();
WDS.browser.findElement(org.openqa.selenium.By.id("i0118")).sendKeys("password");
WDS.browser.findElement(org.openqa.selenium.By.id("idSIButton9")).click();
WDS.browser.findElement(org.openqa.selenium.By.id("idSIButton9")).click();
//WDS.sampleResult.sampleEnd()
This works fine and I am able to retrieve the token by using regex extractor.
Using the Token in API Call and getting retry error
JMeter Failure (getting IO error exceeded number of retry =, tried with 20, 50 ,200 and now 500)
Browser:
Suspecting the session id may be I am not transferring or is there anything I am missing (remember I am not terminating the browser session)

If I would be in your shoes, I should go with Dmitri answer. But we had a similar issue recently and we were sending JSessionId and Dead-SessionId in the cookie. We resolved by doing following
Add a Http Cookie manager to your test plan.
In the UI sampler please add the sample code,
WDS.vars.putObject('sessionCookies', WDS.browser.manage().getCookies())
Add a JSR223 PreProcessor to your Http request with following script
vars.getObject('sessionCookies').each { scookie ->
sampler.getCookieManager().add(new org.apache.jmeter.protocol.http.control.Cookie
(scookie.getName(),
scookie.getValue(),
scookie.getDomain(),
scookie.getPath(),
scookie.secure,
System.currentTimeMillis() + 10000))
}
In our case we were not sending the expiry which was giving us
ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getTime() on null object at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.3.jar:3.0.3]
So we replaced cookie.getExpiry().getTime() with System.currentTimeMillis() + 10000

I think you need to copy the cookies from the browser to the HTTP Request sampler
Add HTTP Cookie Manager to your Test Plan
Add the next line to your WebDriver Sampler code:
WDS.vars.putObject('cookies', WDS.browser.manage().getCookies())
Add JSR223 PreProcessor as a child of the HTTP Request sampler where you want to copy the session from the UI and put the following code into "Script" area:
vars.getObject('cookies').each { cookie ->
sampler.getCookieManager().add(new org.apache.jmeter.protocol.http.control.Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), cookie.secure, cookie.getExpiry().getTime()))
}
That's it, the HTTP Request sampler should send the cookies now and you should be authenticated
More information on manipulating cookies with Groovy code: Modifying Cookies in JMeter with Groovy

Related

Not able to execute api call for delete through JMeter

I have a delete API http request in Jmeter which is like "serverName/ApplName/api/deleteDataset/{datasetId}"
In the header i have added the below information:
But strangely this api is failing with the below error:
Response code:400
Response message:Bad Request
Please note when i am trying the same API in postmen it is passing without any error. In fact i did not have to pass any header information apart from the access token.
I have tried removing the extra header information(accept,content-type) too from JMeter but that also did not work out.
If your request works fine in Postman the easiest way of replicating it in JMeter would be just recording it using JMeter's HTTP(S) Test Script Recorder
Start the HTTP(S) Test Script Recorder
Import JMeter's certificate into Postman, this way JMeter will be able to intercept and record HTTPS requests
Configure Postman to use JMeter as the proxy so the request to API would go through JMeter
Run your request in Postman
JMeter will generate HTTP Request Sampler and HTTP Header Manager
Correlate bearer token and replace hard-coded value with the appropriate JMeter Variable
You should be able to run the request successfully now

Want to extract data in JMeter

I am using the HTTP request sampler, In which I have used Post Method.
When I Execute the request, it executes the another HTTP request from another thread.
I want to extract the data from that thread because that HTTP request contains the transaction id and I want to use that transaction id in other threads.
The said transaction id is only showing in the pop up message.
Kindly help me.
Solution for the first problem retrieving data from request URL i.e.transaction id using a postprocesser, you can refer the following thread,
Post processer for extracting a value from the URL in the request method.
Solution for the second problem passing the variable from one thread to another,
You need to add a beanshell assertion and set it a property variable like ${__setProperty(transaction_id, ${transID})}
In the second thread group add a preprocesser and accept the property variable.
String tid= props.get("transaction_id");
vars.put("TID",tid);
Thread1:
Thread2:
JMeter can only extract data from response body, message, code, headers, or URL.
If you send the request which triggers another request and the data you're looking for is not in the 1st request response scope - unfortunately you won't be able to get it.
The only option I can think of is using WebDriver Sampler which provider JMeter integration with Selenium browser automation framework so you would be able to execute the request and get the data from the popup using real browser.
WebDriver Sampler can be installed using JMeter Plugins Manager

How can I resolve a 403 response code in Jmeter?

I am working with Jmeter to do load testing. I created a simple login script using BlazeMeter. Then I imported the file into Jmeter.
Whenever I try to run the script, it fails. The first problem I encounter here is that in my first http request I receive a 403 response code:
I added a cookie manager and an authorization manager to try and solve this but it is not working. I think the problem is with cors in the manager header as 403 code means that it received the petition but the access to it was denied. Do you have any suggestions on how to resolve this? I tried adding post processors but couldn't make them work. Maybe because I am a newbie to jmeter and load testing. Idk.
As per HTTP Status 403 description
The HTTP 403 Forbidden client error status response code indicates that the server understood the request but refuses to authorize it.
so most probably your request is missing some authentication context.
Your recording seems to be incomplete, you seem to be testing a GeneXus-based application and it is using AJAX_SECURITY_TOKEN header as the security parameter.
From your screenshot it seems that you're sending recorded hard-coded value, however you should design your script as follows:
Perform initial request, i.e. open login page
Extract AJAX_SECURITY_TOKEN header value using Regular Expression Extractor
Substitute this e8985.... recorded value with the JMeter Variable from the Regular Expression Extractor
Check out Using Regular Expressions to Extract Tokens and Session IDs to Variables article for more information.

JSR223 sampler jmeter / Passing cookie data

I am trying to simulate a parallel ajax requests using a JSR223 sampler, as mentioned here https://www.blazemeter.com/blog/how-load-test-ajaxxhr-enabled-sites-jmeter
But for my set of requests ,I am getting an error,Invalid API/Auth Key
I assume it is is becuase the authentication cookie is not being passed ,I tried to grab the cookie from the previous sampler using
HTTPSamplerProxy previousSampler = ctx.getPreviousSampler();
CookieManager cookieManager = previousSampler.getCookieManager();
HTTPSampleResult previousResult = (HTTPSampleResult)ctx.getPreviousResult();
log.info("Cookie Count is : "+ cookieManager.getCookieCount());
But I get the error
Cannot invoke method getCookieCount() on null object
,I do have the cookie manager enabled in my test plan.
Any help on what I am doing wrong would be great .
The error you're getting means that there is no HTTP Cookie Manager associated with the sampler. You need to add it to your Test Plan and your code should start working as expected.
Be aware that as of now there is a much easier way to implement AJAX requests without having to do any coding, there is Parallel Controller which can be used to mimic AJAX calls by running its children in parallel. Just add it to your Test Plan and move HTTP Request samplers which represent AJAX calls under it. See How to Use the Parallel Controller in JMeter for more details if needed.

how to use jmeter with ajax request

How to use JMeter with ajax request?
I have a button with is clicked and by using fiddler I can find the session id which is being sent to the server.
What should I do next using the JMeter in order to handle this.
EDIT:
Let's say I have in my hand the header which call the request. The JSESSIONID AJAXREQUEST
and so on where should I put it ?...
I putted it in http header manager name and value.
Jmeter will not execute Javascript embedded in your web page. However AJAX request is also a HTTP request that Jmeter will be able to run as a separate HTTP request and upon getting the response you can have post processors (using XPATH or REGEX) to extract the session id in a Jmeter variable.
Alternatively you can record the Jmeter scripts using Jmeter proxy and then all the HTTP requests will be recorded for you and you will just need to attach a post processor manually.

Resources