{"_id":"573d0ad1807b940e009f1485","user":"568fffce769f210d0013258f","__v":0,"parentDoc":null,"project":"568fde81b700ce0d002f4b43","category":{"_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","__v":60,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-01-08T18:41:53.562Z","from_sync":false,"order":1,"slug":"commands","title":"Commands"},"version":{"_id":"568fde82b700ce0d002f4b46","__v":23,"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-05-19T00:37:37.824Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"auth":"required","params":[],"url":""},"isReference":false,"order":19,"body":"Allows you to execute a system command. The system command can be anything you would normally run on the command line, such as `npm run build`, `rake db:seed`, etc.\n\n`cy.exec` provides an escape hatch for running arbitrary system commands, so you can take actions necessary for your test, but outside the scope of Cypress. This is great for running build scripts, seeding your test database, starting or killing processes, etc.\n\n`cy.exec` does not support commands that don't exit, such as `rails server`, a task that runs a watch, or any process that needs to be manually interrupted to stop. A command must exit within the timeout or Cypress will kill the command's process and fail the current test.\n\nWe don't recommend executing commands that take a long time to exit. Cypress will not continue running any other commands until `cy.exec` has finished, so a long-running command will drastically slow down your test cycle.\n\nThe current working directory is set to the project root (the directory that contains cypress.json).\n\n| | |\n|--- | --- |\n| **Returns** | an object with the exit `code`, the `stdout`, and the `stderr` |\n| **Timeout** | `cy.exec` will allow the command to execute for the duration of the [`execTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) |\n\n***\n\n# [cy.exec( *command* )](#section-command-usage)\n\nExecute a system command.\n\n***\n\n# Options\n\nPass in an options object to change the default behavior of `cy.exec`.\n\n**cy.exec( *command*, *options* )**\n\nOption | Default | Notes\n--- | --- | ---\n`log` | `true` | whether to display command in command log\n`timeout` | [`execTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) | Total time to allow the command to execute\n`failOnNonZeroExit` | `true` | Fail if the command exits with a non-zero code\n`env` | `{}` | Object of environment variables to set before the command executes (e.g. { USERNAME: 'johndoe' }). Will be merged with existing system environment variables\n\n***\n\n# Usage\n\n## Run a build command\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .exec(\\\"npm run build\\\")\\n  .then(function (result) {\\n    // subject is now the result object\\n    // {\\n    //   code: 0,\\n    //   stdout: \\\"Files successfully built\\\",\\n    //   stderr: \\\"\\\"\\n    // }\\n  })\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n## Seed the database and assert it was successful\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.exec(\\\"rake db:seed\\\").its(\\\"code\\\").should(\\\"eq\\\", 0)\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n## Run an arbitrary script and assert its output\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.exec(\\\"npm run my-script\\\").its(\\\"stdout\\\").should(\\\"contain\\\", \\\"Done running the script\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n## Change the timeout\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// will fail if script takes longer than 20 seconds to finish\\ncy.exec(\\\"npm run build\\\", { timeout: 20000 });\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n## Choose not to fail on non-zero exit and assert on code and stderr\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .exec(\\\"man bear pig\\\", { failOnNonZeroExit: false })\\n  .its(\\\"code\\\").should(\\\"eq\\\", 1)\\n  .its(\\\"stderr\\\").should(\\\"contain\\\", \\\"No manual entry for bear\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n## Specify environment variables\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .exec(\\\"echo $USERNAME\\\", { env: { USERNAME: \\\"johndoe\\\" } })\\n  .its(\\\"stdout\\\").should(\\\"contain\\\", \\\"johndoe\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n## Write to a file to create a fixture from response body\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .server()\\n  .route(\\\"POST\\\", \\\"/comments\\\").as(\\\"postComment\\\")\\n  .get(\\\".add-comment\\\").click()\\n  .wait(\\\":::at:::postComment\\\").then(function(xhr){\\n    cy\\n      .exec(\\\"echo '\\\" + JSON.stringify(xhr.responseBody) + \\\"'>cypress/fixtures/comment.json\\\")\\n      .fixture(\\\"comment.json\\\").should(\\\"deep.eq\\\", xhr.responseBody)\\n  })\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Command Log\n\n## List the contents of cypress.json\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.exec(\\\"cat cypress.json\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nThe command above will display in the command log as:\n\n<img width=\"445\" alt=\"screen shot of command log\" src=\"https://cloud.githubusercontent.com/assets/1157043/15369507/e03a7eca-1d00-11e6-8558-396d8c9b6d98.png\">\n\nWhen clicking on the `exec` command within the command log, the console outputs the following:\n\n<img width=\"758\" alt=\"screen shot of console output\" src=\"https://cloud.githubusercontent.com/assets/1157043/15969867/e3ab646e-2eff-11e6-9199-987ca2f74025.png\">","excerpt":"Execute a system command","slug":"exec","type":"basic","title":"exec"}

exec

Execute a system command

Allows you to execute a system command. The system command can be anything you would normally run on the command line, such as `npm run build`, `rake db:seed`, etc. `cy.exec` provides an escape hatch for running arbitrary system commands, so you can take actions necessary for your test, but outside the scope of Cypress. This is great for running build scripts, seeding your test database, starting or killing processes, etc. `cy.exec` does not support commands that don't exit, such as `rails server`, a task that runs a watch, or any process that needs to be manually interrupted to stop. A command must exit within the timeout or Cypress will kill the command's process and fail the current test. We don't recommend executing commands that take a long time to exit. Cypress will not continue running any other commands until `cy.exec` has finished, so a long-running command will drastically slow down your test cycle. The current working directory is set to the project root (the directory that contains cypress.json). | | | |--- | --- | | **Returns** | an object with the exit `code`, the `stdout`, and the `stderr` | | **Timeout** | `cy.exec` will allow the command to execute for the duration of the [`execTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) | *** # [cy.exec( *command* )](#section-command-usage) Execute a system command. *** # Options Pass in an options object to change the default behavior of `cy.exec`. **cy.exec( *command*, *options* )** Option | Default | Notes --- | --- | --- `log` | `true` | whether to display command in command log `timeout` | [`execTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) | Total time to allow the command to execute `failOnNonZeroExit` | `true` | Fail if the command exits with a non-zero code `env` | `{}` | Object of environment variables to set before the command executes (e.g. { USERNAME: 'johndoe' }). Will be merged with existing system environment variables *** # Usage ## Run a build command [block:code] { "codes": [ { "code": "cy\n .exec(\"npm run build\")\n .then(function (result) {\n // subject is now the result object\n // {\n // code: 0,\n // stdout: \"Files successfully built\",\n // stderr: \"\"\n // }\n })\n", "language": "javascript" } ] } [/block] ## Seed the database and assert it was successful [block:code] { "codes": [ { "code": "cy.exec(\"rake db:seed\").its(\"code\").should(\"eq\", 0)\n", "language": "javascript" } ] } [/block] ## Run an arbitrary script and assert its output [block:code] { "codes": [ { "code": "cy.exec(\"npm run my-script\").its(\"stdout\").should(\"contain\", \"Done running the script\")\n", "language": "javascript" } ] } [/block] ## Change the timeout [block:code] { "codes": [ { "code": "// will fail if script takes longer than 20 seconds to finish\ncy.exec(\"npm run build\", { timeout: 20000 });\n", "language": "javascript" } ] } [/block] ## Choose not to fail on non-zero exit and assert on code and stderr [block:code] { "codes": [ { "code": "cy\n .exec(\"man bear pig\", { failOnNonZeroExit: false })\n .its(\"code\").should(\"eq\", 1)\n .its(\"stderr\").should(\"contain\", \"No manual entry for bear\")\n", "language": "javascript" } ] } [/block] ## Specify environment variables [block:code] { "codes": [ { "code": "cy\n .exec(\"echo $USERNAME\", { env: { USERNAME: \"johndoe\" } })\n .its(\"stdout\").should(\"contain\", \"johndoe\")\n", "language": "javascript" } ] } [/block] ## Write to a file to create a fixture from response body [block:code] { "codes": [ { "code": "cy\n .server()\n .route(\"POST\", \"/comments\").as(\"postComment\")\n .get(\".add-comment\").click()\n .wait(\"@postComment\").then(function(xhr){\n cy\n .exec(\"echo '\" + JSON.stringify(xhr.responseBody) + \"'>cypress/fixtures/comment.json\")\n .fixture(\"comment.json\").should(\"deep.eq\", xhr.responseBody)\n })\n", "language": "javascript" } ] } [/block] *** # Command Log ## List the contents of cypress.json [block:code] { "codes": [ { "code": "cy.exec(\"cat cypress.json\")\n", "language": "javascript" } ] } [/block] The command above will display in the command log as: <img width="445" alt="screen shot of command log" src="https://cloud.githubusercontent.com/assets/1157043/15369507/e03a7eca-1d00-11e6-8558-396d8c9b6d98.png"> When clicking on the `exec` command within the command log, the console outputs the following: <img width="758" alt="screen shot of console output" src="https://cloud.githubusercontent.com/assets/1157043/15969867/e3ab646e-2eff-11e6-9199-987ca2f74025.png">