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

482
Visualizações
Cypress - get value that is inside a component (into a variable)

I'm a new developer and I am trying to build a certain test using cypress. the client has a dynamic grid, and I'd like to see which row in the grid contains the data I want. I cannot change the HTML file.

At the moment I am trying to iterate over all rows using each() cypress command. So I have two questions:

  1. is this a correct approach?
  2. assuming it is correct, how do I get the value out of the component? for example, how do I get the value of "aaaaaaaa" out of here (and into a variable): html structure

thanks!

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

0

Using each is a pretty good approach, select the rows and make use of the index parameter to find which one.

cy.get('[role="row"]').each(($row, index) => {

  if ($row.find('span').text() === 'aaaa') {
    cy.wrap(index).as('rowIndex')
})

cy.get('@rowIndex')
  .then(rowIndex => {
    // use the row index here
  })

There's also this method Cypress get index of th element to use it later.

If it works (the jQuery docs don't show this usage) the code for your HTML would be

cy.contains('[role="row"]', 'aaaa').invoke('index').as('rowIndex')

cy.get('@rowIndex')
  .then(rowIndex => {
    // use the row index here
  })
about 4 years ago · Juan Pablo Isaza Relatório

0

I have a test doing the same thing and the way I've approached it was:

cy.contains('aaaa').invoke("text").as('variableName') 

This can now be used like this:

cy.get(this.variableName).should('be.visible')

cy.contains('aaaa').parent().next().children() //Used to iterate through rows.
about 4 years ago · Juan Pablo Isaza Relatório

0

There are a number of approaches you can take, but I think my preferred one is to store the variable in Cypress environment variable. In the following, I get the element, then use a .then() statement to get the text of the element (with the JQuery function, .text()), and store it in a Cypress environment variable (via Cypress.env()).

describe('tests', () => {
  it('test', () => {
    cy.get('some-element').then(($el) => {
      Cypress.env('myVar', $el.text());
    })
    ... // some code
    // When needing the variable, reference it by Cypress.env('myVar');
   });

  afterEach(() => {
    // after each test, clear the variable
    Cypress.env('myVar', null)
  });
});

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