Store selenium request in jmeter to use in the next request - selenium

I am trying to use the login from selenium to use in the next http request in jmeter. I currently have the below
My selenium script works perfectly and logs me into my website as per the below
WDS.sampleResult.sampleStart()
WDS.browser.get('https://www.testwebsite.com')
WDS.browser.findElement(org.openqa.selenium.By.linkText("Login")).click();
WDS.browser.findElement(org.openqa.selenium.By.id("username")).sendKeys("myusername");
WDS.browser.findElement(org.openqa.selenium.By.id("password")).sendKeys("mypassword");
WDS.browser.findElement(org.openqa.selenium.By.xpath("//button[#type='submit']")).click();
java.lang.Thread.sleep(5000)
WDS.browser.findElement(org.openqa.selenium.By.xpath("//*[contains(text(),'Skip for now')]")).click();
var cookies = WDS.browser.manage().getCookies()
java.lang.Thread.sleep(5000)
WDS.sampleResult.sampleEnd()
What i want to do is keep this session alive and then on the next request use something like this
GET - https://test/anotherpage.com
so i want it to recognise im still logged in. How can i keep the session alive so my jmeter http request can work?

In your WebDriver Sampler you need to store the cookies object into JMeter Variables like:
WDS.vars.putObject('cookies', cookies)
Add HTTP Cookie Manager to your Test Plan
Add JSR223 PreProcessor as a child of the HTTP Request sampler where you need to get the authentication context and put the following code into "Script" area:
def cookies = vars.getObject('cookies')
log.info('cookies=' + cookies)
cookies.collect { cookie ->
new org.apache.jmeter.protocol.http.control.Cookie(cookie.getName(),
cookie.getValue(),
cookie.getDomain(),
cookie.getPath(),
cookie.isSecure(),
cookie.getExpiry().getTime())
}.each { cookie -> sampler.getCookieManager().add(cookie) }
This way you can copy the cookies from the browser to HTTP Request sampler so the request will be authenticated
More information: Modifying Cookies in JMeter with Groovy

Using cookies in selenium, which it looks like yuou've fetched all of them:
https://www.selenium.dev/documentation/en/support_packages/working_with_cookies/
YHou should be able to store those cookies, which should be enough to preserve your session.
https://www.blazemeter.com/blog/using-http-cookie-manager-jmeter
To save cookies as variables, define the property
"CookieManager.save.cookies=true". The names of the cookies contain
the prefix "COOKIE_" before they are stored (this avoids accidental
corruption of local variables). To revert to the original behavior,
define the property "CookieManager.name.prefix= " (with one or more
spaces). If enabled, the value of a cookie with the name TEST can be
referred to as ${COOKIE_TEST}.
That being said, you probably don't need to use selenium (and launch a full browser) to do the login, you could probably fire a simple http request to post the login form. This would be less over head
https://guide.blazemeter.com/hc/en-us/articles/207421705-How-to-use-JMeter-for-Login-Authentication-How-to-use-JMeter-for-Login-Authentication

Related

Passing Cookie as a Request Header - SSO JMeter

I'm doing load testing for an application using JMeter for which I need to perform SSO authentication. I have to send a cookie as request header like I it is done below:
I have the value that I need to send, which I have extracted using Regular Expression Extractor on a previous Request. How can I achieve this?
I am using JMeter 3.1
Add a cookie manager to your Test Plan
Extract cookie with your Regular Expression Extractor to some variable (e.g. my_cookie)
Add JSR223 Sampler and use this code
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
CookieManager manager = sampler.getCookieManager();
Cookie cookie = new Cookie("SESSION_COOKIE_NAME", "${my_cookie}", "${servername}", "/", false, 0);
manager.add(cookie);
More info for CookieManager class here
You basically don't need to extract anything, just add HTTP Cookie Manager to your Test Plan and it will automatically handle cookies.
If you need certain cookie value to use it somewhere else you can add the next line to user.properties file (lives in JMeter's "bin" folder)
CookieManager.save.cookies=true
and restart JMeter to pick the property up. Once done you will be able to refer cookie value as ${COOKIE_mc} where required.
See Using the HTTP Cookie Manager in JMeter for more information.

Handling cookies in HTTP request in Jmeter

I have a Jmeter load project the requires retrieving 2 values of cookies and resend them in the other requests.
Here is my project test plan:
-Test Plan
-Thread Group
-Login request
-Payment page
-Payment history page
-HTTP Cookie Manager
-HTTP Header Manager
-View Results Tree
Cookie Data:
JSESSIONID=0000SZb55xyLAaqLlDzumq_PpIw:-1; XSRF-TOKEN=a684e233-648e-4219-ae21-25fb362e232d
The cookie data is received successfully from the login request by cookie manager and sent in the second request (Payment page) but in the third request (Payment history page) only JSESSIONID is sent and I don't know why the second cookie is not sent in the third request.
Most probably you are not getting this cookie in Set-Cookie response header for second request therefore it doesn't get picked up by the cookie manager. If your application expects the cookie in 3rd request it might be your application issue.
As a workaround you can store the cookie value into a JMeter Variable for later re-use.
Add the next line to user.properties file (lives in the "bin" folder of your JMeter installation
CookieManager.save.cookies=true
Restart JMeter to pick the property up
That's it, now you should be able to refer cookie value as ${COOKIE_XSRF-TOKEN} where required, for example add another HTTP Cookie Manager as a child of the 3rd request (according to Scoping Rules "local" cookie manager will override the "global" one) and define XSRF_TOKEN and other cookies if needed there)
See Using the HTTP Cookie Manager in JMeter article for more information on handling cookies in JMeter tests.

jmeter auth token issue

I have internal web application, which I have to test using Jmeter.
The application has secure protections as a username, password and auth token.
Auth token is changing with every new session. I'm unable to path through secure token. Token is include in URL, which look like
http://mytraining.application.net/(S(vj1ckr0nqyvjq3blehcl2jwl))/ApplDefault.aspx?tabid=1. Cookies has look like
Cookie: AUTH_TOK_vj1ckr0nqyvjq3blehcl2jwl=vj1ckr0nqyvjq3blehcl2jwl; AUTH_TOK_syq3r1yu4equ515xzunjobhb=syq3r1yu4equ515xzunjobhb;
So, my Jmeter able to successfully run only when I submit current token in URL.
Please let me know if you have any idea, how to avoid submitting token in all places (35) every time.
[![enter image description here][1]][1]
My AUTH_TOKEN
Regular Expression
It seems you can figure you your Auth_TOK value from the cookies
Add the next line to user.properties file (located in JMeter's "bin" folder
CookieManager.save.cookies=true
Restart JMeter to pick the property up
Add HTTP Cookie Manager to your Test Plan
Now you should be able to see Cookies stored as JMeter Variables using Debug Sampler and View Results Tree listener combination and use cookies values as part of your URL
I would also recommend checking out ASP.NET Login Testing with JMeter as my expectation is that you will have to deal with few other dynamic parameters.
First try with HTTP Cookie Manager as mentioned by Dmitri. This would work if auth token is used in Headers, like Cookie header. Jmeter automatically fetches from Set-Cookie Header, set by server, (when you add HTTP Cookie Manager) for each thread and store it locally, so that it can use in subsequent requests.
If Auth token is used in other places (but not headers) such as part of Url, request body etc, then we should explicitly capture it using Regular Expression Extractor (post processor) and
Regular Expressions Jmeter to correlate the dynamic values. Once you capture the value into a local_variable, we replace the actual values by local_variable (Reference name field in Regular Expression Extractor) wherever we observe the dynamic value in subsequent requests, here auth token, using syntax ${local_variable}.

Jmeter testing for Login

I have to test a web-app for login using Jmeter.
How is that done?
Should I give the path of the login page in the HTTP Request sampler and add the username and password to the login config element?
It needs to be realistic hence your test should be designed as follows:
HTTP GET Request (simulate opening Login page with a browser)
Post Processor to fetch dynamic parameters values (if any)
HTTP POST Request (login event itself) where you pass your credentials and dynamic parameters
(optional) Response Assertion to verify that user is really logged i
I would also recommend adding HTTP Cookie Manager to represent browser cookies and deal with cookie-based authentication.
See How To Use JMeter For Login Authentication guide for step-by-step overview and demo

How to skip CSRF inmpemented webpages in apache jmeter while recording

I have implemented CSRF (corss site request forgery) in login page of my application.
when i try to do load test using apache jmeter, i am getting (login time exceeded) error.
if i create custom debug.jar to remove csrf it is working fine.
it is getting very tedious for each time to load the debug.jar while doing load test.
I am new to jmeter can any one help me out with this.
Thanks.
CSRF usually results in mandatory either header or cookie which needs to exist elsewise the request won't be served. CSRF token usually lives in every response so it's "classic" correlation example. So the flow should look as follows:
GET first page
Extract CSRF token using one of the following:
Regular Expression Extractor
CSS/JQuery Extractor
XPath Extractor
GET next page
Send CSRF header or cookie using
HTTP Cookie Manager
HTTP Header Manager
These "managers" should be added as a child of "next" request.