philein-sophos
philein-sophos

Reputation: 372

Wait for multiple Promise to return data in Cypress

I have below given code in a javascript file,

class APIMessage
{
    isReady()
        {
            return cy.request({
                method: 'GET',
                url: "http://127.0.0.1:5000/isready"
            
            }).then(res => {
                return new Promise((resolve) => {
                    resolve(res);
                })
            })
        }

    receiveMsg()
    {
        return cy.request({
            method: 'GET',
            url: "http://127.0.0.1:5000/getMsg"
            
        }).then(res => {
            return new Promise((resolve) => {
                resolve(res);
            })
        })
    }

    async GetMessage()
        {
            const status =  await this.isReady()
            
            if(status.body.send == true)
            {
                var message =  await this.receiveMsg()
                cy.log("-- ",message)
            }
        }
}
var msg = new APIMessage()
export default msg

this GetMessage() I am able to import to it block of cypress, and use as

it("Receive Message",function(){
        msg.GetMessage()
    })

the test case calls GetMessage(), gets isReady status, enters if block but fails to print message, even though the API works fine, also it ends the test case successfully here without executing remaining statements.

Upvotes: 2

Views: 701

Answers (0)

Related Questions