I need to get the value for data-id but when I use the below code it does not return any.
cy.get('[data-nemo=token]')
.invoke('attr', 'data-id').then(dataId => {
cy.log('dataId : ', dataId);`enter code here`
});
Thanks,
As per Cypress docs, you can use cy.invoke to call a jQuery method. So, just as in jQuery, you would do
$('[data-nemo=token]').data('id');
in Cypress it's
cy.get('[data-nemo=token]').invoke('data', 'id');
If you then want to use that value, you can either use .then or create an alias with .as and reference the alias later in your tests:
cy.get('[data-nemo=token]')
.invoke('data', 'id')
.then(dataId => cy.log('dataId : ', dataId));
or
cy.get('[data-nemo=token]')
.invoke('data', 'id')
.as('dataId');
cy.get('@dataId')
.then(dataId => cy.log('dataId : ', dataId));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With