Search code examples
javascriptpuppeteere2e-testingplaywright

Add cookies in playwright test


No matter what I do I get an error (either X.cookies is not a function or X.addCookies is not a function). I tried with context, page.context. browserContext etc. and it always ends up in the same way (ok, page.context as well as browserContext are undefined so error is different).

Context:

  • Playwright Version: 1.4.2
  • Operating System: Ubuntu 20.4
  • Node.js version: 10.15.1
  • Browser: Chromium

Code:

beforeEach(async function fn() {
this.timeout(20000);
browser = await chromium.launch({ headless: false });

const context = await browser.newContext();
page = await context.newPage();

await page
  .goto("http://localhost:4200/#/login", {
    waitUntil: "networkidle0",
  })
  .catch(() => {});

});

and in test:

      // await context.addCookies([
  //   { name: "csrftoken", value: cookieToken, path: "/" },
  //   { name: "sessionid", value: cookieSession, path: "/" },
  // ]);
  // await context.cookies();

Solution

  • right after you have your context, you should be able to use it as an example below: await context.addCookies([{name:"csrftoken", value: "mytokenvalue123", url: "your.application.url"}]);