{"id":7,"date":"2015-06-30T12:36:01","date_gmt":"2015-06-30T12:36:01","guid":{"rendered":"http:\/\/loadfocus.com\/blog\/tech\/?p=7"},"modified":"2022-03-04T11:48:57","modified_gmt":"2022-03-04T11:48:57","slug":"how-to-clear-local-storage-using-selenium-webdriver","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/tech\/2015\/06\/how-to-clear-local-storage-using-selenium-webdriver","title":{"rendered":"How to Clear Local Storage using Selenium WebDriver?"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><!-- pn-tldr --><h2>Key takeaways<\/h2><ul><li>Clearing cookies does not clear local storage, so state survives between tests.<\/li><li>Execute localStorage.clear() through the driver&#x27;s JavaScript executor.<\/li><li>Do it in setup rather than teardown, so a crashed test cannot leak state into the next.<\/li><\/ul><!-- \/pn-tldr -->\n<p class=\"lead\">There are two options to interact with browser&#8217;s local storage:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>via javascript by creating your own methods<\/li><li>via Selenium WebDriver (starting with version 2.42) if the driver supports it<\/li><\/ul>\n\n\n\n<p>Let&#8217;s have a look of how can we use these two options in our Selenium WebDriver tests:<\/p>\n\n\n\n<p>1. Let&#8217;s create a LocalStorage class with methods for each action we need for interaction with the browser&#8217;s Local Storage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java line-numbers\"><code>import org.openqa.selenium.JavascriptExecutor;\nimport org.openqa.selenium.WebDriver;\n\npublic class LocalStorageJS {\n\n    private JavascriptExecutor js;\n\n    public LocalStorageJS(WebDriver webDriver) {\n        this.js = (JavascriptExecutor) webDriver;\n    }\n\n    public void removeItemFromLocalStorage(String item) {\n        js.executeScript(String.<em>format<\/em>(\"window.localStorage.removeItem('%s');\", item));\n    }\n\n    public boolean isItemPresentInLocalStorage(String item) {\n        return !(js.executeScript(String.<em>format<\/em>(\"return window.localStorage.getItem('%s');\", item)) == null);\n    }\n\n    public String getItemFromLocalStorage(String key) {\n        return (String) js.executeScript(String.<em>format<\/em>(\"return window.localStorage.getItem('%s');\", key));\n    }\n\n    public String getKeyFromLocalStorage(int key) {\n        return (String) js.executeScript(String.<em>format<\/em>(\"return window.localStorage.key('%s');\", key));\n    }\n\n    public Long getLocalStorageLength() {\n        return (Long) js.executeScript(\"return window.localStorage.length;\");\n    }\n\n    public void setItemInLocalStorage(String item, String value) {\n        js.executeScript(String.<em>format<\/em>(\"window.localStorage.setItem('%s','%s');\", item, value));\n    }\n\n    public void clearLocalStorage() {\n        js.executeScript(String.<em>format<\/em>(\"window.localStorage.clear();\"));\n    }\n}<\/code><\/code><\/pre>\n\n\n\n<p>2. Using the Selenium WebDriver built in functionality present in the HTML5 package<\/p>\n\n\n\n<p><br> <\/p>\n\n\n<p>[emaillocker]<br \/>\n(org\/seleniumhq\/selenium\/selenium-api\/2.45.0\/selenium-api-2.45.0.jar!\/org\/openqa\/selenium\/html5)<br \/>\n<strong>Clearing LocalStorage:<\/strong><\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nLocalStorage local = ((WebStorage) driver).getLocalStorage();\nlocal.clear();\n<\/code><\/pre>\n<p><strong>Setting an item in LocalStorage:<\/strong><\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nlocal.setItem(\"FOO\", \"BAR\");\n<\/code><\/pre>\n<p><strong>Getting an item from LocalStorage:<\/strong><\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nlocal.getItem(\u201cname\u201d)\n<\/code><\/pre>\n<p>Here you can find more details about <a href=\"https:\/\/loadfocus.com\/blog\/blog\/2013\/09\/05\/how-to-locate-web-elements-with-selenium-webdriver\/\">locating elements with Selenium WebDriver<\/a>.<br \/>\n[\/emaillocker]<\/p>\n\n\n\n<p><\/p>\n<!-- pn-faq --><h2>Frequently Asked Questions<\/h2><h3>Does clearing cookies also clear local storage?<\/h3><p>No. They are separate stores, so state written to local storage survives a cookie reset and leaks into the next test. This is a common cause of tests that only fail when run as part of a suite.<\/p><h3>How do I clear local storage in Selenium?<\/h3><p>Through the JavaScript executor: cast the driver to JavascriptExecutor and run window.localStorage.clear(). Selenium has no dedicated API for it on most drivers, so the JavaScript route is the portable one.<\/p><h3>Should I clear it in setup or teardown?<\/h3><p>Setup. A test that crashes never reaches its teardown, so cleaning up beforehand is the only version that holds when something goes wrong.<\/p><!-- \/pn-faq --><!-- pn-related-reading --><h2>Related reading<\/h2><ul><li><a href=\"https:\/\/loadfocus.com\/blog\/2014\/06\/how-to-take-snapshots-of-the-browser-using-selenium-webdriver\">How to take screenshots with Selenium WebDriver<\/a><\/li><li><a href=\"https:\/\/loadfocus.com\/blog\/tech\/2015\/07\/how-to-get-all-the-options-in-a-select-tag-using-selenium-webdriver\">Get All Select Options in Selenium WebDriver<\/a><\/li><li><a href=\"https:\/\/loadfocus.com\/blog\/tech\/2016\/05\/how-to-scroll-web-page-up-or-down-using-selenium-webdriver\">How to scroll web page UP or Down using Selenium WebDriver?<\/a><\/li><\/ul>","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>Key takeaways Clearing cookies does not clear local storage, so state survives between tests. Execute localStorage.clear() through the driver&#x27;s JavaScript executor. Do it in setup rather than teardown, so a crashed test cannot leak state into the next. There are two options to interact with browser&#8217;s local storage: via javascript by creating your own methods&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/tech\/2015\/06\/how-to-clear-local-storage-using-selenium-webdriver\" class=\"more-link\" title=\"Read How to Clear Local Storage using Selenium WebDriver?\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":495,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ui-testing"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":13,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":400,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/7\/revisions\/400"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media\/495"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}