代码之家  ›  专栏  ›  技术社区  ›  Samira Arabgol

CypressError:Cypress检测到您从一个命令返回了一个promise,同时还调用了该promise中的一个或多个cy命令

  •  0
  • Samira Arabgol  · 技术社区  · 4 年前

    我用的是柏树 fixture 和 intercept 上传文件,并验证上传端点(post)是否成功。这是代码:

    describe('something', () => {
     
      let projectID;
    
      before(() => {
        projectID = localStorage.getItem('projectID');
       
        cy.intercept('POST', '/graphql', (req) => {
          cy.log(req.body);
          if (req.body.operationName === 'postRequest') {
            req.alias = 'postRequest';
          }
        });
      });
    
      it('should validate file upload', () => {
        const fileName = 'test_file_upload.xlsx';
        cy.get('.file-uploader').should('exist');
        cy.get('.upload-file-label').first().click({ force: true });
        cy.fixture(fileName, 'binary').then((fileContent) => {
          cy.get('input[type=file]')
            .upload(
              {
                fileContent,
                fileName,
                mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                encoding: 'utf-8',
              },
              { subjectType: 'input' }
            )
            .wait('@postRequest')
            .then((interception) => {
              cy.log(interception.response.body);
            });
        });
      });
    });
    
    

    我不确定错误指的是什么,但我猜可能是异步问题。

    谢谢你的帮助

    0 回复  |  直到 4 年前
        1
  •  0
  •   jjhelguero    4 年前

    这个 .wait() must be chained off a cy 当您传递别名时。

    // code looks good until here
    cy.get('input[type=file]')
      .upload(
        {
          fileContent,
          fileName,
          mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
          encoding: 'utf-8',
        },
        { subjectType: 'input' }
      )
      cy.wait('@postRequest')
        .then((interception) => {
          cy.log(interception.response.body);
        });
    

    一段时间过去了。 500 ) .wait() 可以从 cy 或任何其他命令。

    cy.get('.selector')
      .wait(1000)
    
    cy.wait(500)