Hi,
I am new to K6 and I have a request to Load Test a site with up to 100 vu’s but the problem I am facing is that the site requires you to login. The login is using Laravel so I need a XSRF-TOKEN and a laravel_session but when I try and login it does not work it complains about Captcha Failed.
I’ve done a lot of googling and I want someone to just answer a few questions for me please it will help clear some stuff up?
I understand how to test a site without authentication and it works fine but how do I get all these users to login? These are VUs so they do not have any username or password so how do I create this? Do I need to create each users in the UI/DB and then store these credentials in a file and then loop through this in k6? Say if I want to do 5000 users this would be a nightmare?
Is my thought process right in the first place? I was thinking that I need to login with all 100 users and then hit an API such as editUser with all 100 users at the same time? I could obviously ramp this up but you get what I mean. Is this the approach you would take for load testing a site?
This is all very simple if you don’t need to login you can bypass all this but unfortuantely I do need to login to access these APIs.
export function setup() {
// STEP 1: GET /login to receive CSRF token and cookies
const loginPage = http.get(`${BASE_URL}/login`);
const xsrfToken = decodeURIComponent(loginPage.cookies['XSRF-TOKEN'][0].value);
const cookies = {
'XSRF-TOKEN': loginPage.cookies['XSRF-TOKEN'][0].value,
'laravel_session': loginPage.cookies['laravel_session'][0].value,
};
// STEP 2: Prepare login payload (form-encoded)
const payload = `email=${USERNAME}&password=${PASSWORD}`;
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'X-XSRF-TOKEN': xsrfToken, // required by Laravel
};
// STEP 3: POST login with cookies and CSRF token
const loginRes = http.post(`${BASE_URL}/login`, payload, {
headers,
cookies,
redirects: 0,
});
const authCheck = http.get(`${BASE_URL}/myleagues`, {
headers,
cookies,
redirects: 0,
});
check(authCheck, {
'✅ authenticated access to /myleagues': (r) =>
r.status === 200,
'❌ not redirected back to login': (r) =>
!(r.status === 302 && r.headers.Location && r.headers.Location.includes('/login')),
});
return {
cookies: loginRes.cookies,
};
}
This is what I was trying with to see if I could login with just one user but that doesn’t work yet because of the captcha.
Please help!
Thanks
Ben