Home > Net >  How can I user regex in cy.get()?
How can I user regex in cy.get()?

Time:03-01

The application has list of items in a table and items have [id=item[0].setupCost, id=item[1].setupCost, id=item[2].setupCost] etc.

There's a functionality to add items also, so the index keeps on increasing.

I want to get input field regardless of using magic numbers. For eg (cy.get('[id=item[some_regex].setupCost]')

CodePudding user response:

Instead of Regex you can use ^ with your selector. This denotes the start of the text. So you can write:

cy.get('[id^="item"]')

CodePudding user response:

You could create a custom Cypress command that would insert the index for you. Below, I'm assuming the id you're referencing is the ID of the element and is a standard CSS selector

Cypress.Commands.add('getItemById', (index) => {
  return cy.get(`#item[${index}].setupCost`)
});


cy.getItemById(0)...
  • Related