Class SHAFT.Auth
- Enclosing class:
SHAFT
flow once per name
and caches the resulting browser storage state (cookies + localStorage) to disk so that
subsequent calls — including from other tests or threads — can reuse the same
authenticated session instead of repeating the UI login.
The cache is a JSON file per name, written in the same schema as
BrowserActions.saveStorageState(String), stored under
Paths.authCache() (default target/auth-cache/).
Staleness policy: deliberately simple — the cache is considered valid as long as
the file exists; there is no TTL/max-age check. This keeps the feature minimal while still
solving the common case (run the login once per suite). To force a re-login, delete the file
returned by stateFile(String) or call setup with a different name.
Thread-safety: test suites frequently run in parallel, so concurrent setup calls
for the same name are serialized on a per-name lock — only the first caller
actually runs flow; every other concurrent caller blocks until it finishes and then sees
the now-cached file. Calls for different names never block each other.
Usage example:
// Once per suite (e.g. in a @BeforeSuite), run the login flow and cache its storage state:
SHAFT.Auth.setup("standardUser", driver -> {
driver.browser().navigateToURL("https://example.com/login");
driver.element().type(By.id("username"), "standard_user");
driver.element().type(By.id("password"), "secret_sauce");
driver.element().click(By.id("login-button"));
});
// In every test, reuse the cached session instead of logging in again:
SHAFT.Properties.web.set().storageStatePath(SHAFT.Auth.stateFile("standardUser"));
SHAFT.GUI.WebDriver driver = new SHAFT.GUI.WebDriver();
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringsetup(String name, Consumer<SHAFT.GUI.WebDriver> flow) Runsflowonce pernameand caches its resulting storage state, or reuses an already-cached storage state on subsequent calls.static StringReturns the cache file forname, whether or not it currently exists.
-
Method Details
-
stateFile
Returns the cache file forname, whether or not it currently exists. Feed the result toWeb.SetProperty.storageStatePath(String)orBrowserActions.loadStorageState(String)to reuse a cached session.- Parameters:
name- the auth-cache name (e.g. a user role such as"adminUser")- Returns:
- the absolute-or-relative path of the JSON cache file for
name
-
setup
Runsflowonce pernameand caches its resulting storage state, or reuses an already-cached storage state on subsequent calls.On a cache miss, this creates a new
SHAFT.GUI.WebDriver, runsflowagainst it, saves its storage state tostateFile(String), and quits the driver — even ifflowthrows. On a cache hit,flowis not invoked at all.- Parameters:
name- the auth-cache name to set up (e.g. a user role such as"standardUser")flow- the login flow to run against a freshSHAFT.GUI.WebDriveron a cache miss- Returns:
- the path of the (now guaranteed to exist) storage-state cache file for
name; same value asstateFile(String)
-