{"__v":0,"_id":"5696c3fbf9203821005fe2fa","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-13T21:39:07.112Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"auth":"required","params":[],"url":""},"isReference":false,"order":66,"body":"Use `cy.wait` to wait for a number of milliseconds or for a resource to resolve.\n\n| | |\n|--- | --- |\n| **Returns** | the current subject if waiting for number of milliseconds, the xhr object if waiting for a route |\n| **Timeout** | `cy.wait` will wait for the request the duration of the [requestTimeout](https://on.cypress.io/guides/configuration#section-timeouts) and wait for the response for the duration of the [responseTimeout](https://on.cypress.io/guides/configuration#section-timeouts) or it will wait for both the duration request and response for the `timeout` specified in the command's [options](#section-options).|\n\n***\n\n# [cy.wait( *number* )](#section-number-usage)\n\nWait a specific amount of `ms` before resolving and continuing onto the next command.\n\n***\n\n# [cy.wait( *alias* )](#section-alias-usage)\n\nWait until the matching [aliased](https://on.cypress.io/guides/using-aliases) XHR has a response.\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Read about [Network Requests](https://on.cypress.io/guides/network-requests-xhr) and [Aliasing](https://on.cypress.io/guides/using-aliases) first.\",\n  \"title\": \"New to Cypress?\"\n}\n[/block]\n\n***\n\n# [cy.wait( *\\[alias1*, *alias2*, *alias3\\]* )](#alias-array-usage)\n\nWait for an array of [aliases](https://on.cypress.io/guides/using-aliases) to have responses.\n\n***\n\n# Options\n\nPass in an options object to change the default behavior of `cy.wait`.\n\n**[cy.wait( *text*, *options* )](#options-usage)**\n\nOption | Default | Notes\n--- | --- | ---\n`timeout` | [requestTimeout](https://on.cypress.io/guides/configuration#section-timeouts), [responseTimeout](https://on.cypress.io/guides/configuration#section-timeouts) | Override the default requestTimeout and responseTimeout (in ms)\n`log` | `true` | whether to display command in command log\n\nYou can also change the default `requestTimeout` and `responseTimeout` that all `cy.wait` use in [configuration](https://on.cypress.io/guides/configuration).\n\n***\n\n# Number Usage\n\n## Wait 500ms\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// Wait 500ms before resolving\\ncy.wait(500)\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Alias Usage\n\n## Wait for a specific XHR to respond\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// Wait for the route aliased as 'getAccount' to respond\\n// without changing or stubbing its response\\ncy\\n  .server()\\n  .route(/accounts\\\\/d+/).as(\\\"getAccount\\\")\\n  .visit(\\\"/accounts/123\\\")\\n  .wait(\\\":::at:::getAccount\\\").then(function(xhr){\\n    // we can now access the low level xhr\\n    // that contains the request body,\\n    // response body, status, etc\\n  })\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Wait automatically increments responses\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// each time we cy.wait() for an alias, Cypress will\\n// wait for the next nth matching request\\ncy\\n  .server()\\n  .route(/books/, []).as(\\\"getBooks\\\")\\n  .get(\\\"#search\\\").type(\\\"Grendel\\\")\\n\\n  // wait for the first response to finish\\n  .wait(\\\"@getBooks\\\")\\n\\n  // the results should be empty because we\\n  // responded with an empty array first\\n  .get(\\\"#book-results\\\").should(\\\"be.empty\\\")\\n\\n  // now re-route the books endpoint and force it to\\n  // have a response this time\\n  .route(/books/, [{name: \\\"Emperor of all maladies\\\"}])\\n\\n  .get(\\\"#search\\\").type(\\\"Emperor of\\\")\\n\\n  // now when we wait for 'getBooks' again, Cypress will\\n  // automatically know to wait for the 2nd response\\n  .wait(\\\"@getBooks\\\")\\n\\n  // we responded with 1 book item so now we should\\n  // have one result\\n  .get(\\\"#book-results\\\").should(\\\"have.length\\\", 1)\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Alias Array Usage\n\n## You can pass an array of aliases that will be waited on before resolving.\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .server()\\n  .route(/users/).as(\\\"getUsers\\\")\\n  .route(/activities/).as(\\\"getActivities\\\")\\n  .route(/comments/).as(\\\"getComments\\\")\\n  .visit(\\\"/dashboard\\\")\\n\\n  .wait([\\\"@getUsers\\\", \\\"@getActivities\\\", \\\"getComments\\\"])\\n  .then(function(xhrs){\\n    // xhrs will now be an array of matching XHR's\\n    // xhrs[0] <-- getUsers\\n    // xhrs[1] <-- getActivities\\n    // xhrs[2] <-- getComments\\n  })\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n## You could also use the [`cy.spread`](https://on.cypress.io/api/spread) command here to spread the array into multiple arguments.\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .server()\\n  .route(/users/).as(\\\"getUsers\\\")\\n  .route(/activities/).as(\\\"getActivities\\\")\\n  .route(/comments/).as(\\\"getComments\\\")\\n  .wait([\\\"@getUsers\\\", \\\"@getActivities\\\", \\\"getComments\\\"])\\n  .spread(function(getUsers, getActivities, getComments){\\n    // each XHR is now an individual argument\\n  })\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Notes\n\n## requestTimeout and responseTimeout\n\n`cy.wait` goes through two separate \"waiting\" periods for a matching XHR.\n\nThe first period waits for a matching request to leave the browser. This duration is configured by [`requestTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) - which has a default of `5000` ms.\n\nThis means that when you begin waiting for an XHR, Cypress will wait up to 5 seconds for a matching XHR to be created. If no matching XHR is found, you will get an error message that looks like this:\n\n![screen shot 2015-12-21 at 5 00 09 pm](https://cloud.githubusercontent.com/assets/1268976/11942578/8e7cba50-a805-11e5-805c-614f8640fbcc.png)\n\nOnce Cypress detects that a matching XHR has begun its request it then switches over to the 2nd waiting period. This duration is configured by [`responseTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) - which has a default of `20000` ms.\n\nThis means Cypress will now wait up to 20 seconds for the external server to respond to this XHR. If no response is detected, you will get an error message that looks like this:\n\n![screen shot 2015-12-21 at 5 06 52 pm](https://cloud.githubusercontent.com/assets/1268976/11942577/8e7196e8-a805-11e5-97b1-8acdde27755d.png)\n\nThis gives you the best of both worlds - a fast error feedback loop when requests never go out, and a much longer duration for the actual external response.\n\n***\n\n# Command Log\n\n## Wait for the put to user to resolve.\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .server()\\n  .route(\\\"PUT\\\", /users/, {}).as(\\\"userPut\\\")\\n  .get(\\\"form\\\").submit()\\n  .wait(\\\"@userPut\\\")\\n    .its(\\\"url\\\").should(\\\"include\\\", \\\"users\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nThe commands above will display in the command log as:\n\n<img width=\"584\" alt=\"screen shot 2015-11-29 at 2 20 53 pm\" src=\"https://cloud.githubusercontent.com/assets/1271364/11459433/7eabc516-96a4-11e5-90c3-19f8e49a0b0c.png\">\n\nWhen clicking on `wait` within the command log, the console outputs the following:\n\n<img width=\"952\" alt=\"screen shot 2015-11-29 at 2 21 11 pm\" src=\"https://cloud.githubusercontent.com/assets/1271364/11459434/81132966-96a4-11e5-962f-41718b49b142.png\">\n\n***\n\n# Related\n\n- [server](https://on.cypress.io/api/server)\n- [route](https://on.cypress.io/api/route)\n- [as](https://on.cypress.io/api/as)\n- [spread](https://on.cypress.io/api/spread)","excerpt":"Wait for a specific amount of time or resource to resolve","slug":"wait","type":"basic","title":"wait"}

wait

Wait for a specific amount of time or resource to resolve

Use `cy.wait` to wait for a number of milliseconds or for a resource to resolve. | | | |--- | --- | | **Returns** | the current subject if waiting for number of milliseconds, the xhr object if waiting for a route | | **Timeout** | `cy.wait` will wait for the request the duration of the [requestTimeout](https://on.cypress.io/guides/configuration#section-timeouts) and wait for the response for the duration of the [responseTimeout](https://on.cypress.io/guides/configuration#section-timeouts) or it will wait for both the duration request and response for the `timeout` specified in the command's [options](#section-options).| *** # [cy.wait( *number* )](#section-number-usage) Wait a specific amount of `ms` before resolving and continuing onto the next command. *** # [cy.wait( *alias* )](#section-alias-usage) Wait until the matching [aliased](https://on.cypress.io/guides/using-aliases) XHR has a response. [block:callout] { "type": "info", "body": "Read about [Network Requests](https://on.cypress.io/guides/network-requests-xhr) and [Aliasing](https://on.cypress.io/guides/using-aliases) first.", "title": "New to Cypress?" } [/block] *** # [cy.wait( *\[alias1*, *alias2*, *alias3\]* )](#alias-array-usage) Wait for an array of [aliases](https://on.cypress.io/guides/using-aliases) to have responses. *** # Options Pass in an options object to change the default behavior of `cy.wait`. **[cy.wait( *text*, *options* )](#options-usage)** Option | Default | Notes --- | --- | --- `timeout` | [requestTimeout](https://on.cypress.io/guides/configuration#section-timeouts), [responseTimeout](https://on.cypress.io/guides/configuration#section-timeouts) | Override the default requestTimeout and responseTimeout (in ms) `log` | `true` | whether to display command in command log You can also change the default `requestTimeout` and `responseTimeout` that all `cy.wait` use in [configuration](https://on.cypress.io/guides/configuration). *** # Number Usage ## Wait 500ms [block:code] { "codes": [ { "code": "// Wait 500ms before resolving\ncy.wait(500)\n", "language": "javascript" } ] } [/block] *** # Alias Usage ## Wait for a specific XHR to respond [block:code] { "codes": [ { "code": "// Wait for the route aliased as 'getAccount' to respond\n// without changing or stubbing its response\ncy\n .server()\n .route(/accounts\\/d+/).as(\"getAccount\")\n .visit(\"/accounts/123\")\n .wait(\"@getAccount\").then(function(xhr){\n // we can now access the low level xhr\n // that contains the request body,\n // response body, status, etc\n })\n", "language": "javascript" } ] } [/block] *** ## Wait automatically increments responses [block:code] { "codes": [ { "code": "// each time we cy.wait() for an alias, Cypress will\n// wait for the next nth matching request\ncy\n .server()\n .route(/books/, []).as(\"getBooks\")\n .get(\"#search\").type(\"Grendel\")\n\n // wait for the first response to finish\n .wait(\"@getBooks\")\n\n // the results should be empty because we\n // responded with an empty array first\n .get(\"#book-results\").should(\"be.empty\")\n\n // now re-route the books endpoint and force it to\n // have a response this time\n .route(/books/, [{name: \"Emperor of all maladies\"}])\n\n .get(\"#search\").type(\"Emperor of\")\n\n // now when we wait for 'getBooks' again, Cypress will\n // automatically know to wait for the 2nd response\n .wait(\"@getBooks\")\n\n // we responded with 1 book item so now we should\n // have one result\n .get(\"#book-results\").should(\"have.length\", 1)\n", "language": "javascript" } ] } [/block] *** # Alias Array Usage ## You can pass an array of aliases that will be waited on before resolving. [block:code] { "codes": [ { "code": "cy\n .server()\n .route(/users/).as(\"getUsers\")\n .route(/activities/).as(\"getActivities\")\n .route(/comments/).as(\"getComments\")\n .visit(\"/dashboard\")\n\n .wait([\"@getUsers\", \"@getActivities\", \"getComments\"])\n .then(function(xhrs){\n // xhrs will now be an array of matching XHR's\n // xhrs[0] <-- getUsers\n // xhrs[1] <-- getActivities\n // xhrs[2] <-- getComments\n })\n", "language": "javascript" } ] } [/block] ## You could also use the [`cy.spread`](https://on.cypress.io/api/spread) command here to spread the array into multiple arguments. [block:code] { "codes": [ { "code": "cy\n .server()\n .route(/users/).as(\"getUsers\")\n .route(/activities/).as(\"getActivities\")\n .route(/comments/).as(\"getComments\")\n .wait([\"@getUsers\", \"@getActivities\", \"getComments\"])\n .spread(function(getUsers, getActivities, getComments){\n // each XHR is now an individual argument\n })\n", "language": "javascript" } ] } [/block] *** # Notes ## requestTimeout and responseTimeout `cy.wait` goes through two separate "waiting" periods for a matching XHR. The first period waits for a matching request to leave the browser. This duration is configured by [`requestTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) - which has a default of `5000` ms. This means that when you begin waiting for an XHR, Cypress will wait up to 5 seconds for a matching XHR to be created. If no matching XHR is found, you will get an error message that looks like this: ![screen shot 2015-12-21 at 5 00 09 pm](https://cloud.githubusercontent.com/assets/1268976/11942578/8e7cba50-a805-11e5-805c-614f8640fbcc.png) Once Cypress detects that a matching XHR has begun its request it then switches over to the 2nd waiting period. This duration is configured by [`responseTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) - which has a default of `20000` ms. This means Cypress will now wait up to 20 seconds for the external server to respond to this XHR. If no response is detected, you will get an error message that looks like this: ![screen shot 2015-12-21 at 5 06 52 pm](https://cloud.githubusercontent.com/assets/1268976/11942577/8e7196e8-a805-11e5-97b1-8acdde27755d.png) This gives you the best of both worlds - a fast error feedback loop when requests never go out, and a much longer duration for the actual external response. *** # Command Log ## Wait for the put to user to resolve. [block:code] { "codes": [ { "code": "cy\n .server()\n .route(\"PUT\", /users/, {}).as(\"userPut\")\n .get(\"form\").submit()\n .wait(\"@userPut\")\n .its(\"url\").should(\"include\", \"users\")\n", "language": "javascript" } ] } [/block] The commands above will display in the command log as: <img width="584" alt="screen shot 2015-11-29 at 2 20 53 pm" src="https://cloud.githubusercontent.com/assets/1271364/11459433/7eabc516-96a4-11e5-90c3-19f8e49a0b0c.png"> When clicking on `wait` within the command log, the console outputs the following: <img width="952" alt="screen shot 2015-11-29 at 2 21 11 pm" src="https://cloud.githubusercontent.com/assets/1271364/11459434/81132966-96a4-11e5-962f-41718b49b142.png"> *** # Related - [server](https://on.cypress.io/api/server) - [route](https://on.cypress.io/api/route) - [as](https://on.cypress.io/api/as) - [spread](https://on.cypress.io/api/spread)