Playwright provides powerful cookie and session management through browser contexts. Each context maintains isolated cookies, localStorage, and session data, making it perfect for testing authentication flows and managing user sessions.
Getting Cookies
Retrieve all cookies for the current context or specific URLs:
Python:
import time
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
context = browser.new_context()
page = context.new_page()
page.goto("https://example.com")
# Get all cookies
all_cookies = context.cookies()
print("All cookies:", all_cookies)
# Get cookies for specific URL
url_cookies = context.cookies("https://example.com")
print("URL-specific cookies:", url_cookies)
browser.close()
JavaScript:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://example.com');
// Get all cookies
const allCookies = await context.cookies();
console.log('All cookies:', allCookies);
// Get cookies for specific URL
const urlCookies = await context.cookies('https://example.com');
console.log('URL-specific cookies:', urlCookies);
await browser.close();
})();
Setting Cookies
Add cookies before or after navigation:
Python:
import time
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
context = browser.new_context()
page = context.new_page()
# Set cookies before navigation
context.add_cookies([
{
"name": "session_id",
"value": "abc123xyz",
"domain": "example.com",
"path": "/",
"expires": int(time.time()) + 3600, # 1 hour from now
"httpOnly": True,
"secure": True
},
{
"name": "user_pref",
"value": "dark_theme",
"domain": "example.com",
"path": "/"
}
])
page.goto("https://example.com")
browser.close()
JavaScript:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
// Set cookies before navigation
await context.addCookies([
{
name: 'session_id',
value: 'abc123xyz',
domain: 'example.com',
path: '/',
expires: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
httpOnly: true,
secure: true
},
{
name: 'user_pref',
value: 'dark_theme',
domain: 'example.com',
path: '/'
}
]);
await page.goto('https://example.com');
await browser.close();
})();
Clearing Cookies
Remove specific cookies or clear all cookies:
Python:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
context = browser.new_context()
page = context.new_page()
page.goto("https://example.com")
# Clear specific cookies
context.clear_cookies(name="session_id")
# Clear all cookies for a domain
context.clear_cookies(domain="example.com")
# Clear all cookies
context.clear_cookies()
browser.close()
JavaScript:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://example.com');
// Clear specific cookies
await context.clearCookies({ name: 'session_id' });
// Clear all cookies for a domain
await context.clearCookies({ domain: 'example.com' });
// Clear all cookies
await context.clearCookies();
await browser.close();
})();
Session Management with Storage State
Preserve and reuse sessions across browser instances using storage state:
Python:
from playwright.sync_api import sync_playwright
def login_and_save_session():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
# Perform login
page.goto("https://example.com/login")
page.fill("#username", "your_username")
page.fill("#password", "your_password")
page.click("#login-button")
page.wait_for_url("**/dashboard")
# Save session state
context.storage_state(path="session.json")
browser.close()
def reuse_session():
with sync_playwright() as p:
browser = p.chromium.launch()
# Load previous session
context = browser.new_context(storage_state="session.json")
page = context.new_page()
# Navigate directly to protected page
page.goto("https://example.com/dashboard")
# User is already logged in!
browser.close()
# First run: login and save session
login_and_save_session()
# Subsequent runs: reuse saved session
reuse_session()
JavaScript:
const { chromium } = require('playwright');
async function loginAndSaveSession() {
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext();
const page = await context.newPage();
// Perform login
await page.goto('https://example.com/login');
await page.fill('#username', 'your_username');
await page.fill('#password', 'your_password');
await page.click('#login-button');
await page.waitForURL('**/dashboard');
// Save session state
await context.storageState({ path: 'session.json' });
await browser.close();
}
async function reuseSession() {
const browser = await chromium.launch();
// Load previous session
const context = await browser.newContext({ storageState: 'session.json' });
const page = await context.newPage();
// Navigate directly to protected page
await page.goto('https://example.com/dashboard');
// User is already logged in!
await browser.close();
}
// First run: login and save session
await loginAndSaveSession();
// Subsequent runs: reuse saved session
await reuseSession();
Context Isolation
Each browser context maintains completely isolated sessions:
Python:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
# Context 1: User A session
context1 = browser.new_context()
page1 = context1.new_page()
context1.add_cookies([{
"name": "user_id",
"value": "user_a",
"domain": "example.com",
"path": "/"
}])
# Context 2: User B session (completely isolated)
context2 = browser.new_context()
page2 = context2.new_page()
context2.add_cookies([{
"name": "user_id",
"value": "user_b",
"domain": "example.com",
"path": "/"
}])
# Both contexts can run simultaneously without interference
page1.goto("https://example.com/profile") # Sees user_a data
page2.goto("https://example.com/profile") # Sees user_b data
context1.close()
context2.close()
browser.close()
Authentication Testing
Test login flows and authenticated states:
Python:
from playwright.sync_api import sync_playwright
def test_authentication():
with sync_playwright() as p:
browser = p.chromium.launch()
# Test unauthenticated access
context = browser.new_context()
page = context.new_page()
page.goto("https://example.com/protected")
assert page.url == "https://example.com/login" # Redirected to login
# Login and verify session
page.fill("#username", "testuser")
page.fill("#password", "testpass")
page.click("#login-button")
page.wait_for_url("**/dashboard")
# Verify authentication cookies are set
cookies = context.cookies()
session_cookie = next((c for c in cookies if c['name'] == 'session_token'), None)
assert session_cookie is not None
# Test authenticated access
page.goto("https://example.com/protected")
assert "Protected Content" in page.text_content("body")
browser.close()
test_authentication()
Key Benefits
- Context Isolation: Each context maintains separate cookies and session data
- Session Persistence: Save and restore complete browser state including cookies, localStorage, and sessionStorage
- Authentication Testing: Test login flows and maintain authenticated sessions
- Multi-user Testing: Simulate multiple users simultaneously with different contexts
- State Management: Preserve session state across test runs for faster execution
Playwright's context-based cookie and session management provides robust isolation and state persistence, making it ideal for comprehensive web automation and testing scenarios.