Sometimes your tests need to set specific cookies before navigating to a page or during test execution. This is useful for scenarios like:
  • Configuring user preferences (theme, language, etc.)
  • Bypassing certain flows by pre-setting state cookies
  • Testing cookie-dependent functionality

Adding Cookies with Custom Code

You can add cookies to your test using the β€œAdd Code” feature in the no-code editor. In the code block, you can use the standard Playwright context.addCookies() method. Use the standard Playwright context.addCookies() method to add one or more cookies:
// Add cookies to the context
await context.addCookies([
  {
    name: 'sessionId',
    value: 'abc123',
    domain: 'example.com',
    path: '/'
  },
  {
    name: 'theme',
    value: 'dark',
    domain: 'example.com',
    path: '/',
    expires: Math.floor(Date.now() / 1000) + 86400 // expires in 1 day
  }
]);
When adding cookies, you can specify the following properties:
PropertyTypeRequiredDescription
namestringβœ…The name of the cookie
valuestringβœ…The value of the cookie
domainstringβœ…The domain for which the cookie is valid
pathstring❌The path for which the cookie is valid (defaults to ’/β€˜)
expiresnumber❌Expiration time as Unix timestamp in seconds
httpOnlyboolean❌Whether the cookie is HTTP-only
secureboolean❌Whether the cookie requires HTTPS
sameSitestring❌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