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

**URL:** <https://forum.katalon.com/t/how-to-extract-the-token-from-browser-session-and-use-it-later/39302>\
**Category:** API Testing\
**Tags:** api-testing, katalon-studio\
**Created:** [January 31, 2020, 10:49am UTC](https://forum.katalon.com/t/how-to-extract-the-token-from-browser-session-and-use-it-later/39302 "2020-01-31T10:49:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rojinapariyar.002](https://avatars.discourse-cdn.com/v4/letter/r/f4b2a3/32.png) [@rojinapariyar.002](https://forum.katalon.com/u/rojinapariyar.002)\
**Post date:** [January 31, 2020, 10:49am UTC](https://forum.katalon.com/t/how-to-extract-the-token-from-browser-session-and-use-it-later/39302/1 "2020-01-31T10:49:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [January 31, 2020, 2:03pm UTC](https://forum.katalon.com/t/how-to-extract-the-token-from-browser-session-and-use-it-later/39302/2 "2020-01-31T14:03:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rojinapariyar.002](https://avatars.discourse-cdn.com/v4/letter/r/f4b2a3/32.png) [@rojinapariyar.002](https://forum.katalon.com/u/rojinapariyar.002)\
**Post date:** [February 5, 2020, 6:33am UTC](https://forum.katalon.com/t/how-to-extract-the-token-from-browser-session-and-use-it-later/39302/3 "2020-02-05T06:33:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [February 5, 2020, 5:50pm UTC](https://forum.katalon.com/t/how-to-extract-the-token-from-browser-session-and-use-it-later/39302/4 "2020-02-05T17:50:05Z")

</div>

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;`

---

<div class="post-metadata">

**Author:** ![rojinapariyar.002](https://avatars.discourse-cdn.com/v4/letter/r/f4b2a3/32.png) [@rojinapariyar.002](https://forum.katalon.com/u/rojinapariyar.002)\
**Post date:** [February 7, 2020, 8:36am UTC](https://forum.katalon.com/t/how-to-extract-the-token-from-browser-session-and-use-it-later/39302/5 "2020-02-07T08:36:00Z")

</div>

Thanks, now it’s working.
