Jmeter and webhook - jmeter

Is it possible to have Jmeter return the final results of a set of HTTP request responses to a webhook URL?
For example: I have a mini Journey created with several HTTP request / responses. The final HTTP request response body is retrieved using getResponsesData and using PostProcessor Groovy, I determine whether test passed or failed. I like to integrate that with webhook url how can I do that?

JMeter is built around Apache HttpComponents therefore you can invoke a HTTP Request directly from Groovy code using Apache HttpClient, something like:
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
def httpClient = HttpClients.createDefault()
def httpGet = new HttpGet("http://example.com")
def response = httpClient.execute(httpGet)
//do what you need with the response, i.e. print it to jmeter.log
log.info(EntityUtils.toString(response.getEntity()))
response.close()
httpClient.close()
Check out Sending HTTP and HTTPS Requests Using Groovy in JMeter for comprehensive information and advanced scenarios examples

Related

Continue Session from UI to API in 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

Graphql with Jmeter

I am trying to use jmeter to Automate graphql API testing. I have done the manual testing with postman. However, since some one suggested i am trying to automate with jmeter. unfortunately i am new to API testing itself and having great difficulties.
As i understand Graphql method only deals with POST, but in my Jmeter it is throwing an error, but when i change it to GET, i get 200 ok response.
Also in the body data i am directly entered the graphQL query is this correct.
In the return response data i don get any information related to my query.
I have added header manager with
Content-type = "Application/json"
Accept-encoding
Authorization
Accept
Connection
My graphql query is
query{cmCases(filters: {caseId:"case-6"})
{
items {
id
}
}
}
In the response i am just receiving meta data.
Looking into HTTP Methods, Headers, and Body chapter of the GraphQL documentation GraphQL supports both GET and POST methods so you can choose whatever you want or whatever you need
Looking into HTTP Headers documentation chapter header values are case-sensitive so you need to change Application/json to application/json in the HTTP Header Manager
Given you can successfully execute request in Postman you should be able to record this request by JMeter's HTTP(S) Test Script Recorder, just configure Postman to use JMeter as the proxy
Starting with upcoming JMeter 5.3.1 or 5.4 you’ll be able to use the GraphQL Http Request.
Until it is released you can try a nightly build:
https://ci.apache.org/projects/jmeter/nightlies/
See:
https://github.com/apache/jmeter/pull/627

Is it possible to send a POST request to external API when receiving one?

I'm receiving a JSON POST request from my frontend and store it in my database - this is standard behavior of my backend.
Is it possible to send the same data (or some parts of it, after validation) with another POST request to an external API that is not managed by me? If so, I think it would be by extending create method - am I correct? How could I trigger sending a request to that 3rd Party API on receiving it in my backend?
Do you know any examples?
Yes, You can do that using pip install requests. It means your api is working as a proxy.
Example
from rest_framework import Response
import requests
def api_view(request):
external_api_url = 'https://example.com/api/endpoint/'
data = request.POST
res = requests.post(external_api_url, data)
return Response(res.json())
You can create (override) your custom "create" view method, adding the call to the external API (if the external API is REST, "request" is a good tool).
Establish a new HTTP conections (external) can penalize the endpoint response speed, and if the external service is too slow, can produce timeout so I suggest do the external api call using async mechanism, (celery task worker or async functions) and ensure with a retrive loop and timeout.
Using the requests library, You can POST data to an external API
For Example
import requests
request_type = "POST"
data = {"email":"email", "name": "name"}
api_url = "https://external-api.com/api/post/"
response = requests.request(request_type, api_url, data=data)

In a Jmeter JSR223 sampler how do you access the values set in the HTTP Request Defaults configuration object?

I am using groovy as the language and want to read the server IP value is that was set in the HTTP Request Defaults configuration item.
I looked at http://jmeter.apache.org/usermanual/functions.html and a few other places but nothing useful I can find.
Thanks,
Dan
Actually there is no such thing as HTTP Request Defaults in JMeter script, it is only abstraction layer made for your convenience. When the test is being run the values are merged into HTTP Request samplers where the relevant field(s) is(are) blank. So instead of getting the server IP value from HTTP Request Defaults you should be getting it from the HTTP Request samplers.
I would recommend using JSR223 PostProcessor, add it as a child of the HTTP Request sampler and use the following code:
def serverIP = sampler.getDomain()
It is also possible to do using JSR223 Sampler for the previous sampler (which is supra in the Test Plan) like:
def serverIP = ctx.getPreviousSampler().getDomain()
See Why It's SO Important To Use JMeter's HTTP Request Defaults to learn more about HTTP Request Defaults specifics.

Handling Callback URLs in JMeter

My REST Service takes in a JMeter HTTP request and one of the parameters in the request is a callback url.
The REST Service uses this callback url to post back a response.
Is there any JMeter Listener I can use to receive the callback i.e. so that the REST Service can send the response back to a JMeter Listener.
And if possible the Listener can then send in another HTTP Request based on the response.
I'm not sure about your question. But I think you want to receive your REST response and make another request to URL in the response...
By that way, you need to "catch" the response by using Regular Expression Extractor, store URL in the response to a JMeter Variable. Then, you create HTTP Request, and put URL in that JMeter Variable into request.

Resources