{"_id":"5690047121fcf0190071d943","parentDoc":null,"user":"568fffce769f210d0013258f","__v":19,"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"},"project":"568fde81b700ce0d002f4b43","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":["5693270e4c4d6d0d00f7cbdc"],"next":{"pages":[],"description":""},"createdAt":"2016-01-08T18:48:17.061Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"[Read about Making Assertions first.](https://on.cypress.io/guides/making-assertions)\",\n  \"title\": \"New to Cypress?\"\n}\n[/block]\n\n`cy.and` makes chaining together assertions easy.\n\nYou'd typically use `cy.and` when you are making multiple assertions about the same subject.\n\n| | |\n|--- | --- |\n| **Returns** | the current subject but (in some cases) a new subject  |\n| **Timeout** | the assertion will retry for the duration of the [`defaultCommandTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) |\n\n***\n\n# [cy.and( *chainers* )](#section-chainers-usage)\n\nMake an assertion about the current subject using assertion chainers.\n\n***\n\n# [cy.and( *chainers*, *value* )](#section-chainers-with-value-usage)\n\nMake an assertion about the value of the current subject.\n\nSome chai methods and chai-jQuery methods return a new (different) subject for chain-ability.\n\n***\n\n# [cy.and( *chainers*, *method*, *value* )](#section-chainers-with-method-and-value-usage)\n\nMake an assertion about the subject by calling a method and providing a value to that method.\n\n***\n\n# [cy.and( *function* )](#section-function-usage)\n\nPass a function that can have any number of explicit assertions written within it.\n\nDoes not change the subject. Whatever was passed to the function is what is returned.\n\n***\n\n# Chainers Usage\n\n## Chain assertions on the same subject\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.get(\\\"button\\\").should(\\\"have.class\\\", \\\"active\\\").and(\\\"not.be.disabled\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Chainers with Value Usage\n\n## Chain assertions on subject change\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"<!-- App Code -->\\n<ul>\\n  <li>\\n    <a href=\\\"users/123/edit\\\">Edit User</a>\\n  </li>\\n</ul>\\n\",\n            \"language\": \"html\"\n        }\n    ]\n}\n[/block]\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  // subject is now <a>\\n  .get(\\\"a\\\")\\n\\n  // assert <a> contains text: \\\"Edit User\\\"\\n  // subject is still the <a>\\n  .should(\\\"contain\\\", \\\"Edit User\\\")\\n\\n  // assert subject has 'href' attribute\\n  // subject now changes to return value from the 'href' attribute\\n  .and(\\\"have.attr\\\", \\\"href\\\")\\n\\n  // assert that the string returned from 'href'\\n  // matches the RegExp /users/\\n  // the subject is still the same string\\n  .and(\\\"match\\\", /users/)\\n\\n  // assert that the string does not\\n  // have a '#' character within it\\n  .and(\\\"not.include\\\", \\\"#\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Chainers with Method and Value Usage\n\n## Assert the href is equal to '/users'\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// have.attr comes from chai-jquery\\ncy\\n  .get(\\\"#header a\\\")\\n  .should(\\\"have.class\\\", \\\"active\\\")\\n  .and(\\\"have.attr\\\", \\\"href\\\", \\\"/users\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Function Usage\n\n## Verify length, content, and classes from multiple `<p>`\n\nPassing a function to `cy.and` enables you to assert on arbitrary subjects. This gives you the opportunity to *massage* what you'd like to assert on.\n\nJust be sure *not* to include any code that has side effects in your callback function.\n\nThe callback function will be retried over and over again until no assertions within it throw.\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"<div>\\n  <p class=\\\"text-primary\\\">Hello World</p>\\n  <p class=\\\"text-danger\\\">You have an error</p>\\n  <p class=\\\"text-default\\\">Try again later</p>\\n</div>\\n\",\n            \"language\": \"html\"\n        }\n    ]\n}\n[/block]\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .get(\\\"p\\\")\\n  .should(\\\"not.be.empty\\\")\\n  .and(function($p){\\n    // should have found 3 elements\\n    expect($p).to.have.length(3)\\n\\n    // make sure the first contains some text content\\n    expect($p.first()).to.contain(\\\"Hello World\\\")\\n\\n    // use jquery's map to grab all of their classes\\n    // jquery's map returns a new jquery object\\n    var classes = $p.map(function(i, el){\\n      return cy.$(el).attr(\\\"class\\\")\\n    })\\n\\n    // call classes.get() to make this a plain array\\n    expect(classes.get()).to.deep.eq([\\n      \\\"text-primary\\\",\\n      \\\"text-danger\\\",\\n      \\\"text-default\\\"\\n    ])\\n  })\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Using a callback function will not change the subject\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .get(\\\"button\\\")\\n  .should(\\\"be.active\\\")\\n  .and(function($button){\\n    // whatever we return here is ignored\\n    // as Cypress will always force the return\\n    // value for future commands to be the same\\n    // as the previous subject which is <button>\\n\\n    expect({foo: \\\"bar\\\"}).to.deep.eq({foo: \\\"bar\\\"})\\n\\n    // whatever the return value (if any) is ignored\\n    return {foo: \\\"bar\\\"}\\n  })\\n\\n  .then(function($button){\\n    // $button === <button>\\n    // the subject is unchanged no matter what was returned\\n  })\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## Assertions that change the subject\n\nSometimes using a specific chainer will automatically change the assertion subject.\n\nFor instance in `chai`, the method [`have.property(\"...\")`](http://chaijs.com/api/bdd/) will automatically change the subject.\n\nAdditionally in [`Chai-jQuery`](https://github.com/chaijs/chai-jquery#attrname-value), the methods: `attr`, `prop`, `css`, and `data` also change the subject.\n\nThis allows you to utilize other `chainer` methods such as `match` when making assertions about values.\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// in this example our subject changed to the string 'sans-serif' because\\n// have.css(\\\"font-family\\\") returned a string instead of the <body> element\\ncy\\n  // subject is <body>\\n  .get(\\\"body\\\")\\n\\n  // subject changes to the string return value of 'font-family'\\n  .should(\\\"have.css\\\", \\\"font-family\\\")\\n\\n  // use match to assert the string matches a regular expression\\n  .and(\\\"match\\\", /sans-serif/)\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"// in this example our subject changed to the string '/users' because\\n// have.attr, href, /users returned a string instead of the <a> element\\ncy\\n  // subject is <a>\\n  .get(\\\"a\\\")\\n\\n  // subject changes to the string 'users'\\n  .should(\\\"have.attr\\\", \\\"href\\\", \\\"/users\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n# Notes\n\n## Similarities to Chai\n\nIf you've worked in [Chai](http://chaijs.com/) before, you will recognize that `cy.and` matches the same fluent assertion syntax.\n\nTake this *explicit* assertion for example:\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"expect({foo: \\\"bar\\\"}).to.have.property(\\\"foo\\\").and.eq(\\\"bar\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n`cy.and` reproduces this same assertion behavior.\n\n***\n\n## Can I pass options to cy.and()?\n\nOptions passed to the preceding command will be passed through to `cy.and`.\n\nThe following example is an example of increasing the `timeout` of the `cy.and`:\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy\\n  .get(\\\"input\\\", {timeout: 10000}) // <-- wait up to 10 seconds for this 'input' to be found\\n    .should(\\\"have.value\\\", \\\"foo\\\")   // <-- and to have the value 'foo'\\n    .and(\\\"have.class\\\", \\\"radio\\\")    // <-- and to have the class 'radio'\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"cy.find(\\\"input\\\", {timeout: 10000}).should(\\\"have.value\\\", \\\"foo\\\").and(\\\"have.class\\\", \\\"radio\\\")\\n                         ↲\\n      // adding the timeout here will automatically\\n      // flow down to the assertions, and they will\\n      // be retried for up to 10 seconds\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\n***\n\n## How do I know which assertions change the subject and which keep it the same?\n\nThe chainers that come from [Chai](https://on.cypress.io/guides/bundled-tools#section-chai) or [Chai-jQuery](https://on.cypress.io/guides/bundled-tools#section-chai-jquery) will always document what they return.\n\nAlternatively, it is very easy to use Cypress itself to figure this out.\n\nYou can [read more about debugging assertions](https://on.cypress.io/guides/making-assertions#sections-debugging-assertions) here.\n\n***\n\n# Command Log\n\n## Chain assertions on the same subject\n\n[block:code]\n{\n    \"codes\": [\n        {\n            \"code\": \"  .find(\\\"input[type='checkbox']\\\")\\n    .should(\\\"be.checked\\\")\\n    .and(\\\"not.be.disabled\\\")\\n\",\n            \"language\": \"javascript\"\n        }\n    ]\n}\n[/block]\n\nThe commands above will display in the command log as:\n\n<img width=\"530\" alt=\"screen shot 2015-11-29 at 12 16 46 pm\" src=\"https://cloud.githubusercontent.com/assets/1271364/11458700/36d1e646-9693-11e5-8771-158230530fdc.png\">\n\nWhen clicking on `assert` within the command log, the console outputs the following:\n\n<img width=\"636\" alt=\"screen shot 2015-11-29 at 12 17 03 pm\" src=\"https://cloud.githubusercontent.com/assets/1271364/11458702/3b6873be-9693-11e5-88f7-a928ebdac80c.png\">\n\n***\n\n# Related\n\n- [should](https://on.cypress.io/api/should)\n- [Making Assertions](https://on.cypress.io/guides/making-assertions)","excerpt":"Chain multiple assertions together","slug":"and","type":"basic","title":"and"}

and

Chain multiple assertions together

[block:callout] { "type": "info", "body": "[Read about Making Assertions first.](https://on.cypress.io/guides/making-assertions)", "title": "New to Cypress?" } [/block] `cy.and` makes chaining together assertions easy. You'd typically use `cy.and` when you are making multiple assertions about the same subject. | | | |--- | --- | | **Returns** | the current subject but (in some cases) a new subject | | **Timeout** | the assertion will retry for the duration of the [`defaultCommandTimeout`](https://on.cypress.io/guides/configuration#section-timeouts) | *** # [cy.and( *chainers* )](#section-chainers-usage) Make an assertion about the current subject using assertion chainers. *** # [cy.and( *chainers*, *value* )](#section-chainers-with-value-usage) Make an assertion about the value of the current subject. Some chai methods and chai-jQuery methods return a new (different) subject for chain-ability. *** # [cy.and( *chainers*, *method*, *value* )](#section-chainers-with-method-and-value-usage) Make an assertion about the subject by calling a method and providing a value to that method. *** # [cy.and( *function* )](#section-function-usage) Pass a function that can have any number of explicit assertions written within it. Does not change the subject. Whatever was passed to the function is what is returned. *** # Chainers Usage ## Chain assertions on the same subject [block:code] { "codes": [ { "code": "cy.get(\"button\").should(\"have.class\", \"active\").and(\"not.be.disabled\")\n", "language": "javascript" } ] } [/block] *** # Chainers with Value Usage ## Chain assertions on subject change [block:code] { "codes": [ { "code": "<!-- App Code -->\n<ul>\n <li>\n <a href=\"users/123/edit\">Edit User</a>\n </li>\n</ul>\n", "language": "html" } ] } [/block] [block:code] { "codes": [ { "code": "cy\n // subject is now <a>\n .get(\"a\")\n\n // assert <a> contains text: \"Edit User\"\n // subject is still the <a>\n .should(\"contain\", \"Edit User\")\n\n // assert subject has 'href' attribute\n // subject now changes to return value from the 'href' attribute\n .and(\"have.attr\", \"href\")\n\n // assert that the string returned from 'href'\n // matches the RegExp /users/\n // the subject is still the same string\n .and(\"match\", /users/)\n\n // assert that the string does not\n // have a '#' character within it\n .and(\"not.include\", \"#\")\n", "language": "javascript" } ] } [/block] *** # Chainers with Method and Value Usage ## Assert the href is equal to '/users' [block:code] { "codes": [ { "code": "// have.attr comes from chai-jquery\ncy\n .get(\"#header a\")\n .should(\"have.class\", \"active\")\n .and(\"have.attr\", \"href\", \"/users\")\n", "language": "javascript" } ] } [/block] *** # Function Usage ## Verify length, content, and classes from multiple `<p>` Passing a function to `cy.and` enables you to assert on arbitrary subjects. This gives you the opportunity to *massage* what you'd like to assert on. Just be sure *not* to include any code that has side effects in your callback function. The callback function will be retried over and over again until no assertions within it throw. [block:code] { "codes": [ { "code": "<div>\n <p class=\"text-primary\">Hello World</p>\n <p class=\"text-danger\">You have an error</p>\n <p class=\"text-default\">Try again later</p>\n</div>\n", "language": "html" } ] } [/block] [block:code] { "codes": [ { "code": "cy\n .get(\"p\")\n .should(\"not.be.empty\")\n .and(function($p){\n // should have found 3 elements\n expect($p).to.have.length(3)\n\n // make sure the first contains some text content\n expect($p.first()).to.contain(\"Hello World\")\n\n // use jquery's map to grab all of their classes\n // jquery's map returns a new jquery object\n var classes = $p.map(function(i, el){\n return cy.$(el).attr(\"class\")\n })\n\n // call classes.get() to make this a plain array\n expect(classes.get()).to.deep.eq([\n \"text-primary\",\n \"text-danger\",\n \"text-default\"\n ])\n })\n", "language": "javascript" } ] } [/block] *** ## Using a callback function will not change the subject [block:code] { "codes": [ { "code": "cy\n .get(\"button\")\n .should(\"be.active\")\n .and(function($button){\n // whatever we return here is ignored\n // as Cypress will always force the return\n // value for future commands to be the same\n // as the previous subject which is <button>\n\n expect({foo: \"bar\"}).to.deep.eq({foo: \"bar\"})\n\n // whatever the return value (if any) is ignored\n return {foo: \"bar\"}\n })\n\n .then(function($button){\n // $button === <button>\n // the subject is unchanged no matter what was returned\n })\n", "language": "javascript" } ] } [/block] *** ## Assertions that change the subject Sometimes using a specific chainer will automatically change the assertion subject. For instance in `chai`, the method [`have.property("...")`](http://chaijs.com/api/bdd/) will automatically change the subject. Additionally in [`Chai-jQuery`](https://github.com/chaijs/chai-jquery#attrname-value), the methods: `attr`, `prop`, `css`, and `data` also change the subject. This allows you to utilize other `chainer` methods such as `match` when making assertions about values. [block:code] { "codes": [ { "code": "// in this example our subject changed to the string 'sans-serif' because\n// have.css(\"font-family\") returned a string instead of the <body> element\ncy\n // subject is <body>\n .get(\"body\")\n\n // subject changes to the string return value of 'font-family'\n .should(\"have.css\", \"font-family\")\n\n // use match to assert the string matches a regular expression\n .and(\"match\", /sans-serif/)\n", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "// in this example our subject changed to the string '/users' because\n// have.attr, href, /users returned a string instead of the <a> element\ncy\n // subject is <a>\n .get(\"a\")\n\n // subject changes to the string 'users'\n .should(\"have.attr\", \"href\", \"/users\")\n", "language": "javascript" } ] } [/block] *** # Notes ## Similarities to Chai If you've worked in [Chai](http://chaijs.com/) before, you will recognize that `cy.and` matches the same fluent assertion syntax. Take this *explicit* assertion for example: [block:code] { "codes": [ { "code": "expect({foo: \"bar\"}).to.have.property(\"foo\").and.eq(\"bar\")\n", "language": "javascript" } ] } [/block] `cy.and` reproduces this same assertion behavior. *** ## Can I pass options to cy.and()? Options passed to the preceding command will be passed through to `cy.and`. The following example is an example of increasing the `timeout` of the `cy.and`: [block:code] { "codes": [ { "code": "cy\n .get(\"input\", {timeout: 10000}) // <-- wait up to 10 seconds for this 'input' to be found\n .should(\"have.value\", \"foo\") // <-- and to have the value 'foo'\n .and(\"have.class\", \"radio\") // <-- and to have the class 'radio'\n", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "cy.find(\"input\", {timeout: 10000}).should(\"have.value\", \"foo\").and(\"have.class\", \"radio\")\n ↲\n // adding the timeout here will automatically\n // flow down to the assertions, and they will\n // be retried for up to 10 seconds\n", "language": "javascript" } ] } [/block] *** ## How do I know which assertions change the subject and which keep it the same? The chainers that come from [Chai](https://on.cypress.io/guides/bundled-tools#section-chai) or [Chai-jQuery](https://on.cypress.io/guides/bundled-tools#section-chai-jquery) will always document what they return. Alternatively, it is very easy to use Cypress itself to figure this out. You can [read more about debugging assertions](https://on.cypress.io/guides/making-assertions#sections-debugging-assertions) here. *** # Command Log ## Chain assertions on the same subject [block:code] { "codes": [ { "code": " .find(\"input[type='checkbox']\")\n .should(\"be.checked\")\n .and(\"not.be.disabled\")\n", "language": "javascript" } ] } [/block] The commands above will display in the command log as: <img width="530" alt="screen shot 2015-11-29 at 12 16 46 pm" src="https://cloud.githubusercontent.com/assets/1271364/11458700/36d1e646-9693-11e5-8771-158230530fdc.png"> When clicking on `assert` within the command log, the console outputs the following: <img width="636" alt="screen shot 2015-11-29 at 12 17 03 pm" src="https://cloud.githubusercontent.com/assets/1271364/11458702/3b6873be-9693-11e5-88f7-a928ebdac80c.png"> *** # Related - [should](https://on.cypress.io/api/should) - [Making Assertions](https://on.cypress.io/guides/making-assertions)