Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

486
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda