Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use Cypress functions on variables

I’m looping throw all of the .row and need to grab the inner elements within .row. Is there a way to do this? I wasn’t able to find any documentation on this on Cypress’ website.

let num = 0
cy.get('.row').each(row => { 
  let rowDiv = cy.get(row).get('div') // not correct
  let rowBtn = cy.get(row).get('button') // not correct

  cy.get(rowDiv).should('have.text', 'Task ' + num)
  cy.get(rowBtn).should('have.text', "Btn ' + num)

  num++
})

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use cy.wrap() to turn the yielded JQuery elements back into Cypress Chainables. Additionally, cy.each() can yield you the index of the current iteration, so instead of having to use num, you could just use that index variable.

cy.get('.row')
  .each((row, rowIndex) => { // yields JQuery element and current iteration
    cy.wrap(row).find('div').should('have.text', `Task ${rowIndex}`);
    cy.wrap(row).find('button').should('have.text', `Btn ${rowIndex}`);
  });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading