Using Special Keys in Selenium IDE

Using Special Keys in Selenium IDE

Using Special Keys in Selenium IDE – Part 1

I added special keys support to the sendKeys command in Selenium IDE v2.3.0. Not just in normal playback, but also in Webdriver playback. Along with this, I added special keys support to all the officially supported formatters for Java, Ruby, Python and C#. And also the Perl Webdriver formatter. Yes, the Perl Webdriver formatter, it exists and will be released soon. It is unofficial, but I will maintain it to the level of other officially supported formatters. Wait for it.

To make all this possible I had to come up with a strategy that works seemlessly with all these different ways of using sendKeys. Using the special keys in sendKeys is really simple. You use it like a normal stored variable with a special “KEY_” prefix. For example, to press the ENTER key you would write KEY_ENTER as an variable, i.e. ${KEY_ENTER}.

sendKeys | id=search | ${KEY_ENTER}

Combining Text and the Enter Key

Combining normal text with special keys is really simple. Simply, embed the special key in the text, i.e. cheese${KEY_ENTER}.

sendKeys | id=search | cheese${KEY_ENTER}

The above command would type cheese in to the search box and press the enter key, causing it to search.

Using the Shift Key

The shift and the arrow keys are usually useful for selecting text. To press the shift and the left keys, you can use them like this.

sendKeys | id=search | gx${KEY_SHIFT}${KEY_LEFT}${KEY_SHIFT}o

In the above command, the first shift will press it, then press the left key then release the shift, resulting in the text go in the search field.

Using the Control Key

The usage of the control key is the same as the shift key. To press the control and left keys, which on some browsers and platforms means go to the begining of the text in the field, you can do this.

sendKeys | id=search | o${KEY_CTRL}${KEY_LEFT}${KEY_CTRL}g

In the above command, the first control will press it, then press the left key then release the control, resulting again in the text go in the search field.

Using the Meta / Command Key

On the Mac keyboards, you have the command or meta key which is again used in the same way as the shift or control key.

Why Doesn’t Tab Key Work?

The sendKeys command sends the keys to a field on the webpage. Some browsers intercept certain keys even before they reach the field. These include menu accelerator keys and sometimes keys like tab. In regular playback mode, Selenium IDE uses the Selenium atoms for sendKeys. While this give a lot more functionality, it is not the same as pressing the keys natively. Webdriver playback however supports this under certain circumstances. So you might see a test that tries to send the tab key work under Webdriver playback and fail under regular playback.

Why Doesn’t My Combination Work?

First read the section about ‘Why doesn’t tab key work?’. There will be come combinations that will not work. If you let me know, I can investigate and try to come up with an explaination about it or document it so that more people are aware of what does and does not work.

The part 2 provides the full list of all supported special keys.

 

Using Special Keys in Selenium IDE – Part 2

In part 1, we saw how to use the special keys in the sendKeys command in Selenium IDE starting in v2.3.0. Selenium IDE v2.4.0 shortened some key names. This post serves as a quick reference and provides the full list of all supported special keys and some notes. If there are two options given, you can use either one of them.

Using the Special Keys

The special keys are implemented as stored variables. You use them similar to the way you use the special keys. i.e. enclose them in ${}, e.g.:

${KEY_ENTER}

If you want to use more than one special keys, each needs to be enclosed in ${}, e.g.:

${KEY_LEFT}${KEY_BKSP}

You can mix and match regular text and special keys as you like. e.g.:

Everybody loves chese${KEY_LEFT}${KEY_LEFT}e

Keep in mind that you should not create stored variables with the same names as special keys in your tests. It is recommended that you do not create any stored variables that begin with the KEY_ prefix.

Using the Modifier Keys

The first occurance of a modifier key presses them and the second one releases it. If a modifier key is used more than twice, then, it is easier to think that the odd times are presses and the even times are releases. All modifier keys are released at the end of the sendKeys command and will not affect the subsequent sendKeys command.

Supported Modifier Keys

  • KEY_ALT
  • KEY_CONTROL / KEY_CTRL
  • KEY_META / KEY_COMMAND
  • KEY_SHIFT

Supported Common Keys

  • KEY_BKSP / KEY_BACKSPACE
  • KEY_DEL / KEY_DELETE
  • KEY_ENTER
  • KEY_EQUALS
  • KEY_ESC / KEY_ESCAPE
  • KEY_INS / KEY_INSERT
  • KEY_PAUSE
  • KEY_SEMICOLON
  • KEY_SPACE
  • KEY_TAB

Supported Navigation Keys

  • KEY_LEFT
  • KEY_UP
  • KEY_RIGHT
  • KEY_DOWN
  • KEY_PGUP / KEY_PAGE_UP
  • KEY_PGDN / KEY_PAGE_DOWN
  • KEY_END
  • KEY_HOME

Supported Number Pad Keys

  • KEY_NUMPAD0 / KEY_N0
  • KEY_NUMPAD1 / KEY_N1
  • KEY_NUMPAD2 / KEY_N2
  • KEY_NUMPAD3 / KEY_N3
  • KEY_NUMPAD4 / KEY_N4
  • KEY_NUMPAD5 / KEY_N5
  • KEY_NUMPAD6 / KEY_N6
  • KEY_NUMPAD7 / KEY_N7
  • KEY_NUMPAD8 / KEY_N8
  • KEY_NUMPAD9 / KEY_N9
  • KEY_ADD / KEY_NUM_PLUS
  • KEY_DECIMAL/ KEY_NUM_PERIOD
  • KEY_DIVIDE/ KEY_NUM_DIVISION
  • KEY_MULTIPLY/ KEY_NUM_MULTIPLY
  • KEY_SEPARATOR/ KEY_SEP
  • KEY_SUBTRACT/ KEY_NUM_MINUS

Supported Function Keys

  • KEY_F1
  • KEY_F2
  • KEY_F3
  • KEY_F4
  • KEY_F5
  • KEY_F6
  • KEY_F7
  • KEY_F8
  • KEY_F9
  • KEY_F10
  • KEY_F11
  • KEY_F12

The Future of Special Keys

While I think that this feature is done, I have a couple of more ideas that may make it even better. The first idea is to introduce shorter forms for commonly used keys. The second idea is to provide a way to specify the number of times to press a key. Without any feedback of how many people actually use this, it will be difficult to prioritise these improvements. So, leave a comment, send me a tweet, get in touch!

Comments are closed.