'How can I get "token" from this localStorage item in Cypress?

Here my localStorage Here my localStorage2 localstorage displays while test runnig

I shared the localStorage's SS.

I need to use "token" from "test_auth" in Cypress and tried this:

const token = localStorage.getItem('test_auth.token')

But it didn't work. What should I do?



Solution 1:[1]

local storage stores strings so you should first parse what you get to JSON and then get what you want. below code can be your answer:

const token = JSON.parse(localStorage.getItem('test_auth')).token

Solution 2:[2]

It's looking for a storage item with the name 'test_auth.token'

You want to get the storage object then access it's .token property

const token = localStorage.getItem('test_auth').token

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammad Salehi
Solution 2 Fody