Skip to main content

Cypress Wait Command Pass an Array of Aliases

In this article, we will pass in an array of aliases to intercept using the spread command, which will spread the intercepting array into multiple arguments to work on that within the scope of that.

For a real case scenario, we will intercept resources from the gorest API site, which is purely used for testing purposes.

Before we move onto the process, here's a brief look at what the wait command is and its syntax:

Cypress wait command is used to make a step wait for several milliseconds or wait for an aliased resource to finish the process and move on to next.

Syntax

cy.wait(time)
cy.wait(alias)
cy.wait(aliases)
cy.wait(time, options)
cy.wait(alias, options)
cy.wait(aliases, options)


Some of the options we can include in the command can be a timeout, log, etc.

  • Log: To display the command in the command log.
  • Timeout: Wait for some particular thing to get finished before timing out.
  • Alias: pass in the aliased resource.
  • Options: Can pass options object to change the default normal behavior of the cy.wait().

Just so you know, Aliased wait, delays for the route aliased to respond and then proceeds to intercept without changing or stubbing its response.

Ok, let us see the real deal.

Pass an Array of Aliases

describe('Intercepting Gorestapi',()=>{

    it('Intercepting Routes Of Gorest',()=>{
        cy.visit('https://gorest.co.in/')

        cy.intercept({path : '/public/v1/users' }).as('users')
        cy.intercept({path : '/public/v1/posts' }).as('posts')
        cy.intercept({path : '/public/v1/comments' }).as('comments')
        cy.intercept({path : '/public/v1/todos' }).as('todos')

        cy.get("table:nth-of-type(1) a[href = 'https://gorest.co.in/public/v1/users']").click({force: true})
        cy.get("table:nth-of-type(1) a[href = 'https://gorest.co.in/public/v1/posts']").click({force: true})
        cy.get("table:nth-of-type(1) a[href = 'https://gorest.co.in/public/v1/comments']").click({force: true})
        cy.get("table:nth-of-type(1) a[href = 'https://gorest.co.in/public/v1/todos']").click({force: true})

        cy.wait(['@users', '@posts', '@comments','@todos']).spread(
            (users, posts, comments, todos) => {
                cy.log(JSON.stringify(users))
            console.log(JSON.stringify(users))
            cy.log(JSON.stringify(posts))
            console.log(JSON.stringify(posts))
            cy.log(JSON.stringify(comments))
            console.log(JSON.stringify(comments))
            cy.log(JSON.stringify(todos))
            console.log(JSON.stringify(todos))
            }
          )


    })
})
  • So we are intercepting the available resources of gorestapi.
  • Users, posts, comments, and todos are available resources to see on that site.
  • We are aliasing these resources by providing the path to intercept after visiting.
  • Then after clicking on each of the resources, we perform the intercept by making all the alised interception in an array.
  • The spread command is used here to spread the aliases into separate arguments to be used separately and worked with.

  • We can see four routes we are intercepting here with their aliases.

  • Here we can also see the responses we intercepted from each resource from the gorest site and logged it.
Tags
Submitted by arilio666 on February 1, 2022

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.