1

I am trying to get all the parameters in my upcoming course card div. It has course information, course data, course name, teacher's name and enroll button. I tried the following code and it prints 1, 1+2, 1+2+3, 1+2+3+4. Instead of repetition how can I just have 1 2 3 4?

Then('validate upcoming coursecard all parameters',()=>{
    var a = []
    cy.get('.upcoming_course_card')
    .each(($li, index, $lis) => {
        a.push($li[0].innerText)
        console.log(a)
      }) 
})
yivi
  • 42,438
  • 18
  • 116
  • 138

1 Answers1

0

I'm not sure I understand the question correctly:

  1. Assuming you want to output exactly the element that is pushed into the array in each iteration, then use the index to achieve that like:

Then('validate upcoming coursecard all parameters',()=>{
    let a = []
    cy.get('.upcoming_course_card')
    .each(($li, index, $lis) => {
        a.push($li[0].innerText)
        console.log(a[index])
      }) 
})
  1. Assuming you just want to have a summary log statement at the end that prints out all the contents of your array, then you just need to place the log statement outside the loop:

Then('validate upcoming coursecard all parameters', () => {
  let a = [];
  cy.get('.upcoming_course_card').each(($li, index, $lis) => {
    a.push($li[0].innerText);
  });
  console.log(a);
});

And as a tip, think about using let instead of var. You can find details about this in this post for example.

Sebastiano Schwarz
  • 1,060
  • 2
  • 13
  • 32