cypress-io / cypress-documentation

Cypress Documentation including Guides, API, Plugins, Examples, & FAQ.
https://docs.cypress.io
MIT License
938 stars 1.04k forks source link

Document @alias.all and @alias.2 #1573

Open jennifer-shehane opened 5 years ago

jennifer-shehane commented 5 years ago

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:

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"))
  })
});
clarmso commented 5 years ago

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

lpanger commented 4 years ago

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.

jennifer-shehane commented 4 years ago

@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.

lpanger commented 4 years ago

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

alexagranov7 commented 4 years ago

Is there a plan to make .all work?

akkerd commented 3 years ago

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.

JensRoland commented 3 years ago

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

dzuncoi commented 3 years ago

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

anthony-colpron commented 3 years ago

@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?

imperatormk commented 3 years ago

Really what happened here?

jennifer-shehane commented 3 years ago

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').

martinrojas commented 2 years ago

@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?

rogoit commented 2 years ago

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 ;)

gajraj-gan commented 6 months ago

was this added?

jennifer-shehane commented 6 months ago

@gajraj-gan No