Skip to content

Instantly share code, notes, and snippets.

@matthias-dirickx
Last active November 20, 2023 03:14
Show Gist options
  • Save matthias-dirickx/d51784a6da0090f425cb73f28824cd87 to your computer and use it in GitHub Desktop.
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
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