Skip to content

Commit d1f592e

Browse files
Deprecates the use of backslash escaping for ASCII and NAMED Keys.
1 parent c6da675 commit d1f592e

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

src/Selenium2Library/keywords/_element.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -479,20 +479,27 @@ def simulate(self, locator, event):
479479
def press_key(self, locator, key):
480480
"""Simulates user pressing key on element identified by `locator`.
481481
482-
`key` is either a single character, or a numerical ASCII code of the key
483-
lead by '\\\\'.
482+
`key` is either a single character, a numerical ASCII code of the key,\
483+
or a NAMED KEY as described at https://selenium.googlecode.com/git/docs/api/py/webdriver/selenium.webdriver.common.keys.html
484484
485485
Examples:
486-
| Press Key | text_field | q | |
487-
| Press Key | login_button | \\\\13 | # ASCII code for enter key |
488-
| Press Key | nav_console | \\\\\\\\ARROW_UP | # selenium.webdriver.common.keys ARROW_UP KEY |
489-
"""
490-
if key.startswith('\\\\') and len(key) > 1:
491-
key = getattr(Keys,key[2:])
492-
elif key.startswith('\\') and len(key) > 1:
493-
key = self._map_ascii_key_code_to_key(int(key[1:]))
494-
#if len(key) > 1:
495-
# raise ValueError("Key value '%s' is invalid.", key)
486+
| Press Key | text_field | q | |
487+
| Press Key | login_button | \\\\13 | # ASCII code for Enter key (DEPRECATED) |
488+
| Press Key | login_button | 10 | # ASCII code for Return key |
489+
| Press Key | nav_console | ARROW_UP | # selenium.webdriver.common.keys ARROW_UP KEY |
490+
491+
NAMED KEY value is new in Selenium2Library 1.7.3.
492+
"""
493+
if len(key) > 1:
494+
if key.startswith('\\'):
495+
self._warn("Press Key: Escaped ASCII codes are deprecated. Use plain numeric value: '%s'" % (key[1:]))
496+
key = self._map_ascii_key_code_to_key(int(key[1:]))
497+
else:
498+
try:
499+
key = (key.isdecimal() and self._map_ascii_key_code_to_key(int(key))) or\
500+
((not key.isdecimal()) and self._map_named_key_code_to_special_key(key))
501+
except:
502+
raise ValueError("Key value '%s' is invalid." % (key))
496503
element = self._element_find(locator, True, True)
497504
#select it
498505
element.send_keys(key)
@@ -751,6 +758,15 @@ def _map_ascii_key_code_to_key(self, key_code):
751758
key = chr(key_code)
752759
return key
753760

761+
def _map_named_key_code_to_special_key(self, key_name):
762+
try:
763+
return getattr(Keys, key_name)
764+
except:
765+
message = "Unknown key named '%s'." % (key_name)
766+
self._debug(message)
767+
raise ValueError(message)
768+
return Keys.NULL
769+
754770
def _parse_attribute_locator(self, attribute_locator):
755771
parts = attribute_locator.rpartition('@')
756772
if len(parts[0]) == 0:

test/acceptance/keywords/textfields.robot

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@ Press Key
3030
[Setup] Go To Page "forms/login.html"
3131
#Cannot Be Executed in IE
3232
Input Text username_field James Bond
33-
Press Key username_field \\\\HOME
34-
Press Key username_field \\\\END
35-
Press Key username_field \\\\ARROW_LEFT
36-
Press Key username_field \\\\ARROW_LEFT
37-
Press Key username_field \\\\ARROW_LEFT
38-
Press Key username_field \\\\ARROW_LEFT
39-
Press Key username_field \\\\ARROW_RIGHT
40-
Press Key username_field \\108 #This is the 'l' char
33+
Press Key username_field HOME
34+
Press Key username_field END
35+
Press Key username_field ARROW_LEFT
36+
Press Key username_field ARROW_LEFT
37+
Press Key username_field ARROW_LEFT
38+
Press Key username_field DELETE
39+
Press Key username_field ARROW_LEFT
40+
Press Key username_field ARROW_RIGHT
41+
Press Key username_field \\108 #This is the 'l' char (deprecated style)
42+
Press Key username_field 111 #This is the 'o' char
4143
${text} = Get Value username_field
4244
Should Be Equal ${text} James Blond
4345
Press Key password_field f
44-
Press Key password_field \\9
45-
Press Key login_button \\10
46+
Press Key password_field 9
47+
Press Key login_button 10
4648
Verify Location Is "forms/submit.html"
4749

4850
Attempt Clear Element Text On Non-Editable Field

0 commit comments

Comments
 (0)