Basic Cookie Addition
Use the standard Playwrightcontext.addCookies()
method to add one or more cookies:
Cookie Properties
When adding cookies, you can specify the following properties:Property | Type | Required | Description |
---|---|---|---|
name | string | ✅ | The name of the cookie |
value | string | ✅ | The value of the cookie |
domain | string | ✅ | The domain for which the cookie is valid |
path | string | ❌ | The path for which the cookie is valid (defaults to ’/‘) |
expires | number | ❌ | Expiration time as Unix timestamp in seconds |
httpOnly | boolean | ❌ | Whether the cookie is HTTP-only |
secure | boolean | ❌ | Whether the cookie requires HTTPS |
sameSite | string | ❌ | SameSite attribute (‘Strict’, ‘Lax’, or ‘None’) |
Best Practices
- Add cookies before navigation: Set cookies before navigating to pages that depend on them
- Match the domain: Ensure the cookie domain matches the domain you’re testing
- Use console.log(): Debug cookie issues using console logging and the VM’s browser console
- Set appropriate expiration: Use reasonable expiration times for test cookies