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.

How can I send an keypress without using a target? I have a Firefox Confirm pop-up that appears so I don’t know it’s ID or name. If I “blindly” press the Enter key when the popup appears, it works for me. (the Resend button is pressed)