hi folks i am new in ui/cypress automation i am trying to create a custom command which read a data from ui and return it to calling command where i can compare it with test data and validate the result e.g.
for custom command in support\commands.js
below is my code
which take companyname
as parameter and read the contact info
from ui
Cypress.Commands.add('readContactInfo', (companyName) => {
cy.get('.simple-table__cell:nth-child(1)').contains(companyName).parent().within(function()
{
cy.get('div').eq(2).then(function(res)
{
cy.log(res.text())
})
})
})
and when i call the it from test.spec.js
file like below
cy.readContactInfo('Magazzini Alimentari Riuniti')
the output is like
log Giovanni Rovelli
but i wanted that the readContactInfo
will return the text value
to the cypress command
like what normal function do..
what i tried
Cypress.Commands.add('readContactInfo', (companyName) => {
cy.get('.simple-table__cell:nth-child(1)').contains(companyName).parent().within(function()
{
cy.get('div').eq(2).then(function(res)
{
return cy.wrap(res.text())
})
})
})
and when i call the it from test.spec.js
file like below
cy.readContactInfo('Magazzini Alimentari Riuniti').then(text)=>{
cy.log(text);
}
the ouput is like
8 get .simple-table__cell:nth-child(1)
9 -contain Magazzini Alimentari Riuniti
10 -parent
11 -within
12 get div
13 -eq 2
14 wrap Giovanni Rovelli
l5 log <li.simple-table__row>