Cypress contains exact match

The “contains” selector in Cypress is used to select elements that contain a specific text. By default, this selector performs a partial match, meaning it selects elements that contain the given text anywhere within their content. However, if you want to perform an exact match using the “contains” selector, you can achieve this by using the ‘exact’ option.

To use the ‘exact’ option in Cypress, you can pass it as an argument to the “contains” selector. Here’s an example:

      
         cy.contains('button', 'Click Me', { matchCase: false, exact: true });
      
   

In this example, the “contains” selector is used to select a button element that has the exact text “Click Me”. The ‘exact’ option is set to “true”, ensuring that only elements with an exact match are selected. The ‘matchCase’ option is set to “false” to ignore the case sensitivity (e.g., “click me” or “Click Me” will both be selected).

Keep in mind that the “contains” selector with the ‘exact’ option can be used with any HTML element and any text content within that element. Adjust the element tag and the text accordingly in your actual code.

Read more interesting post

Leave a comment