赛普拉斯拦截不匹配的网址

时间:2021-01-14 14:22:03

标签: javascript testing cypress

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

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。从未发生任何请求。了解详情

1 个答案:

答案 0 :(得分:2)

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

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

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