Cookies not working

I am trying to inject a cookie to access a web app and I am having 2 issues:

  1. I am literally following this example addCookies() (k6.io), just replacing the values for mine (when the expires field exist I am replacing it for the afterDay value in the example) but for some reason when I am trying to add the cookies to the browser I get this error :
ERRO[0001] Uncaught (in promise) GoError: adding cookies: unable to export cookies value to cookieParams. could not convert array element [object Object] to []network.CookieParam at 2: could not convert struct value 1697158743 to *cdp.TimeSinceEpoch for field Expires: could not convert 1697158743 to cdp.TimeSinceEpoch
ui     [at github.com/grafana/xk6-browser/api.BrowserContext.AddCookies-fm (native)

I also tried with the same values we use in a manual workflow in the app but I get the same result.
2. If I delete the expires field from all my cookies there is no error but once the browser starts I cannot see any cookie in there.
Is the context.addCookies() function working?

Hi @neurohypophysis,

Welcome to the forum!

Could you share with us the script snippet? Please also tell us about the k6 version you’re using.

Thanks.

1 Like

Hi, thank you for the answer, I am using “@types/k6”: “~0.47.0”

import { browser } from 'k6/experimental/browser';

export const options = {
  scenarios: {
    browser: {
      executor: 'shared-iterations',
      options: {
        browser: {
            type: 'chromium',
        },
      },
    },
  },
}

export default async function () {
  const context = browser.newContext();
  const page = context.newPage();

  try {
    const unixTimeSinceEpoch = Math.round(new Date() / 1000);
    const day = 60*60*24;
    const dayAfter = unixTimeSinceEpoch+day;
    const dayBefore = unixTimeSinceEpoch-day;

    context.addCookies([
      // this cookie expires at the end of the session
      {
        name: 'testcookie',
        value: '1',
        sameSite: 'Strict',
        domain: 'httpbin.org',
        path: '/',
        httpOnly: true,
        secure: true,
      },
      // this cookie expires in a day
      {
        name: 'testcookie2',
        value: '2',
        sameSite: 'Lax',
        domain: 'httpbin.org',
        path: '/',
        expires: dayAfter,
      },
      // this cookie expires in the past, so it will be removed.
      {
        name: 'testcookie3',
        value: '3',
        sameSite: 'Lax',
        domain: 'httpbin.org',
        path: '/',
        expires: dayBefore
      }
    ]);
    page.waitForTimeout(55000);
  } finally {
    page.close();
  }
}

I am using the timeout to manually check for the cookie

Hi,

I’ve tried the script, and it works on my side:

# note that I've added a console.log(context.cookies()) to
# print the cookies after adding them.
$ k6 run -q script.js
...
INFO[0000] [{"name":"testcookie","value":"1","domain":"httpbin.org","path":"/","httpOnly":true,"secure":true,"sameSite":"Strict","url":"","expires":-1},{"name":"testcookie2","value":"2","domain":"httpbin.org","path":"/","httpOnly":false,"secure":false,"sameSite":"Lax","url":"","expires":1697274154}]  source=console
...

Could you please try updating your k6 command to the latest version? Please go to this link and download the binary specific to your operating system. You need to scroll down to the bottom to see the binary links.

Hope this helps.

I got installed the latest version using winget but looks like that latest is not the same latest in your link . I just uninstalled my winget version and installed the binary from the link and it worked. Thank you.

1 Like