I am new to Cypress and I am trying to use invoke() to access the href attribute of each div tag from the group but it is giving an error. Can anyone suggest you how to do this?
cy.get('.bms-scoreboard__game-tile--mls').each(($el,index,$list) => { $el.get('a') .invoke('attr','href') .then(href => { cy.request(href) .its('status') .should('eq',200) }) })
P粉2765774602023-12-13 00:52:13
$el
is a JQuery element, not itself in the Cypress chain. You need to use cy.wrap()
to use it in a Cypress chain.
cy.get('.bms-scoreboard__game-tile--mls').each(($el,index,$list) => { cy.wrap($el) .get('a') .invoke('attr','href') .then(href => { cy.request(href) .its('status') .should('eq',200) }) })
P粉3598508272023-12-13 00:36:38
I think .get()
is inappropriate - it only works for <body>
, not for every '.bms-scoreboard__game-tile-- mls'
.
Try using .find()
instead of
Using jQuery operators
cy.get('.bms-scoreboard__game-tile--mls') .each(($el,index,$list) => { const href = $el.find('a').attr('href') cy.request(href) .its('status') .should('eq', 200) }) })
Or work with Cypress operators
cy.get('.bms-scoreboard__game-tile--mls') .each(($el,index,$list) => { cy.wrap($el).find('a') .invoke('attr','href') .then(href => { cy.request(href) .its('status') .should('eq',200) }) }) })
Or move "Find" to the first selector
cy.get('.bms-scoreboard__game-tile--mls a') .each($a => { const href = $a.attr('href') cy.request(href) .its('status') .should('eq', 200) }) })