Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
how to return a value from custom command in cypress

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. enter image description here

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>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You'll have to return the entire chain command of the subject you want to return.

Cypress.Commands.add('readContactInfo', (companyName) => {
  return cy.get('.simple-table__cell:nth-child(1)')
    .contains(companyName)
    .parent()
    // find command limited to previous subject
    .find('div')
    .eq(2)
    // will invoke .text() and return it
    .invoke('text')
})

Now whenever you use the custom command, the text will be returned.

cy.readContactInfo('Magazzini Alimentari Riuniti')
  .should('eq', 'text you want to verify here')
about 4 years ago · Juan Pablo Isaza Relatório

0

You are on the right track, but note you don't need to return anything, the cy.wrap() is enough (Cypress set's your res.text() as the current subject).

The problem is .within() always resets the subject, and since it wraps the subject you want it has the last word, so-to-speak.

Replace it with .find()

Cypress.Commands.add("readContactInfo", (companyName) => {
  cy.get(".simple-table__cell:nth-child(1)")
    .contains(companyName)
    .parent()
    .find("div")
    .eq(2)
    .then(function (res) {
      return cy.wrap(res.text());
    });
});

You can also shorten the res.text()

Cypress.Commands.add("readContactInfo", (companyName) => {
  cy.get(".simple-table__cell:nth-child(1)")
    .contains(companyName)
    .parent()
    .find("div")
    .eq(2)
    .invoke('text')
});

Ref .within()

Yields

.within() yields the same subject it was given from the previous command.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda