Pages

Monday 8 April 2013

Adding the missing element back into your Watir tests... the keyboard. #watir #webdriver #testing





Sometimes it can feel that your test suite, like this keyboard, is missing all it's keys. But fear not, it's an easy problem to fix... 







To me, automating doesn't always mean removing the physical, sometimes it means faking it


99% of the time I will .set a text area/field. However, every now and then I will need to simulate a user typing, key by key. Or I will encounter a bit of Javascript (TinyMCE for instance), that will stubbornly refuse to be set. That's when it's important to know how to fake an actual key press.


Here's how to use your very own fake keyboard


And as with almost everything on this blog, it's easier than you might think.

browser.send_keys :escape 

And that's it. But even better, you can combine key presses. Need to select all?

browser.send_keys [:control, :a]  

Here's a list of all the special keys sendable using this method (as well as the standard alphanumerics):

  • :null
  • :cancel
  • :help
  • :backspace
  • :tab
  • :clear
  • :return
  • :enter
  • :shift
  • :left_shift
  • :control
  • :left_control
  • :alt
  • :left_alt
  • :pause
  • :escape
  • :space
  • :page_up
  • :page_down
  • :end
  • :home
  • :left
  • :arrow_left
  • :up
  • :arrow_up
  • :right
  • :arrow_right
  • :down
  • :arrow_down
  • :insert
  • :delete
  • :semicolon
  • :equals
  • :numpad0
  • :numpad1
  • :numpad2
  • :numpad3
  • :numpad4
  • :numpad5
  • :numpad6
  • :numpad7
  • :numpad8
  • :numpad9
  • :multiply
  • :add
  • :separator
  • :subtract
  • :decimal
  • :divide
  • :f1
  • :f2
  • :f3
  • :f4
  • :f5
  • :f6
  • :f7
  • :f8
  • :f9
  • :f10
  • :f11
  • :f12
  • :meta
  • :command
If you found this article helpful please share it using the relevant icons below. If you have another method for sending keys or setting text areas in Javascript please comment below or tweet me.

Photo: Keyboard

No comments:

Post a Comment