Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that element has either of classes in Cypress?

We search for an invalid element as following:

const invalidClasses = '.invalid, .invalid-default';

getInvalidElement() {
    cy.get(invalidClasses)
};

Now I have another function which accepts the element and checks if it has the invalid classes:

isInvalid(selector) {
 return cy.get(selector).should('have.class','invalid');
}

How can I check that the element has any of the two classes?

I know I can do

cy.get(selector).invoke('attr','class').should('match','/invalid/');

But what if the classes were different?

(Also the conditional testing does not apply to this case, there is no logic whether it's the first of the classes or the second one, we just want more abstract class for reusing)

like image 553
MCFreddie777 Avatar asked May 25 '20 14:05

MCFreddie777


People also ask

How do you know if an element is displayed or not in Cypress?

Tip: if a Cypress test fails with "element is not visible" error, but you are sure the element should be visible, you can debug the visibility check yourself by stepping through the Cypress. dom. isVisible code, see Debug the Element Visibility Problems in Cypress.


1 Answers

I guess it will works for you:

cy.get('section')
.should('have.class', 'container')
like image 64
Mohammad Jamal Dashtaki Avatar answered Sep 22 '22 08:09

Mohammad Jamal Dashtaki