Skip to content

Instantly share code, notes, and snippets.

@roydekleijn
Created March 2, 2013 22:35
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save roydekleijn/5073579 to your computer and use it in GitHub Desktop.
Save roydekleijn/5073579 to your computer and use it in GitHub Desktop.
Selenium WebDriver Javascript execution for localStorage
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class LocalStorage {
private JavascriptExecutor js;
public LocalStorage(WebDriver webDriver) {
this.js = (JavascriptExecutor) webDriver;
}
public void removeItemFromLocalStorage(String item) {
js.executeScript(String.format(
"window.localStorage.removeItem('%s');", item));
}
public boolean isItemPresentInLocalStorage(String item) {
return !(js.executeScript(String.format(
"return window.localStorage.getItem('%s');", item)) == null);
}
public String getItemFromLocalStorage(String key) {
return (String) js.executeScript(String.format(
"return window.localStorage.getItem('%s');", key));
}
public String getKeyFromLocalStorage(int key) {
return (String) js.executeScript(String.format(
"return window.localStorage.key('%s');", key));
}
public Long getLocalStorageLength() {
return (Long) js.executeScript("return window.localStorage.length;");
}
public void setItemInLocalStorage(String item, String value) {
js.executeScript(String.format(
"window.localStorage.setItem('%s','%s');", item, value));
}
public void clearLocalStorage() {
js.executeScript(String.format("window.localStorage.clear();"));
}
}
@EECOLOR
Copy link

EECOLOR commented Sep 7, 2014

From version 2.42 of selenium you can use webDriver.getLocalStorage() if the driver supports it.

@cpomp
Copy link

cpomp commented Dec 2, 2015

I just has to access the local storage and I still found this to be the best method.
The method you mentioned is driver specific so is not part of the interface so you would still need to cast the WebDriver to the correct implementation. This doesn't make the code browser independent which is a deal breaker for me.

I really hope that they add it to the interface but until the do I believe this is the way to go.
Thanks for the code, works great!

@matthias-dirickx
Copy link

Enjoyed the code in java alread, now ported it to c# (https://gist.github.com/matthias-dirickx/d51784a6da0090f425cb73f28824cd87) Thanks alot!

@venkat2336
Copy link

Hi Team,

If key value length is 11808 Unable to update session storage key value using selenium automation Small length key values are setting but long length key values getting JS error
Manually it is working but using selenium automation getting JS error. setItemInsessionStorage method using:
java.lang.AssertionError: org.openqa.selenium.JavascriptException: javascript error

Please guide me how to handle

@matthias-dirickx
Copy link

matthias-dirickx commented Jun 25, 2020

I'm not currently actively working with Selenium, so I'm not diving into this. It is interesting though. Both that it does not work, and the fact that you need that size for an update in the session storage. For me it was use cases like making sure some items like the chosen user language (which was in the session storage in that particular implementation) was set correctly. So no experience with large chunks of data. Can you get your hands on more information for what concerns stack trace, maybe some leads there? If you do find out, please put it in a comment :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment