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.

Follow samitbadle on Twitter

This entry was posted in Selenium IDE and tagged , , , , , , , , , . Bookmark the permalink.

8 Responses to Using Special Keys in Selenium IDE – Part 1

  1. Pingback: Using Special Keys in Selenium IDE – Part 2 | Really Simple Thoughts of a Perpetual Learner

  2. Larry says:

    Holly Cow!!! I have been looking for this answer for hours.. Thank you so much.

  3. David says:

    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)

    • Samit Badle says:

      Hi David,

      You need a target. If the confirmation is from Firefox and not the webpage, you cannot automate it. The solution usually is to find the setting that stops Firefox from showing the confirmation dialog.

      Cheers,
      Samit

  4. vlodi says:

    Thanks for great feature!
    I would appreciate your help. It looks like it is working except CTRL key… I tried many combinations and it looks like control key is not behaving as should.

    In your example:
    sendKeys | id=search | o${KEY_CTRL}${KEY_LEFT}${KEY_CTRL}g

    try add second letter at the begging, you will see that it will produce: ogo
    At least it is my case…

    Thanks!

    • Samit Badle says:

      Hi vlodi,

      Please raise an issue on the selenium issues website on google code. You can put more information there.

      Cheers,
      Samit

  5. Emmanuel Angelo.R says:

    As for tab-key we can use fireevent command and target should be as ‘blur’

Leave a Reply

Your email address will not be published. Required fields are marked *