最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress intercept not matching url - Stack Overflow

programmeradmin3浏览0评论

I am fetching data with a network call:

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

Trying to intercept it with code:

// 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
  });
});

However, it does not intercept the network request.

Error I get:

Timed out retrying after 5000ms: cy.wait() timed out waiting 5000ms for the 1st request to the route: getPragmaDocuments. No request ever occurred.Learn more

I am fetching data with a network call:

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

Trying to intercept it with code:

// 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
  });
});

However, it does not intercept the network request.

Error I get:

Timed out retrying after 5000ms: cy.wait() timed out waiting 5000ms for the 1st request to the route: getPragmaDocuments. No request ever occurred.Learn more

Share Improve this question edited Jan 14, 2021 at 15:03 Mindaugas Nakrošis asked Jan 14, 2021 at 14:22 Mindaugas NakrošisMindaugas Nakrošis 1052 silver badges6 bronze badges 2
  • Testing with Cypress.minimatch as the docs suggest show that URL is matched, can you give a minimal reproducible example? – jonrsharpe Commented Jan 14, 2021 at 14:35
  • I tried testing with Cypress.minimatch. It returns true. Edited my question including all code. – Mindaugas Nakrošis Commented Jan 14, 2021 at 15:01
Add a ment  | 

1 Answer 1

Reset to default 7

I see your problem, and I'm not sure why your "fix" worked, haha.

You need to start the cy.intercept() before the click, like this:

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

评论列表(0)

  1. 暂无评论