javascript - How to assert page doesnt reload with Cypress - Stack Overflow

I am running a test that assures a page does not reload when a button is clicked. So far, I have not fo

I am running a test that assures a page does not reload when a button is clicked. So far, I have not found the best way to assert on this. I initially thought to create a route and wait for the xhr request that I don't want to happen, and if it happens, to fail the test. However, there does not appear to be a way to catch an error from a cy.wait() which makes it impossible for me to pass the test once the timeout occurs. My current solution is to ensure that the href for the given element starts with "#", but that seems to have the shorting of someone potentially attaching an event to it that would in-fact reload the page and it would not be caught by my test.

I am running a test that assures a page does not reload when a button is clicked. So far, I have not found the best way to assert on this. I initially thought to create a route and wait for the xhr request that I don't want to happen, and if it happens, to fail the test. However, there does not appear to be a way to catch an error from a cy.wait() which makes it impossible for me to pass the test once the timeout occurs. My current solution is to ensure that the href for the given element starts with "#", but that seems to have the shorting of someone potentially attaching an event to it that would in-fact reload the page and it would not be caught by my test.

Share Improve this question asked Dec 24, 2018 at 0:03 displayName0101displayName0101 511 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You could use the Navigation Timing API:

In your test you will need to assert something like:

expect(performance.navigation.type).to.equal(performance.navigation.TYPE_RELOAD);

Using A Flag on the Window Object

In my tests, I used the method described in Detect page reload from Cypress test by adding a new property shouldNotReloadOnFirefox to the window object, then cheking it's still present (i.e. no reload):

// arrange
cy.window().then((win) => (win.shouldNotReloadOnFirefox = true));

// act 
cy.whateverYouAreTesting()

// assert
cy.window().should('have.prop', 'shouldNotReloadOnFirefox');

Using Browser API

As of 2021-05-27

performance.navigation was deprecated.

You should use PerformanceNavigationTiming instead and the getEntriesByType method:

expect(performance.getEntriesByType('navigation')[0].type).to.equal(
  'reload'
);

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

相关推荐

  • javascript - How to assert page doesnt reload with Cypress - Stack Overflow

    I am running a test that assures a page does not reload when a button is clicked. So far, I have not fo

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信