How to find the href inside of an element with cypress?

Viewed 195

The main problem in here is that we don't know the party record id until we create the opportunity inside of salesforce. For the same reason, I have to see if it is a way to get the href links and from there save them into a fixture file to be able to use that data in the future.

Example:

enter image description here

Code | Cypress:

cy.get('table').get('tbody').get('tr').get('td').get('th').should('be.visible')

HTML:

enter image description here

2 Answers

You can also make use of the locator [data-refid="recordId"], invoke the href attribute and save it in a fixtures file.

cy.get('[data-refid="recordId"]').each(($ele) => {
    cy.writeFile('cypress/fixtures/links.txt', $ele.attr('href'), {
        flag: 'a+'
    })
})

You need to utilise the invoke method. You’ll end up with something like this: cy.get(‘a’).invoke(‘href’)

Related