Smrutiranjana Ray
Smrutiranjana Ray

Reputation: 85

Cypress: Getting Cypress detected that you invoked one or more cy commands in a custom command but returned a different value

While returning any single /set of values from a function (from command.js /Page object class) getting error as "Cypress detected that you invoked one or more cy commands in a custom command but returned a different value.". I have searched different options like 'Warp', '.then' but not have the luck.

Following are the details:

My Command.js file:

Cypress.Commands.add("getconstantvalue", () => {
    const todaysDateTime = Cypress.moment().format('MMMDDYYYYSS')
    cy.log(todaysDateTime)
    return todaysDateTime
})

and I want this 'todaysDateTime' in my driver/describe suit script:

describe('The Home Page', function() {
  it('successfully loads', function() { 
    var data = cy.getconstantvalue()
    cy.log(data)       
  })
})

Upvotes: 1

Views: 5669

Answers (1)

Alapan Das
Alapan Das

Reputation: 18650

This should work. You just have to chain it to the command you are using.

Command.js File

Cypress.Commands.add("getconstantvalue", () => {
    const todaysDateTime = Cypress.moment().format('MMMDDYYYYSS')
    cy.log(todaysDateTime)
    return cy.wrap(todaysDateTime)
})

Test File

describe('The Home Page', function() {
    it('successfully loads', function() {
        cy.getconstantvalue().then(data => {
            cy.log(data);
        })
    })
})

Upvotes: 2

Related Questions