Cypress: Using Variable in cy.get

Viewed 27

I want to get different elements with names listed in an array

const List = ['Age', 'Name', 'Creation']

cy.get('*[value^=${List[0]}]').should('not.have.class','errand-cui')
cy.get('*[value^=${List[1]}]').should('not.have.class','errand-cui')

This doesn't work. Is that even possible the way I try this?

1 Answers

Template literals use backticks in JavaScript. The following should work:

cy.get(`*[value^="${List[0]}"]`).should('not.have.class', 'errand-cui');

Additionally, you could use .each() to iterate through all elements if they have a common selector.

cy.get('foo') // common selector for the elements
  .each(($el) => {
    cy.wrap($el).should('not.have.class', 'errand-cui');
  });
Related