The purpose of this instance is to be able to read HTML5's localStorage and sessionStorage, and delete the stored content.
The specific example code is as follows:
package cn.om.TestHTML5; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; public class TestHtml5Storage { WebDriver driver; String localStoreUrl; String sessionStoreUrl; @Test public void testHtml5localStorage() { driver.get(localStoreUrl); JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; // Call the JavaScript statement "return // localStorage.lastname" gets the stored value of "lastname" stored in localStorage String lastname = (String) jsExecutor.executeScript("return localStorage.lastname;"); // Assert if the retrieved stored value is "Smith" Assert.assertEquals("Gates", lastname); // Call the JavaScript statement "localStorage.clear()" to clear all variable values stored in localStorage jsExecutor.executeScript("localStorage.clear();"); } @Test public void testHtml5sessionStorage() { driver.get(sessionStoreUrl); JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; // Call the JavaScript statement "return // localStorage.lastname" gets the stored value of "lastname" stored in localStorage jsExecutor.executeScript("sessionStorage.setItem('lastname','Gates');"); String lastname = (String) jsExecutor.executeScript("return sessionStorage.lastname;"); // Assert if the retrieved stored value is "Smith" Assert.assertEquals("Gates", lastname); //Clear the "lastname" item stored in sessionStorage jsExecutor.executeScript("sessionStorage.removeItem('lastname');"); // Call the JavaScript statement "localStorage.clear()" to clear all variable values stored in localStorage jsExecutor.executeScript("sessionStorage.clear();"); } @BeforeMethod public void beforeMethod() { System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); localStoreUrl = "http://www.w3school.com.cn/tiy/t.asp?f=html5_webstorage_local"; sessionStoreUrl = "http://www.w3school.com.cn/tiy/t.asp?f=html5_webstorage_session"; } @AfterMethod public void afterMethod() { driver.quit(); } }