{
  "doc": {
    "id": "api/commands/getcookie",
    "title": "cy.getCookie()",
    "description": "Get a browser cookie by its name in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/getcookie.md",
    "version": "29f95bf8bb06f320986f3749f5bf09a35a409eab",
    "updated_at": "2026-09-04T10:49:54.630Z",
    "headings": [
      {
        "id": "api/commands/getcookie#getcookie",
        "text": "getCookie",
        "level": 1
      },
      {
        "id": "api/commands/getcookie#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/getcookie#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/getcookie#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/getcookie#yields",
        "text": "Yields",
        "level": 3
      },
      {
        "id": "api/commands/getcookie#when-a-cookie-matching-the-name-could-not-be-found",
        "text": "When a cookie matching the name could not be found:",
        "level": 4
      },
      {
        "id": "api/commands/getcookie#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/getcookie#session-id",
        "text": "Session id",
        "level": 3
      },
      {
        "id": "api/commands/getcookie#get-session-id-cookie-after-logging-in",
        "text": "Get session_id cookie after logging in",
        "level": 4
      },
      {
        "id": "api/commands/getcookie#using-cy-getcookie-to-test-logging-in",
        "text": "Using cy.getCookie() to test logging in",
        "level": 4
      },
      {
        "id": "api/commands/getcookie#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/getcookie#requirements",
        "text": "Requirements",
        "level": 3
      },
      {
        "id": "api/commands/getcookie#assertions",
        "text": "Assertions",
        "level": 3
      },
      {
        "id": "api/commands/getcookie#timeouts",
        "text": "Timeouts",
        "level": 3
      },
      {
        "id": "api/commands/getcookie#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/getcookie#history",
        "text": "History",
        "level": 2
      },
      {
        "id": "api/commands/getcookie#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/commands/getcookie#syntax",
      "doc_id": "api/commands/getcookie",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n```\ncy.getCookie(name)\ncy.getCookie(name, options)\n```\n\n### Usage\n\n**Correct Usage**\n\n```\ncy.getCookie('auth_key') // Get cookie with name 'auth_key'\n```\n\n### Arguments\n\n**name _(String)_**\n\nThe name of the cookie to get. Required.\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `cy.getCookie()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\n| `domain` | Hostname of the current URL | Retrieves the cookie from the specified domain |\n| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |\n| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `cy.getCookie()` to resolve before [timing out](#Timeouts) |\n\n### Yields\n\n`cy.getCookie()` yields a cookie object with the following properties:\n\n*   `domain`\n*   `expiry` _(if specified)_\n*   `hostOnly` _(if specified)_\n*   `httpOnly`\n*   `name`\n*   `path`\n*   `sameSite` _(if specified)_\n*   `secure`\n*   `value`\n\n`cy.getCookie()` is a query, and it is _safe_ to chain further commands. It will automatically [retry](/llm/markdown/app/core-concepts/retry-ability.md) until all chained assertions have passed, or until it times out, re-reading the cookie on each retry so the yielded value stays up to date. Chained assertions are what drive those retries: with no assertions chained, `cy.getCookie()` yields whatever it reads on the first attempt, which is `null` if the cookie does not exist yet. Chain [`.should('exist')`](/llm/markdown/api/commands/should.md) to wait for a cookie that is set asynchronously.\n\nBecause `cy.getCookie()` is a query, it re-reads the cookie on every retry, and assertions you chain with [`.should()`](/llm/markdown/api/commands/should.md) drive those retries until they pass or the command times out. A [`.then()`](/llm/markdown/api/commands/then.md) callback, by contrast, runs only once after the command settles and is not retried — so prefer chaining [`.should()`](/llm/markdown/api/commands/should.md) assertions over asserting inside `.then()` when you need Cypress to wait for the cookie to be set.\n\n#### When a cookie matching the name could not be found:\n\nOn each read, `cy.getCookie()` yields the cookie object, or `null` if no cookie with that name exists yet. The yielded value is the subject your chained assertions run against. If an assertion fails, either because the cookie is `null`, or because it exists but a property doesn't match yet, the query re-reads the cookie and re-runs the assertion, looping until it passes or the command times out.\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 491
    },
    {
      "id": "api/commands/getcookie#arguments",
      "doc_id": "api/commands/getcookie",
      "heading": "Arguments",
      "heading_level": 3,
      "content_markdown": "### Arguments\n\n**name _(String)_**\n\nThe name of the cookie to get. Required.\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `cy.getCookie()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\n| `domain` | Hostname of the current URL | Retrieves the cookie from the specified domain |\n| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |\n| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `cy.getCookie()` to resolve before [timing out](#Timeouts) |\n",
      "section": "api",
      "anchors": [
        "arguments"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 115
    },
    {
      "id": "api/commands/getcookie#yields",
      "doc_id": "api/commands/getcookie",
      "heading": "Yields",
      "heading_level": 3,
      "content_markdown": "### Yields\n\n`cy.getCookie()` yields a cookie object with the following properties:\n\n*   `domain`\n*   `expiry` _(if specified)_\n*   `hostOnly` _(if specified)_\n*   `httpOnly`\n*   `name`\n*   `path`\n*   `sameSite` _(if specified)_\n*   `secure`\n*   `value`\n\n`cy.getCookie()` is a query, and it is _safe_ to chain further commands. It will automatically [retry](/llm/markdown/app/core-concepts/retry-ability.md) until all chained assertions have passed, or until it times out, re-reading the cookie on each retry so the yielded value stays up to date. Chained assertions are what drive those retries: with no assertions chained, `cy.getCookie()` yields whatever it reads on the first attempt, which is `null` if the cookie does not exist yet. Chain [`.should('exist')`](/llm/markdown/api/commands/should.md) to wait for a cookie that is set asynchronously.\n\nBecause `cy.getCookie()` is a query, it re-reads the cookie on every retry, and assertions you chain with [`.should()`](/llm/markdown/api/commands/should.md) drive those retries until they pass or the command times out. A [`.then()`](/llm/markdown/api/commands/then.md) callback, by contrast, runs only once after the command settles and is not retried — so prefer chaining [`.should()`](/llm/markdown/api/commands/should.md) assertions over asserting inside `.then()` when you need Cypress to wait for the cookie to be set.\n\n#### When a cookie matching the name could not be found:\n\nOn each read, `cy.getCookie()` yields the cookie object, or `null` if no cookie with that name exists yet. The yielded value is the subject your chained assertions run against. If an assertion fails, either because the cookie is `null`, or because it exists but a property doesn't match yet, the query re-reads the cookie and re-runs the assertion, looping until it passes or the command times out.\n",
      "section": "api",
      "anchors": [
        "yields"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 349
    },
    {
      "id": "api/commands/getcookie#when-a-cookie-matching-the-name-could-not-be-found",
      "doc_id": "api/commands/getcookie",
      "heading": "When a cookie matching the name could not be found:",
      "heading_level": 4,
      "content_markdown": "#### When a cookie matching the name could not be found:\n\nOn each read, `cy.getCookie()` yields the cookie object, or `null` if no cookie with that name exists yet. The yielded value is the subject your chained assertions run against. If an assertion fails, either because the cookie is `null`, or because it exists but a property doesn't match yet, the query re-reads the cookie and re-runs the assertion, looping until it passes or the command times out.\n",
      "section": "api",
      "anchors": [
        "when-a-cookie-matching-the-name-could-not-be-found"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 104
    },
    {
      "id": "api/commands/getcookie#examples",
      "doc_id": "api/commands/getcookie",
      "heading": "Examples",
      "heading_level": 2,
      "content_markdown": "## Examples\n\n### Session id\n\n#### Get `session_id` cookie after logging in\n\nIn this example, on first login, our server sends us back a session cookie.\n\n```\n// assume we just logged in\ncy.contains('Login').click()\ncy.url().should('include', 'profile')\n// retries until cookie with value=189jd09su\n// is found or default command timeout ends\ncy.getCookie('session_id')\n  .should('have.property', 'value', '189jd09su')\n  .then((cookie) => {\n    // cookie is an object with \"domain\", \"name\" and other properties\n  })\n```\n\nYou can check the cookie existence without comparing any of its properties\n\n```\ncy.getCookie('my-session-cookie').should('exist')\n```\n\nIf you need the cookie value, for example to use in a subsequent call\n\n```\nlet cookie\n\ncy.getCookie('session_id')\n  .should('exist')\n  .then((c) => {\n    // save cookie until we need it\n    cookie = c\n  })\n\n// some time later, force the \"cy.request\"\n// to run ONLY after the cookie has been set\n// by placing it inside \".then\"\ncy.get('#submit')\n  .click()\n  .then(() => {\n    cy.request({\n      url: '/api/admin',\n      headers: {\n        'my-token-x': cookie.value,\n      },\n    })\n  })\n```\n\n#### Using `cy.getCookie()` to test logging in\n\nCheck out our example recipes using `cy.getCookie()` to test [logging in using HTML web forms](/llm/markdown/app/references/recipes.md#Logging-In), [logging in using XHR web forms](/llm/markdown/app/references/recipes.md#Logging-In) and [logging in with single sign on](/llm/markdown/app/references/recipes.md#Logging-In)\n",
      "section": "api",
      "anchors": [
        "examples"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 256
    },
    {
      "id": "api/commands/getcookie#session-id",
      "doc_id": "api/commands/getcookie",
      "heading": "Session id",
      "heading_level": 3,
      "content_markdown": "### Session id\n\n#### Get `session_id` cookie after logging in\n\nIn this example, on first login, our server sends us back a session cookie.\n\n```\n// assume we just logged in\ncy.contains('Login').click()\ncy.url().should('include', 'profile')\n// retries until cookie with value=189jd09su\n// is found or default command timeout ends\ncy.getCookie('session_id')\n  .should('have.property', 'value', '189jd09su')\n  .then((cookie) => {\n    // cookie is an object with \"domain\", \"name\" and other properties\n  })\n```\n\nYou can check the cookie existence without comparing any of its properties\n\n```\ncy.getCookie('my-session-cookie').should('exist')\n```\n\nIf you need the cookie value, for example to use in a subsequent call\n\n```\nlet cookie\n\ncy.getCookie('session_id')\n  .should('exist')\n  .then((c) => {\n    // save cookie until we need it\n    cookie = c\n  })\n\n// some time later, force the \"cy.request\"\n// to run ONLY after the cookie has been set\n// by placing it inside \".then\"\ncy.get('#submit')\n  .click()\n  .then(() => {\n    cy.request({\n      url: '/api/admin',\n      headers: {\n        'my-token-x': cookie.value,\n      },\n    })\n  })\n```\n\n#### Using `cy.getCookie()` to test logging in\n\nCheck out our example recipes using `cy.getCookie()` to test [logging in using HTML web forms](/llm/markdown/app/references/recipes.md#Logging-In), [logging in using XHR web forms](/llm/markdown/app/references/recipes.md#Logging-In) and [logging in with single sign on](/llm/markdown/app/references/recipes.md#Logging-In)\n",
      "section": "api",
      "anchors": [
        "session-id"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 253
    },
    {
      "id": "api/commands/getcookie#get-session-id-cookie-after-logging-in",
      "doc_id": "api/commands/getcookie",
      "heading": "Get session_id cookie after logging in",
      "heading_level": 4,
      "content_markdown": "#### Get `session_id` cookie after logging in\n\nIn this example, on first login, our server sends us back a session cookie.\n\n```\n// assume we just logged in\ncy.contains('Login').click()\ncy.url().should('include', 'profile')\n// retries until cookie with value=189jd09su\n// is found or default command timeout ends\ncy.getCookie('session_id')\n  .should('have.property', 'value', '189jd09su')\n  .then((cookie) => {\n    // cookie is an object with \"domain\", \"name\" and other properties\n  })\n```\n\nYou can check the cookie existence without comparing any of its properties\n\n```\ncy.getCookie('my-session-cookie').should('exist')\n```\n\nIf you need the cookie value, for example to use in a subsequent call\n\n```\nlet cookie\n\ncy.getCookie('session_id')\n  .should('exist')\n  .then((c) => {\n    // save cookie until we need it\n    cookie = c\n  })\n\n// some time later, force the \"cy.request\"\n// to run ONLY after the cookie has been set\n// by placing it inside \".then\"\ncy.get('#submit')\n  .click()\n  .then(() => {\n    cy.request({\n      url: '/api/admin',\n      headers: {\n        'my-token-x': cookie.value,\n      },\n    })\n  })\n```\n",
      "section": "api",
      "anchors": [
        "get-session-id-cookie-after-logging-in"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 203
    },
    {
      "id": "api/commands/getcookie#using-cy-getcookie-to-test-logging-in",
      "doc_id": "api/commands/getcookie",
      "heading": "Using cy.getCookie() to test logging in",
      "heading_level": 4,
      "content_markdown": "#### Using `cy.getCookie()` to test logging in\n\nCheck out our example recipes using `cy.getCookie()` to test [logging in using HTML web forms](/llm/markdown/app/references/recipes.md#Logging-In), [logging in using XHR web forms](/llm/markdown/app/references/recipes.md#Logging-In) and [logging in with single sign on](/llm/markdown/app/references/recipes.md#Logging-In)\n",
      "section": "api",
      "anchors": [
        "using-cy-getcookie-to-test-logging-in"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 47
    },
    {
      "id": "api/commands/getcookie#rules",
      "doc_id": "api/commands/getcookie",
      "heading": "Rules",
      "heading_level": 2,
      "content_markdown": "## Rules\n\n### Requirements\n\n*   `cy.getCookie()` requires being chained off of `cy`.\n\n### Assertions\n\n*   `cy.getCookie()` will automatically [retry](/llm/markdown/app/core-concepts/retry-ability.md) until all chained assertions have passed.\n\n### Timeouts\n\n*   `cy.getCookie()` can time out waiting for assertions you've added to pass.\n",
      "section": "api",
      "anchors": [
        "rules"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 52
    },
    {
      "id": "api/commands/getcookie#command-log",
      "doc_id": "api/commands/getcookie",
      "heading": "Command Log",
      "heading_level": 2,
      "content_markdown": "## Command Log\n\n**_Get a browser cookie and make assertions about the object_**\n\n```\ncy.getCookie('fakeCookie1').should('have.property', 'value', '123ABC')\n```\n\nThe commands above will display in the Command Log as:\n\nWhen clicking on `getCookie` within the command log, the console outputs the following:\n",
      "section": "api",
      "anchors": [
        "command-log"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 55
    },
    {
      "id": "api/commands/getcookie#history",
      "doc_id": "api/commands/getcookie",
      "heading": "History",
      "heading_level": 2,
      "content_markdown": "## History\n\n| Version | Changes |\n| --- | --- |\n| [16.0.0](/llm/markdown/app/references/changelog.md#16-0-0) | `cy.getCookie()` is now a query and retries assertions until they pass or time out. |\n| [5.0.0](/llm/markdown/app/references/changelog.md#5-0-0) | Removed `experimentalGetCookiesSameSite` and made `sameSite` property always available. |\n| [4.3.0](/llm/markdown/app/references/changelog.md#4-3-0) | Added `sameSite` property when the `experimentalGetCookiesSameSite` configuration value is `true`. |\n",
      "section": "api",
      "anchors": [
        "history"
      ],
      "path": "/llm/json/chunked/api/commands/getcookie.json",
      "token_estimate": 75
    }
  ]
}