How to extract the token from browser session and use it later

I’ve successfully done the extracting of access_token from login API and passing to another POST method API.In the same way, how to extract access_token from browser local storage and use it later?

1 Like

First ideea i have now is … you have to inspect with devtoools how the api request is hanled by the browser.
Usualy the tokens received are stored in a cookie, so grabbing the cookie and re-use it later may work … if the token won’t expire

In my case, access_token is stored on local storage of the browser and there is no cookie saved.Now, I want to know how to execute javascript to store access token on global variable and use it later for another test.

You can execute any JS code that you want with selenium. Give this a try:

import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebDriver
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser("");
WebDriver driver = DriverFactory.getWebDriver();
JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;

// put an example item into local storage
jsExecutor.executeScript("localStorage.setItem(\"test\", \"test\")");

// retrieve the item value
String itemFromStorage = jsExecutor.executeScript("return localStorage.getItem(\"test\")");

From there, it’s as simple as assigning the value to a global variable, like this:

GlobalVariable.myVariable = itemFromStorage;

Thanks, now it’s working.