Class SHAFT.Auth

java.lang.Object
com.shaft.driver.SHAFT.Auth
Enclosing class:
SHAFT

public static class SHAFT.Auth extends Object
Playwright-style cached-authentication helper: runs a login 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 Details

    • stateFile

      public static String stateFile(String name)
      Returns the cache file for name, whether or not it currently exists. Feed the result to Web.SetProperty.storageStatePath(String) or BrowserActions.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

      public static String setup(String name, Consumer<SHAFT.GUI.WebDriver> flow)
      Runs flow once per name and 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, runs flow against it, saves its storage state to stateFile(String), and quits the driver — even if flow throws. On a cache hit, flow is 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 fresh SHAFT.GUI.WebDriver on a cache miss
      Returns:
      the path of the (now guaranteed to exist) storage-state cache file for name; same value as stateFile(String)