Amir E. Aharoni
Amir E. Aharoni

Reputation: 1318

Can I use Selenium's send_keys or some other function to send actual keys rather than characters?

I am using Cucumber and Selenium to test typing in a contenteditable div. (Wikipedia's new VisualEditor, if you're curious.)

This is how the current code looks, more or less:

When(/^I type (.+)$/) do |text|
  on(VisualEditorPage).content_element.send_keys(text)
end

I'd love to test not just typing of strings, but to test actual keyboard keys. The reason I need to do this is that I want to test typing in different languages and different keyboard layouts. The English keyboard has simple one-to-one mapping between keys and characters, but this is not true for all keyboards; Japanese is a very extreme example - some Latin letters are typed there, and then they are converted to a list of possible Japanese spellings from which the users can choose the desired thing. Testing this by simply using send_keys with the Japanese characters misses the point, because that is not what the end user actually does.

In a simpler scenario, I want to test pressing keys when a Burmese or Sinhalese keyboard layout is on and test that the result text is correct; just pasting a whole string is not great, because the keyboard layout re-positions the characters while typing according to smart rules.

So - is there anything in Selenium that allows this? Instead of something like send_keys 'abc' to do something like send_keys :a_key, :b_key, :c_key?

Thanks!

Upvotes: 1

Views: 769

Answers (1)

Amey
Amey

Reputation: 8548

I have not tried this out personally, but I guess this document - selenium.webdriver.common.keys along with this document Legacy Keyboard Event Properties, should help you with your first attempt.

So a send_keys('\u0041\u0042\u0043') should hopefully equate to capital Latin ABC.

Upvotes: 2

Related Questions