Fast authorization. Level— local storage.

Aliaksandr Rasolka
1 min readMay 11, 2018

--

One more method to authorize using faster way.

Photo by Alex Holyoake on Unsplash

Previously I talk about fast authorization via cookie. But now, I found one more variant — it’s a browser local storage.

Mechanism look like cookie, but we need to change our JavaScript code a little.

Last time I use duplicated code and it’s look kinda weird. Let’s upgrade it!

Leave old JavaScript executor and open methods untouched:

private void executeJavaScript(final String jsCode) {
((JavascriptExecutor) webDriver).executeScript(jsCode);
}
private void open(final String url) {
webDriver.get(url);
}

Create private method that set name value in local storage:

private void setItemInLocalStorage(final String item, final String value) {
final String jsCode = String.format(
"window.localStorage.setItem('%s','%s');",
item,
value);
executeJavaScript(jsCode);
}

Create a new public method that will authorize application:

public void authorizeWithLocalStorage(final AccessToken accessToken) {
open("https://my-supa-dupa-app.com/robots.txt");
setItemInLocalStorage("$accessTokenId", accessToken.getId());
setItemInLocalStorage("$currentUserId", accessToken.getUserId());
}

Works as expected stable and fast. Awesome!

Happy testing! ~_~

--

--