{"__v":0,"_id":"56902d3448df220d004ca232","category":{"__v":60,"_id":"569002f19ebef90d0087289d","pages":["56900352769f210d00132595","5690047121fcf0190071d943","569004924719c119002ce654","569004ceb700ce0d002f4b94","569023e518c3920d00be8b37","569023f748df220d004ca215","5690240af7eb9a0d00f4465d","5690241b48df220d004ca217","5690243e48df220d004ca219","56902453741e9c0d00af2fb5","56902463efcc380d0043a5a1","5690247b18c3920d00be8b39","56902492f7eb9a0d00f4465f","569024a16c49d70d00f18075","569024b2efcc380d0043a5a3","569024cc48df220d004ca21b","569024ea18c3920d00be8b3b","5690258618c3920d00be8b3f","5690259bf7eb9a0d00f44662","569025b06c49d70d00f18077","569029b418c3920d00be8b43","569029d4f7eb9a0d00f44667","569029e8e056c80d00fdec58","569029fb48df220d004ca221","56902a1c18c3920d00be8b46","56902a45f7eb9a0d00f44669","56902a6be056c80d00fdec5a","56902a78f7eb9a0d00f4466b","56902a8848df220d004ca224","56902aa018c3920d00be8b4b","56902ace6c49d70d00f18085","56902ae0741e9c0d00af2fc6","56902aee48df220d004ca226","56902af8f7eb9a0d00f44674","56902b26efcc380d0043a5b1","56902b8148df220d004ca22a","56902ba918c3920d00be8b55","56902bb96c49d70d00f1808b","56902bc818c3920d00be8b57","56902bd518c3920d00be8b59","56902be218c3920d00be8b5b","56902bf66c49d70d00f1808e","56902c05e056c80d00fdec5d","56902c196c49d70d00f18090","56902c2648df220d004ca22d","56902c34f7eb9a0d00f44678","56902c5518c3920d00be8b5d","56902c62741e9c0d00af2fcc","56902cde48df220d004ca230","56902cea741e9c0d00af2fcf","56902d0ae056c80d00fdec60","56902d20efcc380d0043a5b4","56902d3448df220d004ca232","5696c3fbf9203821005fe2fb","5696c3fb9e2d000d00947ab0","5696c3fbf9203821005fe2fa","5697efee8d2a770d00d2fd17","569802611c4dc823005426c7","56a65c82b3ffe00d00156eaf","56f01f88332da41700f24b74"],"project":"568fde81b700ce0d002f4b43","version":"568fde82b700ce0d002f4b46","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-01-08T18:41:53.562Z","from_sync":false,"order":1,"slug":"commands","title":"Commands"},"parentDoc":null,"project":"568fde81b700ce0d002f4b43","user":"568fffce769f210d0013258f","version":{"__v":23,"_id":"568fde82b700ce0d002f4b46","project":"568fde81b700ce0d002f4b43","createdAt":"2016-01-08T16:06:26.373Z","releaseDate":"2016-01-08T16:06:26.373Z","categories":["568fde82b700ce0d002f4b47","568ff0e504440a1700e4cbbd","569002f19ebef90d0087289d","569004f4769f210d00132599","5690056d9ebef90d008728a0","569005d394c5030d0028813a","5690067804440a1700e4cbe2","569137eb3c4f510d00ec9b92","56913815e56a790d008dbfe3","569138ba3c4f510d00ec9b93","5691392f3c4f510d00ec9b94","56913bbe72f2810d007e4cb0","56933b8d6ebadc0d005b71d2","56933b8d6ebadc0d005b71d3","569564facaa32519009c41e6","5696a319b6d61f0d00acfb40","5696a319a857080d0082e8e8","5697efe43503e40d0061f4d1","5697efe48d2a770d00d2fd16","569e9597ffccd10d00a05c59","56a7a1523d33bc2100793d5c","56a7a32ecf6d771700baeee8","56b8b0f7ddeb231700e69825"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":false,"codename":"bar","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-01-08T21:42:12.612Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":65,"body":"Visit a remote url. This will most likely be the first command you run. `cy.visit` resolves when the remote page fires its `load` event.\n\nVisit is prefixed with the `baseUrl` configured in the [Network Options](https://on.cypress.io/guides/configuration#section-global).\n\nUsing `baseUrl` is a great way to prevent repeating yourself in every `cy.visit`.\n\n| | |\n|--- | --- |\n| **Returns** | the remote page's window object |\n| **Timeout** | `cy.visit` will retry for the duration of the [pageLoadTimeout](https://on.cypress.io/guides/configuration#section-timeouts) or the duration of the `timeout` specified in the command's [options](#section-options). |\n\n***\n\n# [cy.visit( *url* )](#section-usage)\n\nVisit the specified url passed as a string.\n\n***\n\n# Options\n\nPass in an options object to change the default behavior of `cy.visit`.\n\n**[cy.visit( *url*, *options* )](#section-options-usage)**\n\nOption | Default | Notes\n--- | --- | ---\n`onBeforeLoad` | `function` | Called before your page has loaded all of its resources.\n`onLoad`       | `function` | Called once your page has fired its load event.\n`timeout`      | [pageLoadTimeout](https://on.cypress.io/guides/configuration#section-timeouts) | Total time to wait until `cy.visit` resolves\n`log` | `true` | whether to display command in command log\n\nYou can also set options for all `cy.visit` `pageLoadTimeout` and `baseUrl` globally in [configuration](https://on.cypress.io/guides/configuration).\n\n***\n\n# Usage\n\n## Visit a local server running on http://localhost:8000\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.visit(\\\"http://localhost:8000\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Protocol can be omitted from common hosts\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// Cypress will automatically prepend the http:// protocol\\n// to common hosts.  If you're not using one of these\\n// 3 hosts, then make sure to provide the protocol\\ncy.visit(\\\"localhost:3000\\\") // => http://localhost:3000\\ncy.visit(\\\"0.0.0.0:3000\\\")   // => http://0.0.0.0:3000\\ncy.visit(\\\"127.0.0.1:3000\\\") // => http://127.0.0.1:3000\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Cypress can optionally act as your web server\n\nHaving Cypress serve your files is useful in simple projects and example apps, but isn't recommended for real apps.  It is always better to run your own server and provide the url to Cypress.\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// Cypress will automatically attempt to serve your files\\n// if you do not provide a host. The path should be relative\\n// to your project's root folder. The root folder is\\n// where cypress.json is stored.\\ncy.visit(\\\"app/index.html\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Visit is automatically prefixed with `baseUrl`.\n\nSimply configure `baseUrl` in the `cypress.json` file to prevent repeating yourself in every single `cy.visit(...)`. Read more about [`configuration`](https://on.cypress.io/guides/configuration).\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// cypress.json\\n{\\n  baseUrl: \\\"http://localhost:3000/#/\\\"\\n}\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// this will visit the complete url\\n// http://localhost:3000/#/dashboard\\ncy.visit(\\\"dashboard\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Options Usage\n\n## Change the default timeout\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// change the timeout to be 30 seconds\\ncy.visit(\\\"/index.html\\\", {timeout: 30000})\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Provide an `onBeforeLoad` callback function\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// onBeforeLoad is called as soon as possible, before\\n// your page has loaded all of its resources.  Your scripts\\n// will not be ready at this point, but it's a great hook\\n// to potentially manipulate the page.\\ncy.visit(\\\"http://localhost:3000/#dashboard\\\", {\\n  onBeforeLoad: function(contentWindow){\\n    // contentWindow is the remote page's window object\\n  }\\n})\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Provide an `onLoad` callback function\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// onLoad is called once your page has fired its load event.\\n// all of the scripts, stylesheets, html and other resources\\n// are guaranteed to be available at this point.\\ncy.visit(\\\"http://localhost:3000/#/users\\\", {\\n  onLoad: function(contentWindow){\\n    // contentWindow is the remote page's window object\\n    if(contentWindow.angular){\\n      // do something\\n    }\\n  }\\n})\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Notes\n\n## Visit will always yield the remote page's window object when it resolves\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.visit(\\\"index.html\\\").then(function(contentWindow)){\\n  // contentWindow is the remote page's window object\\n}\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Visit will automatically follow redirects\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// we aren't logged in, and our webserver\\n// redirects us to /login\\ncy\\n  .visit(\\\"http://localhost3000/admin\\\")\\n  .url().should(\\\"match\\\", /login/)\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Cypress automatically wipes page state between visits\n\nWhenever you `cy.visit()`, Cypress will automatically wipe the state of the page before navigating to an external page.\n\nInternally Cypress will visit `about:blank` which flushes the window.\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// internally this does the following:\\n// visit 'dashboard'\\n// visit 'about:blank'\\n// visit 'users'\\ncy\\n  .visit(\\\"dashboard\\\")\\n\\n  ...more commands...\\n\\n  .visit(\\\"users\\\")\\n\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Preventing XHR / AJAX requests before a remote page initially loads\n\nOne common scenario Cypress supports is visiting a remote page and also preventing any AJAX requests from immediately going out.\n\nYou may think this works:\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .visit(\\\"http://localhost:8000/#/app\\\")\\n  .server()\\n  .route(/users/, {...})\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nBut if your app makes a request upon being initialized, *the above code will not work*.  `cy.visit()` will resolve once its `load` event fires.  The `server` and `route` commands are not processed until *after* `visit` resolves.\n\nMany applications will have already begun routing, initialization, and requests by the time `visit` resolves. Therefore creating a `cy.server` will happen too late, and Cypress will not process the requests.\n\nLuckily Cypress supports this use case easily. Simply reverse the order of the commands:\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .server()\\n  .route(/users/, {...})\\n  .visit(\\\"http://localhost:8000/#/app\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nCypress will automatically apply the server and routes to the very next `visit` and does so immediately before any of your application code runs.\n\n***\n\n# Related\n\n- [go](https://on.cypress.io/api/go)\n- [server](https://on.cypress.io/api/server)","excerpt":"Visit a remote url","slug":"visit","type":"basic","title":"visit"}

visit

Visit a remote url

Visit a remote url. This will most likely be the first command you run. `cy.visit` resolves when the remote page fires its `load` event. Visit is prefixed with the `baseUrl` configured in the [Network Options](https://on.cypress.io/guides/configuration#section-global). Using `baseUrl` is a great way to prevent repeating yourself in every `cy.visit`. | | | |--- | --- | | **Returns** | the remote page's window object | | **Timeout** | `cy.visit` will retry for the duration of the [pageLoadTimeout](https://on.cypress.io/guides/configuration#section-timeouts) or the duration of the `timeout` specified in the command's [options](#section-options). | *** # [cy.visit( *url* )](#section-usage) Visit the specified url passed as a string. *** # Options Pass in an options object to change the default behavior of `cy.visit`. **[cy.visit( *url*, *options* )](#section-options-usage)** Option | Default | Notes --- | --- | --- `onBeforeLoad` | `function` | Called before your page has loaded all of its resources. `onLoad` | `function` | Called once your page has fired its load event. `timeout` | [pageLoadTimeout](https://on.cypress.io/guides/configuration#section-timeouts) | Total time to wait until `cy.visit` resolves `log` | `true` | whether to display command in command log You can also set options for all `cy.visit` `pageLoadTimeout` and `baseUrl` globally in [configuration](https://on.cypress.io/guides/configuration). *** # Usage ## Visit a local server running on http://localhost:8000 [block:code] { "codes": [ { "code": "cy.visit(\"http://localhost:8000\")\n", "language": "javascript" } ] } [/block] *** ## Protocol can be omitted from common hosts [block:code] { "codes": [ { "code": "// Cypress will automatically prepend the http:// protocol\n// to common hosts. If you're not using one of these\n// 3 hosts, then make sure to provide the protocol\ncy.visit(\"localhost:3000\") // => http://localhost:3000\ncy.visit(\"0.0.0.0:3000\") // => http://0.0.0.0:3000\ncy.visit(\"127.0.0.1:3000\") // => http://127.0.0.1:3000\n", "language": "javascript" } ] } [/block] *** ## Cypress can optionally act as your web server Having Cypress serve your files is useful in simple projects and example apps, but isn't recommended for real apps. It is always better to run your own server and provide the url to Cypress. [block:code] { "codes": [ { "code": "// Cypress will automatically attempt to serve your files\n// if you do not provide a host. The path should be relative\n// to your project's root folder. The root folder is\n// where cypress.json is stored.\ncy.visit(\"app/index.html\")\n", "language": "javascript" } ] } [/block] *** ## Visit is automatically prefixed with `baseUrl`. Simply configure `baseUrl` in the `cypress.json` file to prevent repeating yourself in every single `cy.visit(...)`. Read more about [`configuration`](https://on.cypress.io/guides/configuration). [block:code] { "codes": [ { "code": "// cypress.json\n{\n baseUrl: \"http://localhost:3000/#/\"\n}\n", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "// this will visit the complete url\n// http://localhost:3000/#/dashboard\ncy.visit(\"dashboard\")\n", "language": "javascript" } ] } [/block] *** # Options Usage ## Change the default timeout [block:code] { "codes": [ { "code": "// change the timeout to be 30 seconds\ncy.visit(\"/index.html\", {timeout: 30000})\n", "language": "javascript" } ] } [/block] *** ## Provide an `onBeforeLoad` callback function [block:code] { "codes": [ { "code": "// onBeforeLoad is called as soon as possible, before\n// your page has loaded all of its resources. Your scripts\n// will not be ready at this point, but it's a great hook\n// to potentially manipulate the page.\ncy.visit(\"http://localhost:3000/#dashboard\", {\n onBeforeLoad: function(contentWindow){\n // contentWindow is the remote page's window object\n }\n})\n", "language": "javascript" } ] } [/block] *** ## Provide an `onLoad` callback function [block:code] { "codes": [ { "code": "// onLoad is called once your page has fired its load event.\n// all of the scripts, stylesheets, html and other resources\n// are guaranteed to be available at this point.\ncy.visit(\"http://localhost:3000/#/users\", {\n onLoad: function(contentWindow){\n // contentWindow is the remote page's window object\n if(contentWindow.angular){\n // do something\n }\n }\n})\n", "language": "javascript" } ] } [/block] *** # Notes ## Visit will always yield the remote page's window object when it resolves [block:code] { "codes": [ { "code": "cy.visit(\"index.html\").then(function(contentWindow)){\n // contentWindow is the remote page's window object\n}\n", "language": "javascript" } ] } [/block] *** ## Visit will automatically follow redirects [block:code] { "codes": [ { "code": "// we aren't logged in, and our webserver\n// redirects us to /login\ncy\n .visit(\"http://localhost3000/admin\")\n .url().should(\"match\", /login/)\n", "language": "javascript" } ] } [/block] *** ## Cypress automatically wipes page state between visits Whenever you `cy.visit()`, Cypress will automatically wipe the state of the page before navigating to an external page. Internally Cypress will visit `about:blank` which flushes the window. [block:code] { "codes": [ { "code": "// internally this does the following:\n// visit 'dashboard'\n// visit 'about:blank'\n// visit 'users'\ncy\n .visit(\"dashboard\")\n\n ...more commands...\n\n .visit(\"users\")\n\n", "language": "javascript" } ] } [/block] *** ## Preventing XHR / AJAX requests before a remote page initially loads One common scenario Cypress supports is visiting a remote page and also preventing any AJAX requests from immediately going out. You may think this works: [block:code] { "codes": [ { "code": "cy\n .visit(\"http://localhost:8000/#/app\")\n .server()\n .route(/users/, {...})\n", "language": "javascript" } ] } [/block] But if your app makes a request upon being initialized, *the above code will not work*. `cy.visit()` will resolve once its `load` event fires. The `server` and `route` commands are not processed until *after* `visit` resolves. Many applications will have already begun routing, initialization, and requests by the time `visit` resolves. Therefore creating a `cy.server` will happen too late, and Cypress will not process the requests. Luckily Cypress supports this use case easily. Simply reverse the order of the commands: [block:code] { "codes": [ { "code": "cy\n .server()\n .route(/users/, {...})\n .visit(\"http://localhost:8000/#/app\")\n", "language": "javascript" } ] } [/block] Cypress will automatically apply the server and routes to the very next `visit` and does so immediately before any of your application code runs. *** # Related - [go](https://on.cypress.io/api/go) - [server](https://on.cypress.io/api/server)