Simulating session timeout during testing is crucial to ensure that your application handles session expiration gracefully. Here’s how you can simulate session timeout in different testing scenarios:
1. Manual Testing
Steps:
Log in to the application.
Note the session timeout duration (e.g., 30 minutes).
Wait for the session to expire (or manually invalidate the session if possible).
Attempt to perform an action that requires an active session (e.g., access a protected page).
Verify that the application redirects to the login page or displays a session expiration message.
Tips:
Use browser developer tools to monitor cookies and session storage.
Manually delete the session cookie to force a session timeout.
2. Automated Testing (Web Applications)
Using Selenium or Similar Tools
Simulate Inactivity: Use threading or timers to pause the test execution for the session timeout duration.
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://your-app.com/login")
# Perform login
# Simulate inactivity
time.sleep(1800) # Wait for 30 minutes
# Attempt to access a protected page
driver.get("https://your-app.com/dashboard")
# Verify redirection to login page
Manually Invalidate Session:
Delete the session cookie programmatically.
driver.delete_cookie('session_id')
Refresh the page and verify the behavior.
Using Test Frameworks (e.g., Cypress, Playwright)
Cypress Example:
it('should handle session timeout', () => {
cy.login(); // Custom command to log in
cy.wait(1800000); // Wait for 30 minutes
cy.visit('/dashboard');
cy.url().should('include', '/login'); // Verify redirection
});