--- title: and comments: true description: '' --- {% note info New to Cypress? %} [Read about Making Assertions first.](https://on.cypress.io/guides/making-assertions) {% endnote %} `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#timeouts) | # [cy.and( *chainers* )](#chainers-usage) Make an assertion about the current subject using assertion chainers. # [cy.and( *chainers*, *value* )](#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* )](#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* )](#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 ```javascript cy.get("button").should("have.class", "active").and("not.be.disabled") ``` # Chainers with Value Usage ## Chain assertions on subject change ```html ``` ```javascript cy // subject is now .get("a") // assert contains text: "Edit User" // subject is still the .should("contain", "Edit User") // assert subject has 'href' attribute // subject now changes to return value from the 'href' attribute .and("have.attr", "href") // assert that the string returned from 'href' // matches the RegExp /users/ // the subject is still the same string .and("match", /users/) // assert that the string does not // have a '#' character within it .and("not.include", "#") ``` # Chainers with Method and Value Usage ## Assert the href is equal to '/users' ```javascript // have.attr comes from chai-jquery cy .get("#header a") .should("have.class", "active") .and("have.attr", "href", "/users") ``` # Function Usage ## Verify length, content, and classes from multiple `

` 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. ```html

Hello World

You have an error

Try again later

``` ```javascript cy .get("p") .should("not.be.empty") .and(function($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 var classes = $p.map(function(i, el){ return cy.$(el).attr("class") }) // call classes.get() to make this a plain array expect(classes.get()).to.deep.eq([ "text-primary", "text-danger", "text-default" ]) }) ``` ## Using a callback function will not change the subject ```javascript cy .get("button") .should("be.active") .and(function($button){ // whatever we return here is ignored // as Cypress will always force the return // value for future commands to be the same // as the previous subject which is