javaselenium-webdriverremotewebdriver

WebStorage with RemoteWebDriver


I am trying to access WebStorage when running my tests, I am running tests locally using ChromeDriver and remotely in the pipeline using RemoteWebDriver.

I am unable to access WebStorage when using RemoteWebDriver

WebStorage webStorage = (RemoteWebStorage) new Augmenter().augment(driver);

I am getting:

org.openqa.selenium.remote.RemoteWebDriver cannot be cast to org.openqa.selenium.remote.html5.RemoteWebStorage


Solution

  • You will have to use below hack to access the local storage in RemoteWebDriver . Tested it on chrome browser using RemoteWebdriver selenium-java - 4.0.0-alpha-2 , Chrome Version 76.0.3809.100 and ChromeDriver 76.0.3809.68 . Working fine for me.

    Import below classes

     import org.openqa.selenium.remote.RemoteExecuteMethod;
     import org.openqa.selenium.remote.html5.RemoteWebStorage;
     import org.openqa.selenium.html5.LocalStorage;
    

    Use below code to access the local storage

     RemoteExecuteMethod executeMethod = new RemoteExecuteMethod((RemoteWebDriver) driver);
     RemoteWebStorage webStorage = new RemoteWebStorage(executeMethod);
     LocalStorage storage = webStorage.getLocalStorage();
     storage.setItem("test", "test");