With keypress steps, you can confirm the application under test responds as expected to a specific keyboard event. Examples of common keypress steps include:
- Pressing the escape key to close a modal
- Pressing the enter key to submit a search query
When the record button is on, the mabl Trainer automatically records keypress steps. If you cannot target the correct element with a recorded keypress, you may create a custom find keypress step. Custom find keypress steps use a CSS or XPath query to target an element and send a keypress directly to it.
This article outlines which types of keypresses are supported in mabl tests.
![Sending an [ENTER] keypress](https://static.mabl.com/doc/readme/img/7e1d59c-enter_keypress.png)
A keypress step in the Trainer
Supported keypresses
mabl can record most single keypresses, including letters, numbers, and many special characters, such as !, ?, /, #, and ~. Some special characters are spelled out instead of using the character symbols, including:
- [ENTER]
- [TAB]
- [BACKSPACE]
- [DELETE]
- [ARROWUP]
- [ARROWDOWN]
- [ARROWLEFT]
- [ARROWRIGHT]
- [ESCAPE]
The space keypress is represented by one space between two quotation marks: ” “. To create a space keypress in a custom find keypress step, put one space between two square brackets: [ ].
Modifier keys
You can also record keypress steps using modifier keys, including Alt, Ctrl, Meta, and Shift.
Handling OS differences with modifier keys
mabl browser tests execute on Linux-based runners in the cloud. It’s important to note that mabl doesn’t automatically translate differences between modifier keys across operating systems. For example, if you train keypress steps with modifier keys on a Mac or Windows, they may not behave as expected on the Linux runner in the cloud.
To address this, you can add conditional logic to define keypress logic based on the execution environment:
- If the value of
app.test_run_idequalslocal-run, execute the Mac-specific or Windows-specific keypress. - Otherwise, execute the Linux-compatible keypress.
Keyboard shortcuts
Keyboard shortcuts are only supported for applications that use event listeners for specific key up or key down events. For example, you can record a keypress step to save a draft using ctrl + shift + s if that keyboard shortcut is part of your application’s code.
Global keyboard shortcuts, such as copying to the clipboard with ctrl + c and ctrl + v, are handled at the system level and are not supported in mabl browser tests.