Search code examples
javascriptjqueryarraysattributescypress

Access Current Subject which can be reused using Cypress?


I have few elements which appear on the page with a data-testid attribute.

enter image description here

The attribute has values which define what kind of element it is. I want to use the attribute value as a string and want to push it into an array.

What I am doing is -


        cy.xpath(NODE_PREVIEW_PANEL)
             .children(NODE_TYPE)
             .each((el) => {
                 orderArray.push(cy.get(el).invoke('attr', 'data-testid'));
             })

but it isn't adding anything to the array, but the assertions

cy.get(el).invoke('attr', 'data-testid').should('eq', 'Single Column sm')

are working fine.


Solution

  • You can do something like this, to get the value of your desired attribute and save it in an array:

    cy.xpath(NODE_PREVIEW_PANEL)
      .children(NODE_TYPE)
      .each((el) => {
        cy.wrap(el).invoke('attr', 'data-testid').then((val) => {
          orderArray.push(val);
        })
      })