Cleaning local and session storage

When tests are run on HTML5 applications using local or session storage, lots of local and session storage entries will be created. When running tests in a batch on an already used environment, cleaning local and session storage is a good idea before you begin your tests.

In this recipe, we will briefly see how to remove local or session storage items and clean the values.

How to do it...

To remove a specific item from local or session storage, you can use the removeItem() method in the following way:

// We can use removeItem method to remove a specific Key along with it's value from local storage
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("localStorage.removeItem(lastname);");

For session storage, the following code snippet is used:

JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("sessionStorage.removeItem(lastname);");

To clear all items, in local or session storage, you can use the clear() method in the following way:

//To Clear Storage values
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("localStorage.clear();");

For session storage, the following code snippet is used:

//To Clear Storage values
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("sessionStorage.clear();");

See also

  • The Web storage – testing session storage recipe
  • The Web storage – testing local storage recipe covered earlier in this chapter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.98.208