7

I'm trying to get an item from local storage using Selenium Webdriver.

I followed this site but when I run my code I get NullPointerException.

When I debug the code I see the function: getItemFromLocalStorage returns NULL for some reason.

Here is my code:

public class storage
 {
    public static WebDriver driver;
    public static JavascriptExecutor js;

    public static void main(String[] args)
    {       
        System.setProperty("webdriver.chrome.driver", "D://chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://html5demos.com/storage");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.id("local")).sendKeys("myLocal");
        driver.findElement(By.id("session")).sendKeys("mySession");
        driver.findElement(By.tagName("code")).click(); // just to escape textbox

        String sItem = getItemFromLocalStorage("value");
        System.out.println(sItem);
    }

    public static String getItemFromLocalStorage(String key)
    {
        return (String) js.executeScript(String.format(
            "return window.localStorage.getItem('%s');", key));
    }
}
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
John
  • 73
  • 1
  • 1
  • 5
  • Hi John, and welcome to Stack Overflow. Can you edit your question to include some more information about the error? A partial callstack would be useful, showing what the exception is and where in your code it is being raised. – Vince Bowdren Apr 19 '15 at 21:20

3 Answers3

3

That is because you forgot to instantiate js object correctly. Add below line after driver = new ChromeDriver();.

js = ((JavascriptExecutor)driver);

It will work.

Priyanshu
  • 3,040
  • 3
  • 27
  • 34
0

I assume you have NPE on your driver instance. You can setup driver location property while driver instance creating:

final ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
        .usingDriverExecutable(new File("D://chromedriver.exe")).build();

driver = new ChromeDriver(chromeDriverService);

BTW, I used selenium 2.44.0

androberz
  • 744
  • 10
  • 25
  • Sorry, I don't know what NPE is, but when I insert your code I still get the same exception. I use selenium 2.44 as well. – John Apr 20 '15 at 03:05
  • NPE stands for null pointer exception. So, do you have code that doing something with your sItem var, which is null? I can't see a line that can throw exception for the case sItem=null – androberz Apr 20 '15 at 04:47
  • Anyway, if you getting a variable from localStorage, you should put it first. As I can see at http://html5demos.com/storage localStorage is empty. In this case you return value from the getItemFromLocalStorage() will be null. – androberz Apr 20 '15 at 06:53
0

Use this Code

        WebStorage webStorage = (WebStorage) new Augmenter().augment(driver);
        LocalStorage localStorage = webStorage.getLocalStorage();

        String user_data_remember = localStorage.getItem("user_data_remember");
        String emailAfterLogout;
        String passwordAfterLogout;

        if (!user_data_remember.equals("")) {

            JSONObject jsonObject = new JSONObject(user_data_remember);

            Boolean remember = jsonObject.getBoolean("remember");

            if (remember) {
                emailAfterLogout = jsonObject.getString("email");
                passwordAfterLogout = jsonObject.getString("password");

                if (emailAfterLogout.equals(email) && passwordAfterLogout.equals(password)) {
                    System.out.println("Remember me is working properly.");
                } else {
                    System.out.println("Remember me is not working.");
                }
            }
        } else {
            System.out.println("Remember me checkbox is not clicked.");
        }