skip to Main Content

A web driver test produces a browser alert: Do you want to leave this page? Cancel | Continue. None of the following commands selects the Continue button.

await browser.acceptAlert()
await browser.pressKeyCode(13) // 13 = Enter key
await browser.confirm()

These have been tried in the content of the afterEach :

describe('xxx', () => {
    afterEach(async () => {
        await browser.execute('window.localStorage.clear()');
        await browser.deleteAllCookies()
        await browser.execute('sessionStorage.clear()')
        await browser.refresh()
        // await browser.acceptAlert() // not working
        // await browser.pressKeyCode(13) // not working
        // await browser.confirm() // not working
        await browser.pause(1000)
    });

Using web driver v7.

This is the alert:

enter image description here

Is this an OS alert rather than a native browser alert?

Edit

In wdio.conf.js I now have:

capabilities: [
    {
        maxInstances: 6,
        browserName: 'chrome',
        'goog:chromeOptions': {
            args: ['--disable-prompt-on-repost']
        },

(based on https://webdriver.io/docs/configurationfile/) but VS Code says:

ERROR @wdio/config:ConfigParser: Failed loading configuration file:
/Users/User/Documents/Projects/wdio-visual-regression-testing/wdio.conf.js:
chromeOptions is not defined

This answer is not applicable.

2

Answers


  1. Solution #1: addEventListener

    Try using

    window.addEventListner("beforeunload", (e) => {
        e.stopImmediatePropagation();
    });
    

    Solution #2: pressKeyCode

    The pressKeyCode function takes two arguments. You can use one for the keyCode, but, you can only use strings. If that doesn’t work try pressing the tab key with

    browser.setValue('input', ['Tab'])
    

    and then, don’t forget to press the enter key

    Login or Signup to reply.
  2. Try browser.keys()

    I think they removed support for browser.setValue in v5, so instead you can try using browser.keys("uE007") instead. the uE007 means the enter key.

    Explanation of the addEventListener

    For the addEventListener, it adds a listener for when the page unloads and stops all the other beforeunload event listeners using stopImmediatePropagation(). You can read more about stopImmediatePropagation() here: MDN Web Docs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search