filiphric / cypress-plugin-api

Cypress plugin to show your API information in the Cypress UI runner
ISC License
264 stars 35 forks source link

First API call after beforehook fails #113

Closed mvandebunt closed 1 year ago

mvandebunt commented 1 year ago

In our test we have a before() hook where we prepare testdata (create a product and bring it to a certain state). Followed by same tests that edit parts of the product. The first test will fail, the others succeed. When we change cy.api in our first test to cy.request all tests pass.

Stacktrace: AssertionError: Timed out retrying after 8000ms: Expected to find element: #140, but never found it. at Fe (webpack:///./node_modules/cypress-plugin-api/dist/support.js:12:2059) at Context.eval (webpack:///./node_modules/cypress-plugin-api/dist/support.js:14:2091)

ballPointPenguin commented 1 year ago

same. I was trying so many things to find why my test fails on first run, and then I switched out cy.api for cy.request and voila! it works again.

filiphric commented 1 year ago

thanks for the report! can you please provide an example of how you use the plugin? I was trying to reproduce the issue, but I wasn’t able to. I used the following code:

beforeEach(() => {
  cy.api('/')
});

it('test', () => {

});
bilarallen commented 1 year ago

Hi @filiphric I also encounter the same, but this occurs in the first API call inside the beforeEachhook()

  1. I set the requestMode to true in my env inside cypress.config.js

  2. in the beforeEach hook of my test I have this custom command that is using cypress recurse by gleb

    beforeEach(() =>{
    ...
    cy.validateWorkingDayStarted()
    // custom command that is using recurse
    recurse(
    () =>
      cy.request({
        method: "POST",
        url: `${Cypress.env("nxApi")}api/v2/management/status`,
        body: {
          fingerprint: "e2e6bf3e3228fd8da7bea1b0d3518111",
        },
      }),
    (res) => expect(res.body.workingDay.status).to.be.eq("STARTED"),
    {
      limit: 200,
      log: false,
      delay: 500,
    }
    );
    })
  3. Stacktrace: AssertionError: Timed out retrying after 20000ms: Expected to find element: #103, but never found it. Because this error occurred during a before each hook we are skipping the remaining tests in the current suite: Administration - Transactio... at Fe (webpack:///./node_modules/cypress-plugin-api/dist/support.js:12:2059) at Context.eval (webpack:///./node_modules/cypress-plugin-api/dist/support.js:14:2091)

ballPointPenguin commented 1 year ago

My case was relatively simple. I have a custom command:

Cypress.Commands.add('createConvoViaAPI', () => {
  cy.request('POST', '/api/v3/conversations', {
    is_active: true,
    is_draft: true,
  })
    .its('body.conversation_id')
    .as('convoId')
})

and in a test I do

cy.createConvoViaAPI().then(() => cy.visit('/m/' + this.convoId))

When I changed cy.request -> cy.api this test just hangs and then spits out the above error

AssertionError: Timed out retrying after 4000ms: Expected to find element: ###, but never found it.

From what I can tell the "element" is not a DOM element, but the alias ("convoId" for me). I had the same error with very similar approaches elsewhere in my tests. The chaining after cy.request/cy.api and storing -> retrieving an alias is the crux of the issue. If I use cy.api without chaining and setting an alias from the response, it seems to work fine.

filiphric commented 1 year ago

I’m still shooting in the dark here, but I am just now releasing a fix that could probably help with these issues. I will close this issue, but feel free to reopen if the update does not fix the issue