11 May 2026 Leave a comment QA
If your tests depend on a clean browser state, clearing localStorage before a specific test group is a practical way to avoid hidden side effects. This is especially useful when one test stores login state, feature flags, preferences, or cached data that could influence the next test.
Why this matters
localStorage persists across page loads and often even across browser sessions, depending on the setup. That makes it convenient for applications, but risky for automated tests. If you do not reset it, a test may pass or fail for the wrong reason.
The safest approach is to clear storage explicitly in your test setup, rather than trying to rely on a browser-specific shortcut.
Recommended approach: use JavaScript execution
For modern Selenium setups, the most reliable solution is to execute JavaScript directly:
driver.execute_script("window.localStorage.clear();")
If you also need to clear session-scoped data, you can clear both:
driver.execute_script("window.localStorage.clear();")
driver.execute_script("window.sessionStorage.clear();")
This works well because it uses the browser’s own JavaScript environment, which is broadly supported and does not depend on deprecated WebStorage interfaces.
When to use WebStorage APIs
Some older Selenium code uses WebStorage-specific methods. That can still be useful in legacy projects, but it is less future-proof. If you are maintaining older tests, you may see code like this:
if (driver instanceof WebStorage) {
WebStorage webStorage = (WebStorage)driver;
webStorage.getLocalStorage().clear();
webStorage.getSessionStorage().clear();
}
This is reasonable only if your driver actually implements that interface. In practice, many teams now prefer JavaScript because it is simpler and more consistent across current browser drivers.
What not to do
- Do not rely on opening a
javascript:URL throughdriver.get()as your primary solution. That approach is fragile and can fail depending on browser and driver behavior. - Do not assume that a storage API exists just because your test framework exposes a constant or helper name.
- Do not clear storage in the middle of a test unless you intentionally want to simulate a fresh browser state.
Best practice: clear storage in test setup
The cleanest pattern is to reset browser storage in a setup hook before a test class, test suite, or selected group of tests. That keeps the test logic focused and makes the cleanup predictable.
For example:
- Before all tests in a login-related suite: clear storage once so each test starts fresh.
- Before each test: use this when tests must be fully isolated.
- Before selected scenarios only: use this when only some tests depend on a clean browser state.
If you are using C#
In C# projects, the modern direction is also to use JavaScript execution rather than older WebStorage properties. A typical pattern is:
driver.ExecuteScript("window.localStorage.clear();");
If you need to verify that storage is available first, you can check for it before clearing, but in most browser-based tests this is usually unnecessary.
Quick decision guide
- Use JavaScript execution if you want the most current, broadly supported solution.
- Use WebStorage APIs only if you are maintaining older code and know your driver supports them.
- Clear in setup hooks when the goal is test isolation across a suite.
Example workflow
A common pattern for a login or account-settings suite looks like this:
- Open the browser.
- Navigate to the application.
- Clear
localStorageand optionallysessionStorage. - Run the tests that depend on a clean state.
This avoids stale tokens, cached preferences, and unexpected redirects.
Related reading
If you are working on broader UI test stability, these guides may also help:
- Effective strategies for handling state changes in automated tests
- Ensuring consistent and reliable Selenium tests
- Best practices for managing test data in automated testing
Where Repeato fits in
If you are automating iOS, Android, or web app tests, Repeato can help you keep test setup fast and maintainable. It supports no-code test creation with a recorder, uses computer vision and AI, and lets you automate setup steps and complex flows with scripts or JavaScript when needed. That makes it a practical choice when your tests need a quick reset of app state before execution.