cypress-documentation icon indicating copy to clipboard operation
cypress-documentation copied to clipboard

Document @alias.all and @alias.2

Open jennifer-shehane opened this issue 5 years ago • 15 comments

I'm submitting a...

[ ] Bug report 
[x] Content update
[ ] Process update (build, deployment, ... )

Type of bug / changes

We should document the use of @alias.all and @alias.2 within a couple of documents:

  • https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html
  • https://docs.cypress.io/api/commands/get.html#Alias

When referencing aliases, you can reference @alias.all or @alias.2 and it will return respectively all of the aliases of that name or the 2nd request matching the name.

Examples:

it("does not poll for list again", function() {
  cy.wait("@getList");
  cy.tick(10000);
  // does not poll for another request
  cy.get("@getList.all").should("have.length", 1);
});
it("first request has correct data", function() {
  cy.route({
    url: 'users',
    response: 'fx:users'
  }).as("getUsers");
  cy.visit("/users");
  cy.get("@getUsers.1").then((xhr) => {
    expect(xhr.url).to.include("page=1"))
  })
});

jennifer-shehane avatar Apr 10 '19 05:04 jennifer-shehane

There needs to be a discussion whether the use of alias.all and alias.[1-9][0-9]? should be documented or not. It sounds like this feature is unadvertised on purpose. https://github.com/cypress-io/cypress/pull/3929#issuecomment-482372968

clarmso avatar Apr 13 '19 02:04 clarmso

I have an issue using the .all option. It doesn't seem to retry when it's used with .should.

Modified from your example above cy.get("@getList.all").should("have.length", 2); I expect it to retry until 2 requests were made. It fails immediately for me. I've also tried using a timeout option with the get call but it still fails immediately no matter how long I set the timeout.

It's not clear to me if this feature is production ready or experimental so I won't log a bug.

lpanger avatar Feb 20 '20 19:02 lpanger

@lpanger Using the cy.get("@getList.all") has no retrying mechanism. It simply gets the requests that have currently been resolved immediately when it is called. This is not a way to 'wait for all requests', as we have no idea what 'all' means for your application. It is only a way to get all currently matching requests for that alias.

jennifer-shehane avatar Feb 24 '20 06:02 jennifer-shehane

I assumed by chaining with .should() it would retry until the condition is met. It's unfortunate it doesn't work that way.

lpanger avatar Feb 24 '20 21:02 lpanger

Is there a plan to make .all work?

alexagranov7 avatar Jun 16 '20 23:06 alexagranov7

Since I didn't know about this .all API, I've found a convoluted way of waiting for a series of responses, when you know the number of responses from a previous request:

    cy.wait('@getCtypes').then(xhr => {
      const numCtypes = get(xhr, 'response.body.data.length', 0);
      searchContentType(searchName, route);
      postSearchAction();

      if (numCtypes !== 0 && waitForPermissions) {
        cy.wait(Array(numCtypes).fill('@getPermissions'), { timeout: 16000 });
      }
    });

This method was leading to lots of "flakyness" and Cypress often failed to find each of the requests as unique, so some waits considered a request twice and lead to timeouts and flaky errors.

I changed it and it works like a charm now.

cy.wait('@getPermissions.all', { timeout: 16000 });

I really think adding .all to the documentation will be beneficial to prevent wasting lots of time in the future like I did trying to find patches for this problem. Adding a warning saying something like THIS API MAY CHANGE will be enough to keep an eye on it on future releases.

akkerd avatar Nov 19 '20 17:11 akkerd

Please add this to the documentation and make it an official feature.

JensRoland avatar Mar 17 '21 09:03 JensRoland

@jennifer-shehane Cypress v7 doesn't work to get all aliases anymore. image Any idea why? Or is it removed out of v7?

dzuncoi avatar Apr 11 '21 21:04 dzuncoi

@jennifer-shehane .all doesn't work for me either since updating to v7.0.1 This was working perfectly in v6.2.1 image

Was it removed or changed in v7?

anthony-colpron avatar Apr 12 '21 16:04 anthony-colpron

Really what happened here?

imperatormk avatar May 17 '21 04:05 imperatormk

See https://github.com/cypress-io/cypress/issues/14916 - the cy.get('alias.all') syntax works in 6.4.0+ which is the original use case of the all.

cy.wait('alias.all') does not make sense, even though it was working to wait on the specific request previous to 6.4.0.

You should replace any uses of cy.wait('@alias.all') with cy.get('@alias.all').

jennifer-shehane avatar May 17 '21 13:05 jennifer-shehane

@jennifer-shehane There is something that doesn't make sense at this point how would the changing the wait to get.

how will cause cypress to wait. Am I missing something?

martinrojas avatar Oct 14 '21 15:10 martinrojas

Thx @jennifer-shehane helps me a lot. By the way i am now doing a german tutorial series about CypressIO with a repo for the Sulu CMS. Maybe we can get in touch to help more deveopers with tests and passion. Feel free to contact me on every channel ;)

rogoit avatar Feb 18 '22 15:02 rogoit

was this added?

gajraj-gan avatar Mar 19 '24 09:03 gajraj-gan

@gajraj-gan No

jennifer-shehane avatar Mar 19 '24 15:03 jennifer-shehane