How can I use multiple await in cypress?

Viewed 112

I am trying to fetch multiple data from UI using cypress. First of all, I thought my selector is incorrect, but I have tried it with the same selector as the below codebase, but it still not working below is the function

async getData(model?: any) {
        const listSelector='[ng-repeat="workflow in vm.workflows track by $index"]';
        const dataFromUi = {};
        const data = await cy.get(listSelector); // this gives data
        const data1 = await cy.get(listSelector); // this doesn't
        dataFromUi['Test1'] = data;
        dataFromUi['Test2'] = data1;
        debugger;
        return dataFromUi;
    }

I was calling this method from a spec file, below is the calling spec file

describe('app test', () => {
    beforeEach(() => {
        cy.login();
        cy.navigateUsingMenu('dasboard', '');
    });
    it('',async ()=>{
        const result = await getData();
        result.Test1 // contains data
        result.Test2 // contains undefined
    })

})

In data I am getting contents, but data1 returns undefined.

1 Answers

I have found a solution and that is using different methods and different it blocks. The below codebase is working, but I want them in a single block.
The service methods

async getData1(model?: any) {
        const listSelector='[ng-repeat="workflow in vm.workflows track by $index"]';
        const data = await cy.get(ListSelector);
        return data;
    }

async getData2(model?: any) {
        const listSelector='[ng-repeat="workflow in vm.workflows track by $index"]';
        const data1 = await cy.get(ListSelector);
        return data1;
    }

The spec

describe('app test', () => {
    beforeEach(() => {
        cy.login();
        cy.navigateUsingMenu('dashboard', '');
    });
    it('',async ()=>{
        const result = await getData1();
    })
    it('',async ()=>{
        const result = await getData2();
    })

})
Related