In Cypress, to get the classes of an element, you can use the `.invoke()` command to access the DOM element and retrieve its `className` property, which contains a list of classes applied to the element. Here’s an example:

// Get the element
cy.get('.your-element-selector').invoke('attr', 'class').then((classes) => {
  // 'classes' is a space-separated string of class names applied to the element
  console.log(classes);
});

In the code above:

  1. Use `cy.get(‘.your-element-selector’)` to select the element you want to get the classes from. Replace `’.your-element-selector’` with your specific selector.
  2. Use `.invoke(‘attr’, ‘class’)` to invoke the ‘attr’ method on the selected element, which retrieves the ‘class’ attribute of the element.
  3. Use `.then()` to handle the result. The retrieved class names will be a space-separated string of class names.

You can then manipulate or check the class names as needed, whether you want to check if a specific class exists or perform other actions based on the classes.

Support On Demand!

                                         
QA Automation