javascript - How to clear browser cache within a Cypress test - Stack Overflow

Background:Hey all, I have a Cypress test I'm trying to write that should check whether an image

Background:

Hey all, I have a Cypress test I'm trying to write that should check whether an image URL has been called, after the image itself has been clicked on.

Currently I run the test and it finds the image URL on the first run but when it is re-run, the test fails to find it because the image has already been saved to the browser cache.

Is there a way to clear the browser cache before each Cypress test so that the image URL will always get called/found when I run the test?

Current solution:

it('should request webp image format for zoomed pdp image', () => {
  cy.intercept(
    '**/cdn/cs/set/assets/blt2bd0405f3cb027cb/5005769.jpg?fit=bounds&format=webply&quality=80&width=1600&height=1600&dpr=1.5'
  ).as('zoomedImageRequest');
  cy.gotoPage('product/transparent-red-sorting-case-to-go-5005769');
  cy.get(pdp.pdpImage).click();
  cy.wait('@zoomedImageRequest').then((xhr) => {
    const zoomedImageUrl = xhr.response.url;
    expect(zoomedImageUrl).to.contain('format=webply');
  });
});

Background:

Hey all, I have a Cypress test I'm trying to write that should check whether an image URL has been called, after the image itself has been clicked on.

Currently I run the test and it finds the image URL on the first run but when it is re-run, the test fails to find it because the image has already been saved to the browser cache.

Is there a way to clear the browser cache before each Cypress test so that the image URL will always get called/found when I run the test?

Current solution:

it('should request webp image format for zoomed pdp image', () => {
  cy.intercept(
    '**/cdn/cs/set/assets/blt2bd0405f3cb027cb/5005769.jpg?fit=bounds&format=webply&quality=80&width=1600&height=1600&dpr=1.5'
  ).as('zoomedImageRequest');
  cy.gotoPage('product/transparent-red-sorting-case-to-go-5005769');
  cy.get(pdp.pdpImage).click();
  cy.wait('@zoomedImageRequest').then((xhr) => {
    const zoomedImageUrl = xhr.response.url;
    expect(zoomedImageUrl).to.contain('format=webply');
  });
});
Share Improve this question edited Jul 11, 2022 at 22:41 Fody 32.4k3 gold badges36 silver badges58 bronze badges asked Jul 11, 2022 at 15:24 devDandevDan 551 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Try clearing the network cache at the top of the test,

cy.wrap(Cypress.automation('remote:debugger:protocol', {
  mand: 'Network.clearBrowserCache',
}))

cy.intercept(...).as('zoomedImageRequest');

cy.gotoPage('product/transparent-red-sorting-case-to-go-5005769');
cy.get(pdp.pdpImage).click();
cy.wait('@zoomedImageRequest').then((xhr) => {
  const zoomedImageUrl = xhr.response.url;
  expect(zoomedImageUrl).to.contain('format=webply');
});

There is also a header that can be removed, but I found it not to be consistent

cy.intercept('...', req => {
  delete req.headers['if-none-match'];
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744142543a4560270.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信