Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

490
Visualizações
How can I user regex in cy.get()?

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]')

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

In case you need another answer:

You can use a regex if you add a jQuery extension.

It works because Cypress uses jQuery internally, and you can modify selector behavior by extending jQuery.

it('finds multiple ids by regex', () => {

  const $ = Cypress.$
  $.extend(
    $.expr[':'], {
      idRegex: function(a, i, m) {
        const regex = new RegExp(m[3], 'i');
        return regex.test(a.getAttribute('id'));
      }
    }
  )

  const regex = 'item\\[\\d+\\].setupCost' 

  cy.get(`input:idRegex(${regex})`)
    .its('length')
    .should('eq', 3)   // ✅
})

Put the jQuery extension in /cypress/support/index.js to use it globally.

Note in the regex we have to double-escaped because we pass in a string not a regex.

about 4 years ago · Juan Pablo Isaza Relatório

0

The regex that applies is \[id="item\[\d+\].setupCost\].

Enclose the regex in forward slashes, not quotes.

cy.get(/\[id="item\[\d+\].setupCost\]/)

But don't use it

This syntax is undocumented - it works (in Cypress v9.5.0) but it only returns one result.

So if you want to count your items, this will fail

cy.get(/\[id="item\[\d+\].setupCost\]/)
  .its('length')
  .should('eq', 3)   // ❌

Partial attribute selector

If you want to use partial attribute selectors, this is strongest as it includes .setupCost

Ref Find the element with id that starts with "local-" and ends with "-remote"

cy.get('[id^="item"][id$=".setupCost"]')  // use "starts-with" and "ends-with" selectors

This succeeds with the count test

cy.get('[id^="item"][id$=".setupCost"]')
  .its('length')
  .should('eq', 3)   // ✅
about 4 years ago · Juan Pablo Isaza Relatório

0

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)...
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda