Last active
April 4, 2025 14:01
-
-
Save matthias-dirickx/d51784a6da0090f425cb73f28824cd87 to your computer and use it in GitHub Desktop.
Local storage manager after the example as found in https://gist.github.com/roydekleijn/5073579
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenQA.Selenium; | |
//Did not yet test this code | |
//I'll update if needed (and if I think of it), but this is the gist of it (pun intended) | |
namespace LocalStorageManager | |
{ | |
class LocalStorageManager | |
{ | |
private IJavaScriptExecutor js; | |
public LocalStorageManager(IWebDriver driver) | |
{ | |
this.js = (IJavaScriptExecutor)driver; | |
} | |
public void RemoveItemFromLocalStorage(string item) | |
{ | |
js.ExecuteScript( | |
$"window.localStorage.removeItem'{item}');" | |
); | |
} | |
public bool IsItemPresentInLocalStorage(string item) | |
{ | |
return !(js.ExecuteScript( | |
$"return window.localStorage.getItem('{item}');") == null); | |
} | |
public string GetItemFromLocalStorage(string key) | |
{ | |
return (string)js.ExecuteScript( | |
$"return window.localStorage.key('{key}')"); | |
} | |
public string GetKeyFromLocalStorage(int key) | |
{ | |
return (string)js.ExecuteScript( | |
$"return window.localStorage.key('{key}')"); | |
} | |
public long GetLocalStorageLength() | |
{ | |
return (long)js.ExecuteScript( | |
"return window.localStorage.length;"); | |
} | |
public void SetItemInLocalStorage(string item, string value) | |
{ | |
js.ExecuteScript( | |
$"window.localStorage.setItem('{item}', '{value}');"); | |
} | |
public void ClearLocalStorage() | |
{ | |
js.ExecuteScript( | |
"window.localStorage.clear();"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment