How to user variable form webdriver sampler in HTTP Header Manager - jmeter

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.

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 load test GraphQL API with Jmeter

I want to configure Jmeter to test the performance of following GraphQL API. I am unable to configure.
API End Point : https://graphqlzero.almansi.me/api
Query : query: {
user(id: 1) {
id
name
}
}``
Please help me with a solution.
It seems that you're trying to execute a POST request therefore you need to:
Add HTTP Request sampler to your test plan and configure it like:
Textual representation of the body just in case:
{
"query": "{user(id: 1) {id name}}"
}
Add HTTP Header Manager to send Content-Type header with the value of application/json
Demo:
If you have the call as a cURL command line, you can use menu:
Tools > Import from curl
Otherwise:
add Thread group
add a Header Manager inside it with Content-Type set to application/javascript
Add in it a Http Request, put in host the host name and in path the /api/

Capturing cookies in JMeter

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

Jmeter Clear cookies after each http request

In jmeter context, is there any way to clear cookies after each http request within the same thread group?
Why in that case you need HTTP Cookie Manager at all? Just remove it and you'll get expected behaviour.
Just in case you have some form of weird negative test scenario:
Add a Beanshell Listener or Beanshell Assertion at the same level with all HTTP Request samplers
Put the following code into "Script" area
import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();
manager.clear();
See the following reference material:
JMeterContext class JavaDoc (referenced as ctx in the above script)
CookieManager class JavaDoc
How to Use BeanShell: JMeter's Favorite Built-in Component

Creating custom header variable using Fiddler proxy server

I understand that Fiddler proxy server supports custom HTTP header variables. I'm new at this, my goal is to simulate a passed custom variable the way an F5 appliance would pass an HTTP header variable (string) for a web app. The variable string is used to authenticate the user. I do not have access to a load balancing appliance and have to find a way to simualte or manually add it.
Will apreciate any input on how to accomplish this...
You can add your own header variables to the OnBeforeRequest function (or OnBeforeResponse) within CustomRules.js, such as the following:
oSession.oRequest["NewHeaderName"] = "New header value";
More info: http://fiddler2.com/documentation/KnowledgeBase/FiddlerScript/ModifyRequestOrResponse

Resources