首页 > 解决方案 > 赛普拉斯拦截与 url 不匹配

问题描述

我正在通过网络调用获取数据:

https://mydomain.xxx/third-party-service/pragma?perPage=15&page=1
Request Method: GET

试图用代码拦截它:

// Overriden not to clear localStorage authentication tokens
const clear = Cypress.LocalStorage.clear;
Cypress.LocalStorage.clear = function(keys) {
  if (keys) {
    return;
  }
};

context('Navigation', () => {
  before(() => {
    cy.login();
    cy.visit('/');
  });
  beforeEach(() => {
    cy.get('[data-test=test-burger]').click();
  });

  it('Tests table', () => {
    cy.get('[data-test=invoices]').click();
    cy.intercept('**/pragma**').as('getPragmaDocuments');
    cy.wait('@getPragmaDocuments');
    //....assertions here after API call is waited
  });
});

但是,它不会拦截网络请求。

我得到的错误:

5000 毫秒后重试超时:cy.wait() 超时等待 5000 毫秒以等待对路由的第一个请求:getPragmaDocuments。从未发生过请求。了解更多

标签: javascripttestingcypress

解决方案


我看到了你的问题,但我不确定你的“修复”为什么有效,哈哈。

您需要在单击cy.intercept() 之前开始,如下所示:

cy.intercept('**/pragma**').as('getPragmaDocuments');
cy.get('[data-test=invoices]').click();
cy.wait('@getPragmaDocuments');

推荐阅读