Local Storage

Local storage is a web browser feature that allows websites to store data locally on a user's computer

Local storage is a web browser feature that allows websites to store data locally on a user's computer. It's a way for web applications to persistently store information on the client side (in the user's browser) rather than relying solely on server-side databases or cookies. This can be useful for various purposes, such as caching data for offline use, saving user preferences, or reducing the need to repeatedly fetch data from a server.

Here are some key points about local storage in web browsers:

Storage Mechanism: Local storage uses a simple key-value pair storage mechanism. You can store data in the form of strings, and each piece of data is associated with a unique key. This data is stored in a structured manner and can be easily accessed and manipulated using JavaScript.

Storage Capacity: Local storage typically allows websites to store up to 5-10 MB of data per domain, depending on the browser. This limit is per domain, so different websites on the same browser won't share their local storage.

Storage Capacity: Local storage typically allows websites to store up to 5-10 MB of data per domain, depending on the browser. This limit is per domain, so different websites on the same browser won't share their local storage.

Security: Data stored in local storage is limited to the same-origin policy, which means that a website can only access data it has stored in its own local storage, not data from other websites. This provides a level of security and privacy.

Data Type: Local storage can only store data as strings. If you want to store complex data types like objects or arrays, you need to convert them to JSON strings before storing and parse them when retrieving.

Here's how you can use local storage in JavaScript:

                            
// Storing data with a key
localStorage.setItem('username', 'JohnDoe');

// Retrieving data by key
const username = localStorage.getItem('username');

// Removing data by key
localStorage.removeItem('username');

// Clearing all data in local storage for the current domain
localStorage.clear();
                            
                        

how can we handle this using selenium java?

                            
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocalStorage {

	public static void main(String[] args) {
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.selenium.dev/");
		// Store data in local storage
		String script = "localStorage.setItem('username', 'ranga swami reddy');";
		((JavascriptExecutor) driver).executeScript(script);
		// Retrieve data from local storage
		script = "return localStorage.getItem('username');";
		String storedValue = (String) ((JavascriptExecutor) driver).executeScript(script);
		System.out.println("Stored value: " + storedValue);
		// Remove data from local storage
		script = "localStorage.removeItem('username');";
		((JavascriptExecutor) driver).executeScript(script);
		// Clear all data from local storage
		script = "localStorage.clear();";
		((JavascriptExecutor) driver).executeScript(script);
		driver.quit();
	}

}