【问题标题】:How to cleanup cypress cy.intercept requests queue?如何清理 cypress cy.intercept 请求队列?
【发布时间】:2021-11-10 16:49:33
【问题描述】:

我正在使用before(() => {...}) 块中编写的间谍功能连续测试仪表板的几个过滤器(它们在后端):

function aliasQuery(
  request: CyHttpMessages.IncomingHttpRequest,
  operationName: string,
): void {
  const { body } = request;
  if (body.operationName === operationName) {
    request.alias = operationName;
  }
}

export function spyOnGraphQL(operationName: string): void {
  cy.fixture('hosts').then(({ graphQLHostname }) => {
    cy.intercept(ApiMethods.Post, graphQLHostname, (request) => {
      aliasQuery(request, operationName);
    });
  });
}

然后在for循环内我使用

cy.wait(`@${operationName}`).should(({response}) => {...})

逐一检查过滤器。

虽然有问题,在使用每个过滤器并获得结果后,我需要通过发送另一个 graphql 请求来重置所有过滤器,此请求的查询名称与过滤器的查询名称匹配,因此当再次调用 cy.wait 时,它会捕获 reset过滤请求会破坏一切。它是这样的:

  • 应用过滤器 1(graphql 请求 1)
  • 使用cy.wait,它会捕获请求1
  • 重置过滤器(graphql 请求 2)
  • 应用过滤器 2(graphql 请求 3)
  • 使用cy.wait,它会捕获请求2 -->这就是问题的开始

有没有办法在应用新过滤器之前清理cy.intercept 捕获的请求?或者至少使用请求有效负载区分重置请求和过滤请求?

【问题讨论】:

    标签: javascript typescript graphql cypress e2e-testing


    【解决方案1】:

    我没有找到清理请求队列的方法,但我能够使用额外的回调通过给它们不同的别名来忽略一些请求。

    function aliasQuery(
      request: CyHttpMessages.IncomingHttpRequest,
      operationName: string,
      callback?: TypeAliasQueryCallback,
    ): void {
      const { body } = request;
    
      if (body.operationName === operationName) {
        request.alias = operationName;
    
        if (typeof callback === 'function') {
          callback(request);
        }
      }
    }
    
    export function spyOnGraphQL(
      operationName: string,
      callback?: TypeAliasQueryCallback,
    ): void {
      cy.fixture('hosts').then(({ graphQLHostname }) => {
        cy.intercept(ApiMethods.Post, graphQLHostname, (request) => {
          aliasQuery(request, operationName, callback);
        });
      });
    }
    
    export function ignoreResetRequest(
      request: CyHttpMessages.IncomingHttpRequest,
    ): void {
      const { body } = request;
    
      // Checking if the filters are sent (resetting sends empty array)
      if (!body.variables.filter.and.length) {
        request.alias = 'ignored';
      }
    }
    
    spyOnGraphQL('some_operation_name', ignoreResetRequest);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      • 2010-12-06
      • 2011-07-16
      • 2021-01-13
      相关资源
      最近更新 更多