https://cypress.io logo
Conditionally run commands/assertions based on a p...
# i-need-help
h
I want a test to run and succeed in two versions (legacy/rewrite) of an app I’m working on. The problem is that some things are necessarily implemented differently. How can I run a different command/assertion depending on a property of some element? Here’s what it might look like (but doesn’t work so far):
Copy code
js
let genreF = cy.get("[data-cy=genre-f]");

if (genreF.its('tagName') === "DIV") {
   // New app
   genreF.parent().should('have.attr', 'aria-checked', 'true')
} else {
   // Legacy app
   genreF.should('be.checked')
}
I feel like it might be impossible this way. I just realized I can simply pass in a variable indicating in which context I am. A sort of environment variable, but just a function parameter, since I’m already in the context of a function called by two separate test files. Would that be a more sensible approach?
Marking that as done since I solved it using the above approach.
h
Looks exactly like what I needed. Thanks a lot, I’ll try some of these, it might be more elegant than what I have now.