Home > Software engineering >  Cypress click on an nth image in a table with specifc src
Cypress click on an nth image in a table with specifc src

Time:04-01

I have a list of items in the table, each item has an image , active (blue) and not active (black), how do I make this query for cypress.

This is the image of the table list : enter image description here

Then this is the html code for the table : enter image description here

When expanded the image is below, it has a src path of /static/images/icon_profile.png enter image description here

My current test is like this :

it('Should create a legal form of a Customer profile', ()=>{
    cy.get('th').eq(50).find('img').should('have.attr', 'src', '/static/images/icon_profile.png').first().click()
  })

But when I ran it I get this error :

enter image description here

CodePudding user response:

You can do it more easily by combining selectors

cy.get('tr img[src="/static/images/icon_profile.png"]')
  .first()
  .parent('a')  // click the link not the img
  .click()

To confirm you have the right index when using .eq(50) run

cy.get('tr img[src="/static/images/icon_profile.png"]')
  .first()
  .parent('tr')
  .invoke('index')  // which row is it?
  .log()

CodePudding user response:

You can do something like this

cy.get('tr').each(($ele) => {
  if ($ele.attr('src') == '/static/images/icon_profile.png') {
    cy.wrap($ele).parent('a').click()
  }
})
  • Related