Set Cookie and Token inside Local Storage using K6

I can use HAR to record browser session. Then using converter, I can generate K6 script. With few modification, this script works.

Is it possible for me to set Cookie and Token inside Local Storage using K6 ?

HI @vishal.lanke,

Local storage is a web browser technology and isn’t support by k6.

Maybe if you explain what you are trying to do someone else or I can help you :slight_smile:

1 Like

Just to add to @mstoykov’s answer, at least parts of what you describe is possible using the built-in cookie jar. See Cookies. However, this should likely not be necessary as you should be able to just store your cookie in a variable and reuse it in subsequent requests.

Hope that helps!

can i get a token value that is stored in the local storage?
my app is using authentication so i can’t do anything without retrieving this value + each time i need to remove all the local storage.

anyone have an idea how to do it in k6?

k6 is not a browser, so it doesn’t execute the JavaScript code on web pages and doesn’t have local storage.

@nadiush, I’d advise you to inspect the HTTP requests your web app is sending to your backend. The authentication value may be stored in the local storage of the browser, but your website JS code sends it to the backend via an HTTP header or parameter to authenticate the user, which you can also do with k6. You just need to know how your app does it and code the same logic in k6.

Take a look at the following articles that describe how you can record the actual HTTP traffic when browsing your website and logging into it: Using the HAR converter and Using the browser recorder. The resulting HAR files and converted k6 scripts will show how the authentication is performed and can be used as a good starting place for handling it properly.