Capturing cookies in JMeter - ajax

I am facing issue for AJAX call in jmeter.
A new Ajax Token generated every time by taking value of cookies
efpcookieiQmdOSdtYvVcktctGnNfrwCC4350121699716256984xxxxxxxxx=nAgvF4MhWoTTb7NTV8zaowCC;
I need to pass the cookie in the following URL:
http://www.example.com/?usecase=maintain_account&command=load_acc_type_command&guid=${uid6}&commandorigin=0.create_account_step1_view&fpid=efpcookieiQmdOSdtYvVcktctGnNfrwCC4350121699716256984xxxxxxxxx&view=create_account_step1_view&pipe=ajax&hash=hash1287912XXXX&ajaxtoken=${ajaxtokan}&accProdId=1
How can I capture this?

Edit the jmeter.properties file inside the JMeter bin directory, and set CookieManager.save.cookies=true
Restart JMeter and add a HTTP Cookie Manager. Now, just replace the Cookie value with ${COOKIE_fpid}, this is:
usecase=maintain_account&command=load_acc_type_command&guid=${uid6}&commandorigin=0.create_account_step1_view&fpid=${COOKIE_fpid}&view=create_account_step1_view&pipe=ajax&hash=hash1287912XXXX&ajaxtoken=${ajaxtokan}&accProdId=1

Related

adding cookie to the cookiemanager using JSR223 sampler is throwing no such method error

I want to add two cookies for the cookie manager but the JSR223 sampler is throwing an error as Method getCookieManager() not found
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
CookieManager manager = sampler.getCookieManager();
Cookie cookie =
new Cookie("CraftSessionId", "${COOKIE_CraftSessionId}", "nginx/1.20.1", "/", false, 0);
Cookie cookie2 =
new Cookie("CRAFT_CSRF_TOKEN", "${COOKIE_CRAFT_CSRF_TOKEN}", "nginx/1.20.1", "/", false, 0);
manager.add(cookie);
manager.add(cookie2);
log.info("cookieManager::"+manager);
enter image description here
enter image description here
You don't have the Cookie Manager in the JSR223 Sampler, if you really need to add cookies using scripting - add a JSR223 PreProcessor as a child of the HTTP Request sampler and put your "code" there.
Replace this "${COOKIE_CraftSessionId}" with vars.get("COOKIE_CraftSessionId") as by default JMeter will compile and cache only first occurrence of variable, so the first value will be used in all subsequent iterations and it may ruin your test.
Do the same for the COOKIE_CRAFT_CSRF_TOKEN
I don't think you can have / in the domain, so it should be just nginx and the 1.20.1 should go to the path.
You don't need any scripting at all, it's possible to add cookies manually using HTTP Cookie Manager
More information:
JSR223 Sampler Documentatinon
Top 8 JMeter Java Classes You Should Be Using with Groovy

How to user variable form webdriver sampler in HTTP Header Manager

I am able to extract auth_key from session storage .
I want load an API with auth_key in HTTP Header Manager
How can this be done.
var foo = WDS.browser.executeScript("return window.sessionStorage.getItem('ngStorage-jwtToken');")
var obj = JSON.parse(foo);
vars.put("auth",obj.oauth_token)
WDS.log.info(vars.get('auth'))
And I want to use auth as global variable to access on all threads.
Replace this line:
vars.put("auth",obj.oauth_token)
with this one:
WDS.vars.put("auth",obj.oauth_token)
Add HTTP Header Manager as a child of the request which header you need to amend and configure it like:
See General Concepts section of the WebDriver Sampler user manual entry to learn what pre-defined variables are available for the scripting.
Sdd HTTP Header Manager in scope with name auth_key and value ${auth}.
${auth} will be updated with the value from webdriver sampler.

How to set a session id in Postman

I need to hit a post request to an API service which requires a session id along with other parameters in its post request field in order to get the required information.
I am using Postman to test this API.
I would like to know how to send a 'session id' in a post request when using Postman?
I am aware of pre-request script in Postman, but I am unaware of how to use the variable in post request.
In Postman native app:
Turn on the Interceptor.
Go to Headers
Key: Cookie
Value : sessionid=omuxrmt33mnetsfirxi2sdsfh4j1c2kv
This post is bit old but I want to still answer incase someone else is looking for an answer.
First, you need to see if intercepter is enabled in the toolbar, it is present one step away from sign-in
If does not not get enabled when you click on it, you can install extension. I think there is one for Chrome. Go ahead and add the extension.
After that you can go back to Postman and enable intercepter
You will be able to see cookies in postman and at this point you can add _session_id
I hope this will help.
Thanks,
Hit inspect on the site you are working on, when logged in
On your Chrome/browser, go to application - cookies.
Copy your PHPSESSID.
On postman headers Key: Cookie
Value: PHPSESSID=dsdjshvbjvsdh (your key)
For standalone Postman app
You can use global variables in postman. First in the Tests tab of the first request set the session as global variable:
var a = pm.cookies.get('session');
pm.globals.set("session", a);
It might be 'session_id' as well (check in the headers of your first request) instead of session. To check if this variable is set, after you do your first request, go to the gear icon and click on globals.
Then go to your second request -> Headers and for key add 'Cookie' while for value add 'session={{session}}'
Side note: be careful not to save keys that are used by your framework or they might be deleted for some reason.
On your browser:
Open the developer tools (right click and Inspect).
Go to Application Tab > Storage > Cookies.
Open your site Cookie and copy the Name and Value.
In Postman 8+:
In your Request tab, go to Headers.
Click in the eye icon to see the hidden headers.
Click in the "Cookie" link that is in the top right corner.
In the "Manage Cookies" popup, select your domain, click on "+Add" button, or edit the existent cookie.
Paste the values that you copy from the browser. The complete value will look like PHPSESSID=f20nbdor26v9r1k5hdfj6tji0p; Path=/;
Click on "Save", and close the popup.
Select Tests Section below the Request URL
if(postman.getResponseHeader("authorization")!==null)
{
postman.setGlobalVariable("token","Bearer " + postman.getResponseHeader("authorization") );
}
Here u can use SessionId to get SessionId from Header and put in global variable .

Bottle, how to get_cookie during AJAX request (same domain)

I use bottle set/get cookie mecanism to track my user_id (with 'secret' param when calling set/get_cookie()
During normal http(s) request everything is fine but when making a xhr request (same domain)user_id = request.get_cookie('user_id', secret='mysecret') returns None.
When checking on client browser, cookie and key/value are still available.
How to deal with it ?
(I've always been told that xhr requests are http requests, so from same domain, cookies should be shared, no ? is problem arrising from Bottle 'secret' handling ?)
The documentation states, when setting a cookie the default path is "/"
https://bottlepy.org/docs/dev/tutorial.html#tutorial-cookies
path: Limit the cookie to a given path (default: /)
But the code comment says default is "current path" e.g. /XXX/dev
https://github.com/bottlepy/bottle/blob/master/bottle.py#L1796
:param path: limits the cookie to a given path (default: current path)
Based on this behaviour we can assume the later is true.
So when setting the cookie, if you want any path to have access to the cookie from your domain. Set the path in your cookie as "/"
set_cookies('test', 123, secret='mysecret', path='/')
I tried to set_cookies('test', 123, secret='mysecret') under AJAX request, it worked, but still couldn't find previous cookies.
Then I remarked that my previous cookies, called cook1 and cook2, written under 'normal' http request, if they had same domain, had different 'path' (under Chrome ressource explorer). They were set under path '/XXX/dev' and my AJAX request was just under path '/XXX'
So I modified my AJAX request from /XXX/do_stuff to point to '/XXX/dev/do_stuff', and then, surprise ! cook1 and cook2 could be read by my AJAX request.
Not sure if it's a Bottle bug or if such behaviour is designed on purpose (in this case, if someone can explain to me why...), but at least I have my solution.

Disable cache in ExtLib REST control (which uses dojox.data.JsonRestStore)

In my XPage I have a xe:djxDataGrid (dojox.grid.datagrid) which uses xe:restService which seems to use dojox.data.JsonRestStore.
Everything works fine without proxy but my client accesses the application via a proxy because of corporate policy. After a user updates data in the DataGrid it shows old values when accessed behind the proxy.
When the REST Control/JsonRestStore sends an ajax GET request to get data, there is no Cache-Control parameter in request headers. And Domino does not place Expires parameter in the reponse headers. I believe that's why the old version of the GET request gets cached by the proxy.
We have tried to disable cache in browsers but that does not help which indicates the proxy is caching the requests.
I believe this could be solved either by:
Setting Cache-Control parameter in request headers OR
Setting Expires parameter in response headers
But I haven't found a way to set either of these. For the XPage Domino sets Expires:-1 response header but not for the ajax GET request which is:
/mypage.xsp/?$$viewid=!ddrg6o7q1z!&$$axtarget=view:_id1:_id2:callback1:restService1
This returns the JSON data to JsonRestStore and gets cached by the proxy.
One options is to try to get an exception to the proxy so requests to this site would bypass the proxy cache. But exceptions are generally not easy to get thru.
Any ideas? Thanks.
Update1
My colleque suggested that I could intercept the xhr GET requests made by dojox.data.JsonRestStore and add a time parameter to the URL to prevent cache. Here is my question about that:
Prevent cache in every Dojo xhr request on page
Update2
#SvenHasselbach has a great solution for preventing cache for all xhrs:
http://openntf.org/XSnippets.nsf/snippet.xsp?id=cache-prevention-for-dojo-xhr-requests
It seems to work perfectly, &dojo.preventCache= parameter is added to the URLs and the requests seem to return correct JSON also with this parameter. But the DataGrid stops working when I use that code. Every xhr causes this error:
Tried with Firefox and Chrome. The first page of data still loads because xhr interception is not yet in place but the subsequent pages show only "..." in each cell.
The solution is Sven Hasselbach's code in the comment section of Julian Buss's blog which needs to be slightly modified.
I changed xhrPost to xhrGet and did not place the code to dojo.addOnLoad. When placed there it was not effective in the first XHR by the DataGrid/Store.
I also removed the headers modification because it overrides existing headers. When the REST control requests data from server with xhrGet the URL is always the same and rows requested are in HTTP header like this:
Range: items=0-9
This (and other) headers disappear when the original code is used. To just add headers we would have take the existing headers from args and append to them. I didn't see a need for that because it should be enough to add the parameter in the URL. Here is the extremely simple code I'm using:
if( !(dojo._xhrGet )) {
dojo._xhrGet = dojo.xhrGet;
}
dojo.xhrGet = function (args) {
args['preventCache'] = true;
return dojo._xhrGet(args);
}
Now I'm getting all rows and all XHR Get URLs have &dojo.preventCache= parameter which is exactly what I wanted. Next we'll test in customer environment to see if this solves their problem.
Update
As Julian points out in his blog I could also use a Web Site Rule to set Expires or cache-control http response headers.
Update
The customer reports it's working now for them!

Resources