Match assertion

If the current subject is a jQuery element, the match assertion uses jQuery .isopen in new window method to check if the element matches the selector. If the current subject is text, the match assertion checks it against the given regular expression.

Watch the video Using Should Match Assertion Against Elements Or Textopen in new window or continue reading.

Match jQuery element selector

<div id="one" class="btn btn-large">First</div>
cy.contains('div', 'First')
  // the current subject should match the selector
  // "#one.btn-large"
  .should('match', '#one.btn-large')
  // we can use other jQuery selectors like
  // :contains to confirm the text
  .and('match', 'div:contains("First")')

Match the text using regular expression

<div id="fruit">Orange</div>
// using
cy.contains('#fruit', /(orange|apple|grape)/i)
// using the "match" assertion
  .should('match', /orange/i)

Match url using regular expression

I love matching URL using a regular expression. For example, if we are redirected to the item's URL at /product/p<id>, then we can strictly confirm it using cy.locationopen in new window command

// the current page looks like /product/p1234
cy.location('pathname').should('match', \/product\/p\d+$/)