{"__v":0,"_id":"56995ac8cb127f0d003cc165","category":{"__v":50,"_id":"5697efe48d2a770d00d2fd16","pages":["5697f9021c4dc823005426a7","5697f9a7d231880d00676f88","5697f9a72cf4060d004eaa98","5697f9a71c4dc823005426a9","5697f9a82cf4060d004eaa99","5697f9a82cf4060d004eaa9a","5697f9a81c4dc823005426aa","5697f9a869393517000c8451","5697f9a73503e40d0061f4e1","5697f9a869393517000c8452","5697f9a92cf4060d004eaa9b","5697f9a88560a60d00e2c398","5697f9a81c4dc823005426ab","5697fe158560a60d00e2c3a6","5697fe15d231880d00676f8e","56995ac8cb127f0d003cc165","56995ac854d7db17000ee9e5","56995ac8113a901900ef8f78","56995ac8113a901900ef8f77","56a0ff2a8b2e6f0d0018e94b","56ab8b29c159aa0d0070de3d","56ab8b290b9e0b0d00616230","56ab8b294e8ba20d006a2377","56ab8b2935138c0d005c944e","56ab8b2af25f160d00e17ef8","56ab8b624e4b730d009eb09e","56ab8b62f25f160d00e17ef9","56ab8b62d4432d1900eed1b0","56ab8b62678b58170031ebb2","56ab8b62678b58170031ebb1","56ab8bf435138c0d005c9450","56ab8bf44e4b730d009eb0a1","56ab8bf435138c0d005c9451","56ab8bf5f9757e0d007c65e7","56ab8bf58beeff0d003b809a","56ab8c3ef25f160d00e17efc","56ab8c3f4e8ba20d006a237a","56ab8c3f8beeff0d003b809b","56ab8c3f4e4b730d009eb0a2","56ab8c3f0b9e0b0d00616234","56ab8c704e4b730d009eb0a3","56ab8c709327b30d00f7c1f1","56ab8c714e8ba20d006a237c","56ab8c71f9757e0d007c65e9","56ab8c71f9757e0d007c65ea","56ab8ce8678b58170031ebbd","56ab8ce78beeff0d003b809d","56ab8ce70b9e0b0d00616237","56ab8ce84e4b730d009eb0a4","56ab8ce84e8ba20d006a237d"],"project":"568fde81b700ce0d002f4b43","version":"568fde82b700ce0d002f4b46","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-01-14T18:58:44.771Z","from_sync":false,"order":3,"slug":"cypress-api","title":"Cypress API"},"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-15T20:47:04.295Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"Cypress comes with its own API for creating custom commands. In fact, the same public methods *you* have access to are the same ones we use to create all of the built in commands. In other words, there's nothing special or different about ours versus yours. You can customize every aspect of commands, not only their behavior, but also their display in the Command Log.\n\nThis allows you to build up specific commands for your application which take their own custom arguments, and perform their own custom behavior.\n\nFor example, the first custom command you'll probably create is the canonical `login` command. This typically would navigate the user to your `/login` url, fill out a username / password combination, submit the form, and then assert that the dashboard page comes up (or whatever happens upon successful login).\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"A great place to define these commands is in your `cypress/support/commands.js` file, since it is loaded before any test files are evaluated.\"\n}\n[/block]\n\n# [Cypress.addChildCommand()]()\n\nChild commands are always chained off of a **parent** command, or another **child** command.\n\n***\n\n# [Cypress.addDualCommand]()\n\nWhile parent commands always start a new chain of commands and child commands require being chained off a parent command, dual commands can behave as parent or child command. That is, they can **start** a new chain, or be chained off of an **existing** chain.\n\n***\n\n# [Cypress.addParentCommand](#add-parent-command-usage)\n\nParent commands always **begin** a new chain of commands. Even if you've written a previous chain, parent commands will always start a new chain, and ignore previous chains.\n\n***\n\n# Add Parent Command Usage\n\n## Custom command for 'login'\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"Cypress.addParentCommand(\\\"login\\\", function(email, password){\\n  var email    = email || \\\"joe:::at:::example.com\\\"\\n  var password = password || \\\"foobar\\\"\\n\\n  var log = Cypress.Log.command({\\n    name: \\\"login\\\",\\n    message: [email, password],\\n    onConsole: function(){\\n      return {\\n        email: email,\\n        password: password\\n      }\\n    }\\n  })\\n\\n  cy\\n    .visit(\\\"/login\\\", {log: false})\\n    .contains(\\\"Log In\\\", {log: false})\\n    .get(\\\"#email\\\", {log: false}).type(email, {log: false})\\n    .get(\\\"#password\\\", {log: false}).type(password, {log: false})\\n    .get(\\\"button\\\", {log: false}).click({log: false}) //this should submit the form\\n    .get(\\\"h1\\\", {log: false}).contains(\\\"Dashboard\\\", {log: false}) //we should be on the dashboard now\\n    .url({log: false}).should(\\\"match\\\", /dashboard/, {log: false})\\n    .then(function(){\\n      log.snapshot().end()\\n    })\\n})\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]","excerpt":"Commands API","slug":"commands","type":"basic","title":"Commands"}

Commands

Commands API

Cypress comes with its own API for creating custom commands. In fact, the same public methods *you* have access to are the same ones we use to create all of the built in commands. In other words, there's nothing special or different about ours versus yours. You can customize every aspect of commands, not only their behavior, but also their display in the Command Log. This allows you to build up specific commands for your application which take their own custom arguments, and perform their own custom behavior. For example, the first custom command you'll probably create is the canonical `login` command. This typically would navigate the user to your `/login` url, fill out a username / password combination, submit the form, and then assert that the dashboard page comes up (or whatever happens upon successful login). [block:callout] { "type": "info", "body": "A great place to define these commands is in your `cypress/support/commands.js` file, since it is loaded before any test files are evaluated." } [/block] # [Cypress.addChildCommand()]() Child commands are always chained off of a **parent** command, or another **child** command. *** # [Cypress.addDualCommand]() While parent commands always start a new chain of commands and child commands require being chained off a parent command, dual commands can behave as parent or child command. That is, they can **start** a new chain, or be chained off of an **existing** chain. *** # [Cypress.addParentCommand](#add-parent-command-usage) Parent commands always **begin** a new chain of commands. Even if you've written a previous chain, parent commands will always start a new chain, and ignore previous chains. *** # Add Parent Command Usage ## Custom command for 'login' [block:code] { "codes": [ { "code": "Cypress.addParentCommand(\"login\", function(email, password){\n var email = email || \"joe@example.com\"\n var password = password || \"foobar\"\n\n var log = Cypress.Log.command({\n name: \"login\",\n message: [email, password],\n onConsole: function(){\n return {\n email: email,\n password: password\n }\n }\n })\n\n cy\n .visit(\"/login\", {log: false})\n .contains(\"Log In\", {log: false})\n .get(\"#email\", {log: false}).type(email, {log: false})\n .get(\"#password\", {log: false}).type(password, {log: false})\n .get(\"button\", {log: false}).click({log: false}) //this should submit the form\n .get(\"h1\", {log: false}).contains(\"Dashboard\", {log: false}) //we should be on the dashboard now\n .url({log: false}).should(\"match\", /dashboard/, {log: false})\n .then(function(){\n log.snapshot().end()\n })\n})\n", "language": "javascript" } ] } [/block]