In HTML5, the new feature of Web Storage allows users to store data in a local browser. Cookies can be used to accomplish this task in earlier browsers, but web Storage are more secure and efficient, and web Storage are stored by key-value pairs, and only their Web applications can access stored data
The Web Storage is divided into the following 2 types
1. Localstorage: Stored data does not expire permanently
2. Sessionstorage: Stored data is only valid in the current session
Localstorage and Sessionstorage information can be viewed in the Resources tab of Chrome's developer tools
Example of Operation Localstorage:
Package com.learingselenium.android;
Import static org.junit.Assert.assertEquals;
Import org.junit.*;
Import Org.openqa.selenium.WebDriver;
Import Org.openqa.selenium.html5.LocalStorage;
Import Org.openqa.selenium.html5.WebStorage;
Import Org.openqa.selenium.android.AndroidDriver;
...
Webdriver Driver = new Androiddriver ("Http://localhost:8888/wd/hub");
Driver.get ("http://www.1.com");
Localstorage localstorage = ((webstorage) driver). Getlocalstorage ();
System.out.println ("The size of Localstorage is:" + localstorage.size ());
Localstorage.setitem ("Key1", "Learningselenium");
System.out.println (Localstorage.getitem ("Key1"));
Driver.quit ();
...
From the execution results, Localstorage retains the previously stored data. If you need to ensure that localstorage is clean every time you run a test case, you need to execute the following code snippet before using Localstorage:
Localstorage localstorage = ((webstorage) driver). Getlocalstorage ();
Localstorage.clear ();
Example of Operation Sessionstorage:
...
Webdriver Driver = new Androiddriver ("Http://localhost:8888/wd/hub");
Driver.get ("http://www.1.com");
Sessionstorage sessionstorage = ((webstorage) driver). Getsessionstorage ();
System.out.println ("The size of Sessionstorage is:" + sessionstorage. Size ());
Sessionstorage. SetItem ("Key1", "Learningselenium");
System.out.println (Sessionstorage getItem ("Key1"));
Driver.quit ();
...
From the execution results, Sessionstorage does not retain the data stored in the previous session, that is, the data stored in Sessionstorage is only valid in the current session.
Android HTML5 in Web Storage