With the Persist browser state after flow execution setting on a login flow, one test in a cloud plan run signs in and the tests that follow start from that session instead of signing in again. This article explains how to set up a login flow to share its session with the rest of a plan run.
Sharing a session helps when many tests in a plan sign in as the same user, when the login is slow, or when it depends on a one-time code that you'd rather request once per run than once per test. Setting it up takes three steps:
See limitations for where the setting does and doesn't apply.
Turn on the setting
In the mabl Trainer or in quick edit mode, open a test that contains the login flow and take the following steps:
- Click on the pencil icon to edit the flow.
- On the Basics tab, turn on Persist browser state after flow execution.
- Click OK.
The setting applies to every instance of the flow, so you don't need to change the tests that call it.
You can also hand the whole setup to the mabl agent. Ask it to make the login flow persist its browser state, and it turns the setting on and wraps the login steps in a conditional for you. Login flows that the agent builds from scratch come with both already in place.
Wrap the flow's login steps in a conditional
The flow runs in every test, even when the browser already carries the session. A login flow that always types a username and password breaks in that case, because the login page isn't there. Wrap the login steps in a conditional that only holds when the login page is showing:
- Add an IF step that checks for an element that only exists on the login page, such as the Sign in button.
- Move the steps that enter the credentials and submit the form inside the IF.
- After the conditional, add an assertion for something that proves the user is signed in, such as the account menu.
Keep the assertion after the conditional rather than inside it, so it runs whether the flow signed in or found the session already in place. When you're done, save the test.
Run the login before the rest of the plan
Session data is only stored after the first test finishes the login flow, so tests that start at the same time each sign in for themselves. To sign in once for the whole run, give the login its own plan stage:
- First stage - Place a test that runs the login flow here to capture and store the authenticated session.
- Later stages - Place the tests that use the same login flow and credentials in later stages.
When tests in later stages reach the login flow, mabl automatically applies the session data, the conditional step in the flow evaluates to false, and the test continues from a logged-in state.
The stored session includes cookies, local storage, and IndexedDB. If storing or loading the session fails for any reason, the flow runs as it always has, and the test isn't affected.
Limitations
- Only browser flows can save session details.
- Shared session details are only available in plan runs in the mabl cloud.
- Tests must use the same application and credentials to load the saved state.
- Looping flows are excluded. Turning the setting on for a loop has no effect.