mercredi 7 août 2019

Bypass "user gesture" requirement when testing "beforeunload" dialogue in Cypress

I am trying to write a Cypress test to validate that a "beforeunload" dialogue appears when a user tries to navigate away from an incomplete form.

When a user tries to leave a page after interacting with a form, my site activates a "beforeunload" dialogue to warn them unsaved data may be lost. Everything works fine when I test the behavior manually but When I try to write a test in Cypress I get '[Intervention] Blocked attempt to show a 'beforeunload' confirmation panel for a frame that never had a user gesture since its load. https://www.chromestatus.com/feature/5082396709879808'. It seams that JS does not trigger a true "user gesture" . I understand why Chrome does this but I would hope there would still be a way to write tests. I've found workarounds for a similar problem with video playback @ https://github.com/cypress-io/cypress/issues/2376. But this does not seam to work for "beforeunload". None of the flags I found here seam to work https://peter.sh/experiments/chromium-command-line-switches/

    on('before:browser:launch', (browser = {}, args) => {
      if (browser.name === 'chrome') {
        // Mac/Linux
        args.push('--disable-gesture-requirement-for-presentation')
      }
      return args
    })

Hoping there might be a work around. Can I either simulate an actual "gesture" in Cypress or disable the requirement in Chrome? Any help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire