Created
October 29, 2012 10:34
-
-
Save anonymous/3972875 to your computer and use it in GitHub Desktop.
Selenium WebDriver Javascript execution for sessionStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openqa.selenium.JavascriptExecutor; | |
import org.openqa.selenium.WebDriver; | |
public class SessionStorage { | |
private JavascriptExecutor js; | |
public SessionStorage(WebDriver driver) { | |
this.js = (JavascriptExecutor) driver; | |
} | |
public void removeItemFromSessionStorage(String item) { | |
js.executeScript(String.format( | |
"window.sessionStorage.removeItem('%s');", item)); | |
} | |
public boolean isItemPresentInSessionStorage(String item) { | |
if (js.executeScript(String.format( | |
"return window.sessionStorage.getItem('%s');", item)) == null) | |
return false; | |
else | |
return true; | |
} | |
public String getItemFromSessionStorage(String key) { | |
return (String) js.executeScript(String.format( | |
"return window.sessionStorage.getItem('%s');", key)); | |
} | |
public String getKeyFromSessionStorage(int key) { | |
return (String) js.executeScript(String.format( | |
"return window.sessionStorage.key('%s');", key)); | |
} | |
public Long getSessionStorageLength() { | |
return (Long) js | |
.executeScript("return window.sessionStorage.length;"); | |
} | |
public void setItemInSessionStorage(String item, String value) { | |
js.executeScript(String.format( | |
"window.sessionStorage.setItem('%s','%s');", item, value)); | |
} | |
public void clearSessionStorage() { | |
js.executeScript(String.format("window.sessionStorage.clear();")); | |
} | |
} |
From version 2.42 of selenium you can use webDriver.getLocalStorage()
if the driver supports it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that HTML5 localStorage API is very similar, just replace "sessionStorage" with "localStorage".