SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.09k stars 8.09k forks source link

FirefoxDriver: keyCode values for keydown, keyup events are different #639

Closed asashour closed 7 years ago

asashour commented 9 years ago

Hi all,

I am not sure if this is related to #510

With:

For the below html, and typing !, real firefox prints

down: 16,0 down: 49,0 press: 0,33 up: 49,0 up: 16,0

however, WebDriver prints down: 16,0 down: 33,0 press: 0,33 up: 33,0 up: 16,0

HTML:

<html><head><script>
  function appendMessage(message) {
    document.getElementById('result').innerHTML += message + ' ';
  }
</script></head>
<body >
  <input id='input1' onkeyup="appendMessage('up: ' + event.keyCode + ',' + event.charCode)"
  onkeypress="appendMessage('press: ' + event.keyCode + ',' + event.charCode)"
  onkeydown="appendMessage('down: ' + event.keyCode + ',' + event.charCode)"><br>
<p id='result'></p>
</body></html>

Java

        WebDriver driver = new FirefoxDriver();
        driver.get("http://url");
        final WebElement input = driver.findElement(By.id("input1"));
        final WebElement result = driver.findElement(By.id("result"));

        input.sendKeys("!");
        System.out.println(result.getText());

Please let me know if I should submit a PR with @Ignore test case in TypingTest

juangj commented 8 years ago

I've recently encountered this problem as well. Specifically, I have a test of a terminal emulator that is sensitive to the actual keyCodes of the keydown event. For example, pressing the hyphen ('-') key should send keycode 173, but FirefoxDriver sends keyCode 45, which corresponds to the Insert key. The wrong keycode causes the terminal emulator to inappropriately call preventDefault() on the event (and as of https://github.com/SeleniumHQ/selenium/commit/94d41301a809d3b7de0791b41ac3fd950d4a24fb, that prevents the ensuing keypress event from firing).

The hyphen problem is fixable by adding something like this to https://github.com/SeleniumHQ/selenium/blob/master/javascript/firefox-driver/js/utils.js:

    } else if (c == '-') {
      keyCode = Components.interfaces.nsIDOMKeyEvent.DOM_VK_HYPHEN_MINUS;
      charCode = c.charCodeAt(0);
    }

But for the symbols that appear on number keys (such as !@#, etc.), I'm not sure if that is the right approach. In @asashour's example, '!' should have keyCode 49 (the keyCode for the '1' key). There is a keyCode available for a '!' key (DOM_VK_EXCLAMATION = 161), and that would probably work, but it wouldn't really simulate the typical way that users send '!'.

@barancev Any opinions on how we could fix the keycodes here? I would be happy to contribute as needed.

barancev commented 7 years ago

This issue is not going to be fixed in legacy Firefox driver, no matter if it is still actual or not.

For the new Firefox driver implementation (aka geckodriver) watch https://github.com/mozilla/geckodriver/issues/646