How to manipulate cookies in browser

Hello,

I currently have a test structured to log in and it works well:

export default async function () {
  const browser = chromium.launch({
    args: ["no-sandbox"],
    ignoreDefaultArgs: ["enable-automation"],
    headless: true,
    timeout: "60s", // Or whatever time you want to define
  });
  const context = browser.newContext();
  const page = context.newPage();

  try {
    await page.goto("https://somesite.com/login");
    page
      .locator('input[name="identifier"]')
      .fill("user@test.com");
    let submitButton = page.locator('input[type="submit"]');
    await submitButton.click();

    const passwordInput = page.locator('input[type="password"]');
    passwordInput.fill("pass");
    const submitPasswordButton = page.locator('input[type="submit"]');

    await Promise.all([page.waitForNavigation(), submitPasswordButton.click()]);

    ...
  } finally {
    ...
  }
}

However, if I increase the number of VUs that are used, our login limits how many times we can log in. To circumvent this, we have a backdoor way of authenticating with the http client and getting a cookie in the response. How can I apply this cookie to work on the browser or page? I can’t seem to find a way using the docs.

Another possible solution would be to do the login process pasted above in a setup method. The problem with this solution is that setup can only pass JSON to VU code so I am unable to pass the page or context to VU code. Is there a way to accomplish this?

Thank you for any help.

Hi @urmit,

Have you tried working with the browserContext.addCookies API?

Cheers,
Ankur