Cypress 拦截不匹配的 url

Min*_*šis 3 javascript testing cypress

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

https://mydomain.xxx/third-party-service/pragma?perPage=15&page=1
Request Method: GET
Run Code Online (Sandbox Code Playgroud)

尝试用代码拦截它:

// 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
  });
});
Run Code Online (Sandbox Code Playgroud)

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

我得到的错误:

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

小智 6

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

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

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