{
  "doc": {
    "id": "api/commands/location",
    "title": "location | Cypress Documentation",
    "description": "Get the global `window.location` object of the page that is currently active in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/location.md",
    "version": "e6988a974973e9090ce70406c38cb2b9e0eac9fa",
    "updated_at": "2026-05-15T15:50:22.536Z",
    "headings": [
      {
        "id": "api/commands/location#location",
        "text": "location",
        "level": 1
      },
      {
        "id": "api/commands/location#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/location#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/location#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/location#yields-learn-about-subject-management",
        "text": "Yields Learn about subject management",
        "level": 3
      },
      {
        "id": "api/commands/location#when-not-given-a-key-argument",
        "text": "When not given a key argument:",
        "level": 4
      },
      {
        "id": "api/commands/location#when-given-a-key-argument",
        "text": "When given a key argument:",
        "level": 4
      },
      {
        "id": "api/commands/location#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/location#no-args",
        "text": "No Args",
        "level": 3
      },
      {
        "id": "api/commands/location#make-assertions-about-every-location-property",
        "text": "Make assertions about every location property",
        "level": 4
      },
      {
        "id": "api/commands/location#check-location-for-query-params-and-pathname",
        "text": "Check location for query params and pathname",
        "level": 4
      },
      {
        "id": "api/commands/location#key",
        "text": "Key",
        "level": 3
      },
      {
        "id": "api/commands/location#assert-that-a-redirect-works",
        "text": "Assert that a redirect works",
        "level": 4
      },
      {
        "id": "api/commands/location#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/location#native-location",
        "text": "Native Location",
        "level": 3
      },
      {
        "id": "api/commands/location#no-need-to-use-window-location",
        "text": "No need to use window.location",
        "level": 4
      },
      {
        "id": "api/commands/location#console-output-of-window-location",
        "text": "Console output of window.location",
        "level": 4
      },
      {
        "id": "api/commands/location#console-output-of-location",
        "text": "Console output of .location()",
        "level": 4
      },
      {
        "id": "api/commands/location#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/location#requirements-learn-about-chaining-commands",
        "text": "Requirements Learn about chaining commands",
        "level": 3
      },
      {
        "id": "api/commands/location#assertions-learn-about-assertions",
        "text": "Assertions Learn about assertions",
        "level": 3
      },
      {
        "id": "api/commands/location#timeouts-learn-about-timeouts",
        "text": "Timeouts Learn about timeouts",
        "level": 3
      },
      {
        "id": "api/commands/location#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/location#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/commands/location#syntax",
      "doc_id": "api/commands/location",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n```\ncy.location()cy.location(key)cy.location(options)cy.location(key, options)\n```\n\n### Usage\n\n**Correct Usage**\n\n```\ncy.location() // Get location objectcy.location('host') // Get the host of the location objectcy.location('port') // Get the port of the location object\n```\n\n### Arguments\n\n**key _(String)_**\n\nA key on the location object. Returns this value instead of the full location object.\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `cy.location()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\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.location()` to resolve before [timing out](#Timeouts) |\n\n### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)\n\n`cy.location()` is a query, and it is _safe_ to chain further commands.\n\n#### When not given a `key` argument:\n\n`cy.location()` yields the location object with the following properties:\n\n*   `hash`\n*   `host`\n*   `hostname`\n*   `href`\n*   `origin`\n*   `pathname`\n*   `port`\n*   `protocol`\n*   `search`\n*   `toString`\n\n#### When given a `key` argument:\n\n*   `cy.location()` yields the value of the location property as a string\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 241
    },
    {
      "id": "api/commands/location#arguments",
      "doc_id": "api/commands/location",
      "heading": "Arguments",
      "heading_level": 3,
      "content_markdown": "### Arguments\n\n**key _(String)_**\n\nA key on the location object. Returns this value instead of the full location object.\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `cy.location()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\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.location()` to resolve before [timing out](#Timeouts) |\n",
      "section": "api",
      "anchors": [
        "arguments"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 101
    },
    {
      "id": "api/commands/location#yields-learn-about-subject-management",
      "doc_id": "api/commands/location",
      "heading": "Yields Learn about subject management",
      "heading_level": 3,
      "content_markdown": "### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)\n\n`cy.location()` is a query, and it is _safe_ to chain further commands.\n\n#### When not given a `key` argument:\n\n`cy.location()` yields the location object with the following properties:\n\n*   `hash`\n*   `host`\n*   `hostname`\n*   `href`\n*   `origin`\n*   `pathname`\n*   `port`\n*   `protocol`\n*   `search`\n*   `toString`\n\n#### When given a `key` argument:\n\n*   `cy.location()` yields the value of the location property as a string\n",
      "section": "api",
      "anchors": [
        "yields-learn-about-subject-management"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 96
    },
    {
      "id": "api/commands/location#when-not-given-a-key-argument",
      "doc_id": "api/commands/location",
      "heading": "When not given a key argument:",
      "heading_level": 4,
      "content_markdown": "#### When not given a `key` argument:\n\n`cy.location()` yields the location object with the following properties:\n\n*   `hash`\n*   `host`\n*   `hostname`\n*   `href`\n*   `origin`\n*   `pathname`\n*   `port`\n*   `protocol`\n*   `search`\n*   `toString`\n",
      "section": "api",
      "anchors": [
        "when-not-given-a-key-argument"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 48
    },
    {
      "id": "api/commands/location#examples",
      "doc_id": "api/commands/location",
      "heading": "Examples",
      "heading_level": 2,
      "content_markdown": "## Examples\n\n### No Args\n\n#### Make assertions about every location property\n\n```\ncy.visit('http://localhost:8000/app/index.html?q=dan#/users/123/edit')cy.location().should((loc) => {  expect(loc.hash).to.eq('#/users/123/edit')  expect(loc.host).to.eq('localhost:8000')  expect(loc.hostname).to.eq('localhost')  expect(loc.href).to.eq(    'http://localhost:8000/app/index.html?q=dan#/users/123/edit'  )  expect(loc.origin).to.eq('http://localhost:8000')  expect(loc.pathname).to.eq('/app/index.html')  expect(loc.port).to.eq('8000')  expect(loc.protocol).to.eq('http:')  expect(loc.search).to.eq('?q=dan')  expect(loc.toString()).to.eq(    'http://localhost:8000/app/index.html?q=brian#/users/123/edit'  )})\n```\n\n#### Check location for query params and pathname\n\nWe can yield the location object within a [`.should()`](/llm/markdown/api/commands/should.md) command and work with it directly.\n\n```\ncy.get('#search').type('niklas{enter}')cy.location().should((loc) => {  expect(loc.search).to.eq('?search=niklas')  expect(loc.pathname).to.eq('/users')})\n```\n\n### Key\n\n#### Assert that a redirect works\n\nGrab only the `pathname` and add an assertion.\n\n```\ncy.visit('http://localhost:3000/admin')cy.location('pathname').should('eq', '/login')\n```\n",
      "section": "api",
      "anchors": [
        "examples"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 108
    },
    {
      "id": "api/commands/location#no-args",
      "doc_id": "api/commands/location",
      "heading": "No Args",
      "heading_level": 3,
      "content_markdown": "### No Args\n\n#### Make assertions about every location property\n\n```\ncy.visit('http://localhost:8000/app/index.html?q=dan#/users/123/edit')cy.location().should((loc) => {  expect(loc.hash).to.eq('#/users/123/edit')  expect(loc.host).to.eq('localhost:8000')  expect(loc.hostname).to.eq('localhost')  expect(loc.href).to.eq(    'http://localhost:8000/app/index.html?q=dan#/users/123/edit'  )  expect(loc.origin).to.eq('http://localhost:8000')  expect(loc.pathname).to.eq('/app/index.html')  expect(loc.port).to.eq('8000')  expect(loc.protocol).to.eq('http:')  expect(loc.search).to.eq('?q=dan')  expect(loc.toString()).to.eq(    'http://localhost:8000/app/index.html?q=brian#/users/123/edit'  )})\n```\n\n#### Check location for query params and pathname\n\nWe can yield the location object within a [`.should()`](/llm/markdown/api/commands/should.md) command and work with it directly.\n\n```\ncy.get('#search').type('niklas{enter}')cy.location().should((loc) => {  expect(loc.search).to.eq('?search=niklas')  expect(loc.pathname).to.eq('/users')})\n```\n",
      "section": "api",
      "anchors": [
        "no-args"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 79
    },
    {
      "id": "api/commands/location#check-location-for-query-params-and-pathname",
      "doc_id": "api/commands/location",
      "heading": "Check location for query params and pathname",
      "heading_level": 4,
      "content_markdown": "#### Check location for query params and pathname\n\nWe can yield the location object within a [`.should()`](/llm/markdown/api/commands/should.md) command and work with it directly.\n\n```\ncy.get('#search').type('niklas{enter}')cy.location().should((loc) => {  expect(loc.search).to.eq('?search=niklas')  expect(loc.pathname).to.eq('/users')})\n```\n",
      "section": "api",
      "anchors": [
        "check-location-for-query-params-and-pathname"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 40
    },
    {
      "id": "api/commands/location#notes",
      "doc_id": "api/commands/location",
      "heading": "Notes",
      "heading_level": 2,
      "content_markdown": "## Notes\n\n### Native Location\n\n#### No need to use `window.location`\n\nCypress automatically normalizes the `cy.location()` command and strips out extraneous values and properties found in `window.location`. Also, the object literal yielded by `cy.location()` is a basic object literal, not the special `window.location` object.\n\nWhen changing properties on the real `window.location` object, it forces the browser to navigate away. In Cypress, the object yielded is a plain object, so changing its properties will have no effect on navigation.\n\n#### Console output of `window.location`\n\n```\ncy.window().then((win) => {  console.log(win.location)})\n```\n\n#### Console output of `.location()`\n\n```\ncy.location().then((loc) => {  console.log(loc)})\n```\n",
      "section": "api",
      "anchors": [
        "notes"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 133
    },
    {
      "id": "api/commands/location#native-location",
      "doc_id": "api/commands/location",
      "heading": "Native Location",
      "heading_level": 3,
      "content_markdown": "### Native Location\n\n#### No need to use `window.location`\n\nCypress automatically normalizes the `cy.location()` command and strips out extraneous values and properties found in `window.location`. Also, the object literal yielded by `cy.location()` is a basic object literal, not the special `window.location` object.\n\nWhen changing properties on the real `window.location` object, it forces the browser to navigate away. In Cypress, the object yielded is a plain object, so changing its properties will have no effect on navigation.\n\n#### Console output of `window.location`\n\n```\ncy.window().then((win) => {  console.log(win.location)})\n```\n\n#### Console output of `.location()`\n\n```\ncy.location().then((loc) => {  console.log(loc)})\n```\n",
      "section": "api",
      "anchors": [
        "native-location"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 131
    },
    {
      "id": "api/commands/location#no-need-to-use-window-location",
      "doc_id": "api/commands/location",
      "heading": "No need to use window.location",
      "heading_level": 4,
      "content_markdown": "#### No need to use `window.location`\n\nCypress automatically normalizes the `cy.location()` command and strips out extraneous values and properties found in `window.location`. Also, the object literal yielded by `cy.location()` is a basic object literal, not the special `window.location` object.\n\nWhen changing properties on the real `window.location` object, it forces the browser to navigate away. In Cypress, the object yielded is a plain object, so changing its properties will have no effect on navigation.\n",
      "section": "api",
      "anchors": [
        "no-need-to-use-window-location"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 97
    },
    {
      "id": "api/commands/location#rules",
      "doc_id": "api/commands/location",
      "heading": "Rules",
      "heading_level": 2,
      "content_markdown": "## Rules\n\n### Requirements [Learn about chaining commands](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands)\n\n*   `cy.location()` requires being chained off of `cy`.\n\n### Assertions [Learn about assertions](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions)\n\n*   `cy.location()` will automatically [retry](/llm/markdown/app/core-concepts/retry-ability.md) until all chained assertions have passed\n\n### Timeouts [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts)\n\n*   `cy.location()` can time out waiting for assertions you've added to pass.\n",
      "section": "api",
      "anchors": [
        "rules"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 65
    },
    {
      "id": "api/commands/location#command-log",
      "doc_id": "api/commands/location",
      "heading": "Command Log",
      "heading_level": 2,
      "content_markdown": "## Command Log\n\n**_Assert on the location's href_**\n\n```\ncy.location().should((loc) => {  expect(loc.href).to.include('commands/querying')})\n```\n\nThe commands above will display in the Command Log as:\n\nWhen clicking on `location` within the command log, the console outputs the following:\n",
      "section": "api",
      "anchors": [
        "command-log"
      ],
      "path": "/llm/json/chunked/api/commands/location.json",
      "token_estimate": 49
    }
  ]
}