Skip to content

[🐛 Bug]: Alert closes immediately after opening with the keyboard #11832

@shaamoth

Description

@shaamoth

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

latest

Node.js Version

18.17.0

Mode

WDIO Testrunner

Which capabilities are you using?

{
 browserName: 'chrome'
}

What happened?

JavaScript alert is closed immediately after it is opened by a button that is focused and pressed using the keyboard (enter key). I ran into this problem in a larger project, but reproduced it in a small example as well.

I have a button that opens an alert. When I click on it with await $('#alertBtn').click(), the alert stays open (correct behavior). If I set focus on the button and press enter with await browser.keys(Key.Enter);, the alert opens and then closes immediately. If I try these steps manually, the alert stays open in both cases.

What is your expected behavior?

Alert should remain open after the key is pressed, just like when it is opened with a mouse click.

How to reproduce the bug.

Repository: https://github.com/shaamoth/wdio_alert_issue

Test:

import { Key } from 'webdriverio';
import { browser, $ } from '@wdio/globals'

describe('Alert test', () => {

    before(async () => {
        await browser.url(`index.html`);
    })

    it('alert test - click', async () => {
        await $('#alertBtn').click();
        // Alert remains open
        await browser.pause(5000);
        const alertText = await browser.getAlertText();
        console.log(`Alert text is "${alertText}"`); // output: Alert text is "Click me button pressed"
        await browser.acceptAlert();
        await expect(alertText).toBe('Click me button pressed');
        // Passing
    })

    it('alert test - keyboard', async () => {
        await $('#focusBtn').click();
        await browser.keys(Key.Tab);
        await browser.pause(1000);
        await browser.keys(Key.Enter);
        // Alert is open and closes immediately
        await browser.pause(5000);
        const alertText = await browser.getAlertText(); // Failing - no such alert
        console.log(`Alert text is ${alertText}`);
        await browser.acceptAlert();
        await expect(alertText).toBe('Click me button pressed');
    })
})

Simple HTML file to demonstrate the issue, index.html:

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <button type="button" id="focusBtn">Focus</button>
        <button type="button" id="alertBtn" onclick="alert('Click me button pressed')">Click me</button>
    </body>
</html>

Relevant log output

2023-12-08T12:19:20.322Z INFO @wdio/local-runner: Run worker command: run
2023-12-08T12:19:20.628Z INFO webdriver: Initiate new session using the WebDriver protocol
2023-12-08T12:19:20.983Z INFO webdriver: Downloading Chromedriver v120.0.6099.63
2023-12-08T12:19:21.473Z WARN webdriver: Chromedriver v120.0.6099.63 don't exist, trying to find known good version...
2023-12-08T12:19:21.547Z INFO webdriver: Download of Chromedriver v120.0.6099.71 was successful
2023-12-08T12:19:22.576Z INFO @wdio/utils: Started Chromedriver v120.0.6099.63 with params --port=50899 --allowed-origins=* --allowed-ips=0.0.0.0 in 1605ms on port 50899
2023-12-08T12:19:22.728Z INFO webdriver: [POST] http://0.0.0.0:50899/session
2023-12-08T12:19:22.729Z INFO webdriver: DATA {
  capabilities: {
    alwaysMatch: { browserName: 'chrome', 'goog:chromeOptions': [Object] },
    firstMatch: [ {} ]
  },
  desiredCapabilities: {
    browserName: 'chrome',
    'goog:chromeOptions': {
      binary: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
    }
  }
}
2023-12-08T12:19:23.798Z INFO webdriver: COMMAND navigateTo("c:\Users\userA\wdio_project_test/index.html")
2023-12-08T12:19:23.799Z INFO webdriver: [POST] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/url
2023-12-08T12:19:23.799Z INFO webdriver: DATA {
  url: 'c:\\Users\\userA\\wdio_project_test/index.html'
}
2023-12-08T12:19:23.890Z INFO webdriver: RESULT null
2023-12-08T12:19:23.894Z INFO webdriver: COMMAND findElement("css selector", "#alertBtn")
2023-12-08T12:19:23.894Z INFO webdriver: [POST] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/element
2023-12-08T12:19:23.895Z INFO webdriver: DATA { using: 'css selector', value: '#alertBtn' }
2023-12-08T12:19:23.921Z INFO webdriver: RESULT {
  'element-6066-11e4-a52e-4f735466cecf': 'DEF1A4702D64691DD327661DC99BBB81_element_2'
}
2023-12-08T12:19:23.933Z INFO webdriver: COMMAND elementClick("DEF1A4702D64691DD327661DC99BBB81_element_2")
2023-12-08T12:19:23.934Z INFO webdriver: [POST] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/element/DEF1A4702D64691DD327661DC99BBB81_element_2/click
2023-12-08T12:19:23.981Z INFO webdriver: RESULT null
2023-12-08T12:19:28.992Z INFO webdriver: COMMAND getAlertText()
2023-12-08T12:19:28.993Z INFO webdriver: [GET] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/alert/text
2023-12-08T12:19:28.999Z INFO webdriver: RESULT Click me button pressed
2023-12-08T12:19:29.000Z INFO webdriver: COMMAND acceptAlert()
2023-12-08T12:19:29.000Z INFO webdriver: [POST] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/alert/accept
2023-12-08T12:19:29.005Z INFO webdriver: RESULT null
2023-12-08T12:19:29.010Z INFO webdriver: COMMAND findElement("css selector", "#focusBtn")
2023-12-08T12:19:29.010Z INFO webdriver: [POST] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/element
2023-12-08T12:19:29.010Z INFO webdriver: DATA { using: 'css selector', value: '#focusBtn' }
2023-12-08T12:19:29.028Z INFO webdriver: RESULT {
  'element-6066-11e4-a52e-4f735466cecf': 'DEF1A4702D64691DD327661DC99BBB81_element_4'
}
2023-12-08T12:19:29.033Z INFO webdriver: COMMAND elementClick("DEF1A4702D64691DD327661DC99BBB81_element_4")
2023-12-08T12:19:29.034Z INFO webdriver: [POST] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/element/DEF1A4702D64691DD327661DC99BBB81_element_4/click
2023-12-08T12:19:29.074Z INFO webdriver: RESULT null
2023-12-08T12:19:29.075Z INFO webdriver: COMMAND performActions(<object>)
2023-12-08T12:19:29.075Z INFO webdriver: [POST] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/actions
2023-12-08T12:19:29.075Z INFO webdriver: DATA {
  actions: [ { id: 'action1', type: 'key', parameters: {}, actions: [Array] } ]
}
2023-12-08T12:19:29.108Z INFO webdriver: RESULT null
2023-12-08T12:19:29.108Z INFO webdriver: COMMAND releaseActions()
2023-12-08T12:19:29.108Z INFO webdriver: [DELETE] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/actions
2023-12-08T12:19:29.112Z INFO webdriver: RESULT null
2023-12-08T12:19:30.113Z INFO webdriver: COMMAND performActions(<object>)
2023-12-08T12:19:30.114Z INFO webdriver: [POST] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/actions
2023-12-08T12:19:30.114Z INFO webdriver: DATA {
  actions: [ { id: 'action2', type: 'key', parameters: {}, actions: [Array] } ]
}
2023-12-08T12:19:30.121Z INFO webdriver: RESULT null
2023-12-08T12:19:30.121Z INFO webdriver: COMMAND releaseActions()
2023-12-08T12:19:30.121Z INFO webdriver: [DELETE] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/actions
2023-12-08T12:19:31.148Z INFO webdriver: RESULT null
2023-12-08T12:19:36.158Z INFO webdriver: COMMAND getAlertText()
2023-12-08T12:19:36.160Z INFO webdriver: [GET] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/alert/text
2023-12-08T12:19:36.175Z WARN webdriver: Request failed with status 404 due to no such alert
  (Session info: chrome=120.0.6099.63)
2023-12-08T12:19:36.175Z INFO webdriver: Retrying 1/3
2023-12-08T12:19:36.175Z INFO webdriver: [GET] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/alert/text
2023-12-08T12:19:36.179Z WARN webdriver: Request failed with status 404 due to no such alert
  (Session info: chrome=120.0.6099.63)
2023-12-08T12:19:36.179Z INFO webdriver: Retrying 2/3
2023-12-08T12:19:36.179Z INFO webdriver: [GET] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/alert/text
2023-12-08T12:19:36.182Z WARN webdriver: Request failed with status 404 due to no such alert
  (Session info: chrome=120.0.6099.63)
2023-12-08T12:19:36.182Z INFO webdriver: Retrying 3/3
2023-12-08T12:19:36.182Z INFO webdriver: [GET] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/alert/text
2023-12-08T12:19:36.186Z ERROR webdriver: Request failed with status 404 due to no such alert: no such alert
  (Session info: chrome=120.0.6099.63)
2023-12-08T12:19:36.191Z INFO webdriver: COMMAND getLogTypes()
2023-12-08T12:19:36.191Z INFO webdriver: [GET] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/se/log/types
2023-12-08T12:19:36.193Z INFO webdriver: RESULT [ 'browser', 'driver' ]
2023-12-08T12:19:36.194Z INFO webdriver: COMMAND getLogs("driver")
2023-12-08T12:19:36.194Z INFO webdriver: COMMAND getLogs("browser")
2023-12-08T12:19:36.195Z INFO webdriver: [POST] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/se/log
2023-12-08T12:19:36.195Z INFO webdriver: DATA { type: 'driver' }
2023-12-08T12:19:36.196Z INFO webdriver: [POST] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30/se/log
2023-12-08T12:19:36.196Z INFO webdriver: DATA { type: 'browser' }
2023-12-08T12:19:36.199Z INFO webdriver: RESULT []
2023-12-08T12:19:36.200Z INFO webdriver: RESULT []
2023-12-08T12:19:36.200Z INFO webdriver: COMMAND deleteSession()
2023-12-08T12:19:36.200Z INFO webdriver: [DELETE] http://0.0.0.0:50899/session/3d1848e5a20c801d39b26832e09e6a30
2023-12-08T12:19:36.318Z INFO webdriver: RESULT null
2023-12-08T12:19:36.318Z INFO webdriver: Kill C:\Users\userA\AppData\Local\Temp\chromedriver\win64-120.0.6099.71\chromedriver-win64\chromedriver.exe driver process with command line: --port=50899 --allowed-origins=* --allowed-ips=0.0.0.0

Code of Conduct

  • I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions