mardi 3 novembre 2020

async cypress comands return string

I am tying to to fetch a value using cypress command, this command calls an async function to retrieve value. When I try to log the fetched value within the command I can see a string, but when I am logging the value in the called function it is receiving an object. Is there any way to make the cypress command execute this async function, wait for its result and return the string.

I am using cypress with typescript.

Following is the cypress command (Output: "success", Output type: string)

Cypress.Commands.add('fetchValue', async (name: string) => {
    try {
        const data = await <apiCall>().promise();
        const op = JSON.stringify(JSON.parse(data.outPut??'{}')[name])
        cy.task('log', op)
        return op
    } catch (error) {
        return ''
    }
});

Following is the function call (Output type: object)

...
...
const op = cy.fetchValue('input')
cy.task('log', op)
...
...

Any help appreciated.

Aucun commentaire:

Enregistrer un commentaire