{
  "doc": {
    "id": "api/commands/and",
    "title": "and | Cypress Documentation",
    "description": "Create an assertion. Assertions are automatically retried as part of the previous command until they pass or time out.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/and.md",
    "version": "3cf5b86b3403f604bdf7f3e35025c3bc3865e02c",
    "updated_at": "2026-05-07T17:44:31.931Z",
    "headings": [
      {
        "id": "api/commands/and#and",
        "text": "and",
        "level": 1
      },
      {
        "id": "api/commands/and#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/and#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/and#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/and#yields-learn-about-subject-management",
        "text": "Yields Learn about subject management",
        "level": 3
      },
      {
        "id": "api/commands/and#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/and#chainers",
        "text": "Chainers",
        "level": 3
      },
      {
        "id": "api/commands/and#chain-assertions-on-the-same-subject",
        "text": "Chain assertions on the same subject",
        "level": 4
      },
      {
        "id": "api/commands/and#value",
        "text": "Value",
        "level": 3
      },
      {
        "id": "api/commands/and#chain-assertions-when-yield-changes",
        "text": "Chain assertions when yield changes",
        "level": 4
      },
      {
        "id": "api/commands/and#method-and-value",
        "text": "Method and Value",
        "level": 3
      },
      {
        "id": "api/commands/and#assert-the-href-is-equal-to-users",
        "text": "Assert the href is equal to '/users'",
        "level": 4
      },
      {
        "id": "api/commands/and#function",
        "text": "Function",
        "level": 3
      },
      {
        "id": "api/commands/and#verify-length-content-and-classes-from-multiple-p",
        "text": "Verify length, content, and classes from multiple <p>",
        "level": 4
      },
      {
        "id": "api/commands/and#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/and#chai",
        "text": "Chai",
        "level": 3
      },
      {
        "id": "api/commands/and#similarities-to-chai",
        "text": "Similarities to Chai",
        "level": 4
      },
      {
        "id": "api/commands/and#subjects",
        "text": "Subjects",
        "level": 3
      },
      {
        "id": "api/commands/and#how-do-i-know-which-assertions-change-the-subject-and-which-keep-it-the-same",
        "text": "How do I know which assertions change the subject and which keep it the same?",
        "level": 4
      },
      {
        "id": "api/commands/and#using-a-callback-function-will-not-change-what-is-yielded",
        "text": "Using a callback function will not change what is yielded",
        "level": 4
      },
      {
        "id": "api/commands/and#differences",
        "text": "Differences",
        "level": 3
      },
      {
        "id": "api/commands/and#whats-the-difference-between-then-and-should-and",
        "text": "What's the difference between .then() and .should()/.and()?",
        "level": 3
      },
      {
        "id": "api/commands/and#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/and#requirements-learn-about-chaining-commands",
        "text": "Requirements Learn about chaining commands",
        "level": 3
      },
      {
        "id": "api/commands/and#timeouts-learn-about-timeouts",
        "text": "Timeouts Learn about timeouts",
        "level": 3
      },
      {
        "id": "api/commands/and#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/and#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "and"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Create an assertion. Assertions are automatically retried as part of the previous command until they pass or time out."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "An alias of "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/should.md",
            "children": [
              {
                "type": "text",
                "value": "`.should()`"
              }
            ]
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Note: `.and()` assumes you are already familiar with core concepts such as "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions",
            "children": [
              {
                "type": "text",
                "value": "assertions"
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Syntax"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": ".and(chainers).and(chainers, value).and(chainers, method, value).and(callbackFn)"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Usage"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Correct Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('.err').should('be.empty').and('be.hidden') // Assert '.err' is empty & hiddency.contains('Login').and('be.visible') // Assert el is visiblecy.wrap({ foo: 'bar' })  .should('have.property', 'foo') // Assert 'foo' property exists  .and('eq', 'bar') // Assert 'foo' property is 'bar'"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Incorrect Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.and('eq', '42') // Can not be chained off 'cy'cy.get('button').click().and('be.focused') // Should not be chained off// action commands that may update the DOM"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Arguments"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "chainers (String)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Any valid chainer that comes from "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/references/assertions.md#Chai",
            "children": [
              {
                "type": "text",
                "value": "Chai"
              }
            ]
          },
          {
            "type": "text",
            "value": " or "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/references/assertions.md#Chai-jQuery",
            "children": [
              {
                "type": "text",
                "value": "Chai-jQuery"
              }
            ]
          },
          {
            "type": "text",
            "value": " or "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/references/assertions.md#Sinon-Chai",
            "children": [
              {
                "type": "text",
                "value": "Sinon-Chai"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "value (String)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Value to assert against chainer."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "method (String)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "A method to be called on the chainer."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "callbackFn (Function)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Pass a function that can have any number of explicit assertions within it. Whatever was passed to the function is what is yielded."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Yields "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management",
            "children": [
              {
                "type": "text",
                "value": "Learn about subject management"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "In most cases, `.and()` yields the same subject it was given."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.and()` is an assertion, and it is safe to chain further commands that use the subject."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('nav') // yields <nav>  .should('be.visible') // yields <nav>  .and('have.class', 'open') // yields <nav>"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "However, some chainers change the subject. In the example below, `.and()` yields the string `sans-serif` because the chainer `have.css, 'font-family'` changes the subject."
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('nav') // yields <nav>  .should('be.visible') // yields <nav>  .and('have.css', 'font-family') // yields 'sans-serif'  .and('match', /serif/) // yields 'sans-serif'"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Examples"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Chainers"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Chain assertions on the same subject"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('button').should('have.class', 'active').and('not.be.disabled')"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Value"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Chain assertions when yield changes"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "{/* App Code */}<ul>  <li>    <a href=\"users/123/edit\">Edit User</a>  </li></ul>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('a')  .should('contain', 'Edit User') // yields <a>  .and('have.attr', 'href') // yields string value of href  .and('match', /users/) // yields string value of href  .and('not.include', '#') // yields string value of href"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Method and Value"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Assert the href is equal to '/users'"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('#header a')  .should('have.class', 'active')  .and('have.attr', 'href', '/users')"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Function"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Verify length, content, and classes from multiple `<p>`"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Passing a function to `.and()` enables you to assert on the yielded subject. This gives you the opportunity to massage what you'd like to assert."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Be sure not to include any code that has side effects in your callback function."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The callback function will be retried over and over again until no assertions within it throw."
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "<div>  <p class=\"text-primary\">Hello World</p>  <p class=\"text-danger\">You have an error</p>  <p class=\"text-default\">Try again later</p></div>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('p')  .should('not.be.empty')  .and(($p) => {    // should have found 3 elements    expect($p).to.have.length(3)    // make sure the first contains some text content    expect($p.first()).to.contain('Hello World')    // use jquery's map to grab all of their classes    // jquery's map returns a new jquery object    const classes = $p.map((i, el) => {      return Cypress.$(el).attr('class')    })    // call classes.get() to make this a plain array    expect(classes.get()).to.deep.eq([      'text-primary',      'text-danger',      'text-default',    ])  })"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Using a callback function "
          },
          {
            "type": "link",
            "title": null,
            "url": "#Subjects",
            "children": [
              {
                "type": "text",
                "value": "will not change the subject"
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Notes"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Chai"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Similarities to Chai"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "If you've worked in "
          },
          {
            "type": "link",
            "title": null,
            "url": "http://chaijs.com/",
            "children": [
              {
                "type": "text",
                "value": "Chai"
              }
            ]
          },
          {
            "type": "text",
            "value": " before, you will recognize that `.and()` matches the same fluent assertion syntax."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Take this explicit assertion for example:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "expect({ foo: 'bar' }).to.have.property('foo').and.eq('bar')"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "`.and()` reproduces this same assertion behavior."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Subjects"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "How do I know which assertions change the subject and which keep it the same?"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The chainers that come from "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/references/bundled-libraries.md#Chai",
            "children": [
              {
                "type": "text",
                "value": "Chai"
              }
            ]
          },
          {
            "type": "text",
            "value": " or "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/references/bundled-libraries.md#Chai-jQuery",
            "children": [
              {
                "type": "text",
                "value": "Chai-jQuery"
              }
            ]
          },
          {
            "type": "text",
            "value": " will always document what they return."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Using a callback function will not change what is yielded"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Whenever you use a callback function, its return value is always ignored. Cypress always forces the command to yield the value from the previous cy command's yield (which in the example below is `<button>`)"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('button')  .should('be.active')  .and(($button) => {    expect({ foo: 'bar' }).to.deep.eq({ foo: 'bar' })    return { foo: 'bar' } // return is ignored, .and() yields <button>  })  .then(($button) => {    // do anything we want with <button>  })"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Differences"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "What's the difference between `.then()` and `.should()`/`.and()`?"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Using `.then()` allows you to use the yielded subject in a callback function and should be used when you need to manipulate some values or do some actions."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "When using a callback function with `.should()` or `.and()`, on the other hand, there is special logic to rerun the callback function until no assertions throw within it. You should be careful of side affects in a `.should()` or `.and()` callback function that you would not want performed multiple times."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Rules"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Requirements "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands",
            "children": [
              {
                "type": "text",
                "value": "Learn about chaining commands"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.and()` requires being chained off a previous command."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Timeouts "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts",
            "children": [
              {
                "type": "text",
                "value": "Learn about timeouts"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.and()` will continue to "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/retry-ability.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "retry"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": " its specified assertions until it times out."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// timeout here will be passed down to the '.and()'// and it will retry for up to 10 secscy.get('input', { timeout: 10000 })  .should('have.value', '10')  .and('have.class', 'error')"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// timeout here will be passed down to the '.and()'// unless an assertion throws earlier,// ALL of the assertions will retry for up to 10 secscy.get('input', { timeout: 10000 })  .should('have.value', 'US')  .and(($input) => {    expect($input).to.not.be('disabled')    expect($input).to.not.have.class('error')  })"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Command Log"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Chain assertions on the same subject"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('.list')  .find('input[type=\"checkbox\"]')  .should('be.checked')  .and('not.be.disabled')"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The commands above will display in the Command Log as:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "When clicking on `assert` within the command log, the console outputs the following:"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "See also"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/should.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`.should()`"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions",
                    "children": [
                      {
                        "type": "text",
                        "value": "Guide: Introduction to Cypress"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/assertions.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "Reference: List of Assertions"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "token_estimate": 1212
}