How to Bypass Login in Tests Using Stored Sessions
This guide explains how to bypass the login step in your tests by storing a logged-in session once and reusing it across multiple tests. This method reduces test runtime and eliminates the need for repeated logins during automation runs.
Overview
When you run tests that require authentication, you can store a logged-in session from one test and reuse it in others. You’ll record a dedicated session store test that logs in, saves the session, and then use that session in any other test through the reset browser session step.
Step 1: Create a Session Store Test
- Record a new test and name it, for example,
store_session_login_user1. - Perform a standard login using your credentials.
- If your website has a Remember Me checkbox, select it. This keeps the session persistent across multiple runs.
- Complete any two-factor authentication (2FA or OTP) if required.
- Add a verification step after login to confirm that you’ve signed in successfully.
- Add a session store step:
- Select Add Step > Existing.
- Search for session.
- Choose Store browser session.
- Name the session, for example:
user1.
- Add one more verification step after the store step. This ensures that the test doesn’t end before the session is saved.
- Save the test as:
When Store browser session "<filePath>"
Your session store test should include:
- Login
- Post-login verification
- Store session step
- Final verification step
gherkin
Scenario Outline: Store session for user
Given The user logins with username "<user_email>" and password "<user_password>"
When The user verifies their identity using verification code "<totp_code>"
Then Verify the text "Seller Home" can be found in the page
When Store browser session "<filePath>"
Then Verify the text "Seller Home" can be found in the page
Examples:
| user_email | user_password | totp_code | filePath |
| sapnesh.naik@blinq.io | pass | {{TOTP}} | user1 |Step 2: Run the Session Store Test
- Run the test at least once to generate the session file.
- After a successful run, a file (for example,
user1.json) is created in your repository. - This file contains all session and cookie information needed for future logins.
Tip: You only need to rerun this test when your session expires or when you’re running tests in a new environment (such as a new machine or CI/CD runner).
Step 3: Use the Stored Session in Other Tests
You can load the stored session in two ways:
Option A: Edit Gherkin Directly
Replace your login and 2FA steps with the following:
When Reset browser session "user1"Option B: Use the Recorder
- Open the test in Record Mode.
- Add a step above your existing login step:
- Select Add Step > Existing.
- Search for session.
- Choose Reset browser session "user1".
- Remove the original login/2FA steps.
- Save the test. Your test will now start in a logged-in state without performing the login steps manually.
Step 4: Use the Session in New Tests
When recording a new test:
Add this step as the first action:
Reset browser session "user1".Continue recording the test as usual.
This allows all new tests to reuse the stored session seamlessly.
Step 5: Best Practices and Rules
| Rule | Description |
|---|---|
| Do not log out | Any test using a stored session must not perform a logout action. Logging out will invalidate the session file. |
| Do not log out in the store session test | The session store test should only log in, verify, and store the session. |
| Refresh sessions as needed | If the bypass login fails after a few days, rerun the session store test to refresh the session file. |
| Run in CI/CD correctly | In cloud or CI/CD runs, always execute the store session test first so that other tests can reuse the latest session file. |
Step 6: Session Step Reference
| Step Type | Gherkin Command | Description |
|---|---|---|
| Store session | When Store browser session "param" | Saves the logged-in browser session. |
| Reset session | When Reset browser session "param" | Loads the saved session and restores the login state. |
Troubleshooting
| Issue | Possible Cause | Solution |
|---|---|---|
| Test prompts for login again | Session file missing or expired | Rerun the session store test to regenerate the file. |
| “Remember Me” not persistent | Checkbox was not selected | Ensure “Remember Me” is enabled when recording the store session test. |
| Other tests fail unexpectedly | A test logged out during execution | Remove any logout steps from session-based tests. |
