Fix Browser Keys in Firefox with WebdriverIO
In this article
This guide shows you how to fix browser key commands (like shortcuts for back, forward, refresh, or custom key bindings) when using WebdriverIO with Firefox. If your tests fail because Firefox ignores certain key presses or sends the wrong key codes, the issue is usually related to how WebdriverIO sends key events or how Firefox handles them. By the end, you'll have your key interactions working reliably in Firefox.
What You'll Need
- Node.js (v14 or later) installed
- A WebdriverIO project set up (with @wdio/cli and @wdio/local-runner)
- Firefox browser installed (version 78 or later recommended)
- Geckodriver installed and in your PATH
- Basic knowledge of JavaScript and WebdriverIO syntax
Step-by-Step
- Identify the problem. Run your existing test that uses browser keys (e.g.,
browser.keys('Control')orbrowser.keys(['Control', 't'])) and note the exact failure. Common symptoms: keys not recognized, wrong characters typed, or the browser not responding to shortcuts. - Check your WebdriverIO version. Run
npx wdio --version. If you're on v7 or v8, the key handling is based on the WebDriver protocol'sactionsAPI. For older versions, you might need to update to get proper key support. - Update geckodriver and Firefox. Outdated geckodriver or Firefox can cause key event mismatches. Download the latest geckodriver from the official Mozilla repository and ensure Firefox is up to date. Replace the old geckodriver binary in your PATH.
- Use the correct key syntax. In WebdriverIO, keys are specified as strings. For special keys, use the predefined constants like
Key.EnterorKey.Control(available fromwebdriveriopackage). For example:import { Key } from 'webdriverio'; await browser.keys([Key.Control, 't']);. Avoid using raw Unicode characters for non-printable keys. - Test with a simple key press. Write a minimal test that sends a single key, like
await browser.keys('a'), and check if it types 'a' in an input field. If it works, the issue is with complex key combinations. - For key combinations, use the actions API. Instead of
browser.keys(), usebrowser.action('key')to create a key sequence. Example:await browser.action('key').down(Key.Control).down('t').up(Key.Control).up('t').perform();This gives more control over key down and up events. - Handle Firefox-specific quirks. Firefox sometimes requires a small delay between key presses. Add
await browser.pause(100)between key actions if you experience flakiness. - Disable native events (if needed). In your WebdriverIO config, set
moz:firefoxOptionswith the preferencenativeEventsset tofalse(though this is rarely needed). More commonly, ensure that Firefox is not in a restricted mode (like private browsing) that might block shortcuts. - Test in a clean profile. Create a fresh Firefox profile for testing to rule out extension interference. You can specify a profile in your config using
firefoxProfileormoz:firefoxOptions.profile. - Run your test again. After applying the above, run your original test. If it passes, you're done. If not, proceed to troubleshooting.
Common Mistakes
- Using
browser.keys()for key combinations without holding down modifier keys properly. For example, sending['Control', 't']as an array sends them sequentially, not simultaneously. Use the actions API for simultaneous presses. - Forgetting to import
Keyfrom WebdriverIO. Instead, hardcoding strings like'\uE009'for Control, which is error-prone. - Not waiting for the page to be ready before sending keys. If the page is still loading, key events may be lost.
- Ignoring the need for a clean Firefox profile. Extensions like ad-blockers can intercept key events.
Tips
- Always use
Keyconstants for special keys to avoid typos. - For keyboard shortcuts that open new tabs or windows, ensure your test handles the new window/tab (using
browser.switchWindowor similar). - If you're testing a web application's custom keyboard shortcuts, focus the correct element (like
document.body) before sending keys. - Use
browser.execute()to dispatch synthetic keyboard events as a fallback if WebdriverIO's native key events fail. - Keep your WebdriverIO and geckodriver versions in sync with Firefox's release cycle.
Troubleshooting
- Keys not recognized at all: Verify that geckodriver is running and that Firefox is launched with the correct profile. Check the WebdriverIO logs for any errors related to key actions.
- Wrong characters appear: This often happens when the keyboard layout is not set to US. In your Firefox profile, set
intl.keyboard.layouttoen-USvia preferences. - Key combinations trigger browser shortcuts instead of page actions: For example, Ctrl+T opens a new tab. To prevent this, you can use
browser.execute()to callpreventDefault()on keydown events, but that's tricky. Instead, test in a context where the shortcut is overridden by the page. - Flaky behavior: Add explicit waits and pauses, and try using the
action('key')API withdownandupevents separately. - Firefox crashes on key events: Update Firefox and geckodriver, and try a new profile. Also check if your system has any global hotkey software that might conflict.
Frequently Asked Questions
Why does browser.keys('Control') not work in Firefox?
It might be that the key is being sent as a regular character, not as a modifier. Use Key.Control from the WebdriverIO constants.
Can I use browser.keys() for keyboard shortcuts like Ctrl+C?
Yes, but you must send the keys down and up in the right order. Using the actions API is more reliable.
Why do my key presses work in Chrome but not Firefox?
Different browsers handle key events differently. Firefox may require a different approach, such as using the actions API or adjusting the profile.
How do I send a key to a specific element?
Use $(selector).keys() instead of browser.keys() to target a specific input field.
Is there a way to simulate key presses without geckodriver?
No, geckodriver is required for WebdriverIO to control Firefox. Make sure it's installed and up to date.
What if the page uses JavaScript to capture keyboard events?
WebdriverIO's key events are trusted events, so they should trigger JavaScript listeners. If not, use browser.execute() to dispatch synthetic events.