Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver Add In LocalStorage

I try to add below flag into LocalStorage and it doesn't work.

localStorage.setItem('ClientRedirectEnabled', 'true');

My code below

public void FlagIntroduce() throws InterruptedException {
    Thread.sleep(20000);
    LocalStorage localStorage = new LocalStorage() {
        @Override
        public String getItem(String s) {
            return null;
        }

        @Override
        public Set<String> keySet() {
            return null;
        }

        @Override
        public void setItem(String s, String s1) {
            setItem("ClientRedirectEnabled", "true");
        }

        @Override
        public String removeItem(String s) {
            return null;
        }

        @Override
        public void clear() {

        }

        @Override
        public int size() {
            return 0;
        }
    };


}

When test starts in chromedriver I'm opening console and Application bookmark. In Local Storage I don't have key and value which I set in setItem

like image 760
goney225 Avatar asked Aug 21 '19 11:08

goney225


2 Answers

Check example here LocalStorageTest

  @Test
  public void testLocalStorageSetAndGetItem() {
    ChromeDriver driver = new ChromeDriver();
    driver.get("https://www.testurl");

    LocalStorage local = ((WebStorage) driver).getLocalStorage();    

    local.setItem("FOO", "BAR");

    local.clear();

  }
like image 190
Rahul L Avatar answered Oct 06 '22 00:10

Rahul L


You can use a JavascriptExecutor to do the job for you.

If you are using Java as your langugage, following code would help you

JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("localStorage.setItem(arguments[0],arguments[1])",key,value));
like image 26
work_ishaan Avatar answered Oct 05 '22 23:10

work_ishaan