java.lang.Object
net.serenitybdd.screenplay.playwright.interactions.SaveSessionState
All Implemented Interfaces:
net.serenitybdd.core.SkipNested, net.serenitybdd.screenplay.Performable

public class SaveSessionState extends Object implements net.serenitybdd.screenplay.Performable
Save the current browser session state (cookies, localStorage, sessionStorage) to a file. This allows reusing authentication state across tests, significantly speeding up test suites.

The saved state includes:

  • All cookies from the browser context
  • localStorage data for all origins
  • sessionStorage data for all origins

Sample usage:

     // After logging in, save the session state
     actor.attemptsTo(
         Navigate.to(loginPage),
         Enter.theValue("user@example.com").into("#email"),
         Enter.theValue("password").into("#password"),
         Click.on("#login-btn")
     );

     // Save the authenticated state to a file
     actor.attemptsTo(SaveSessionState.toFile("auth-state.json"));

     // In subsequent tests, restore the state to skip login
     actor.attemptsTo(RestoreSessionState.fromFile("auth-state.json"));
 
See Also:
  • Method Details

    • toFile

      public static SaveSessionState toFile(String name)
      Save session state to a file with the given name in the default directory. The file will be saved as: target/playwright/session-state/{name}.json
      Parameters:
      name - The name for the state file (without extension)
    • toPath

      public static SaveSessionState toPath(Path path)
      Save session state to the specified path.
      Parameters:
      path - The full path for the state file
    • toPath

      public static SaveSessionState toPath(String path)
      Save session state to the specified path.
      Parameters:
      path - The full path for the state file
    • performAs

      public <T extends net.serenitybdd.screenplay.Actor> void performAs(T actor)
      Specified by:
      performAs in interface net.serenitybdd.screenplay.Performable
    • getOutputPath

      public Path getOutputPath()
      Get the path where the session state will be saved.