Skip to content

Commit d11f6a4

Browse files
committed
Merge pull request #474 from HelioGuilherme66/press_key_#275
Add named keys to Press Key.
2 parents d6a42af + 837f224 commit d11f6a4

File tree

2 files changed

+65
-41
lines changed

2 files changed

+65
-41
lines changed

src/Selenium2Library/keywords/_element.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -479,19 +479,25 @@ 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 lead by '\\\\',
483+
or a NAMED KEY as described in the [https://selenium.googlecode.com/git/docs/api/py/webdriver/selenium.webdriver.common.keys.html|Selenium docs].
484484
485485
Examples:
486-
| Press Key | text_field | q |
487-
| Press Key | login_button | \\\\13 | # ASCII code for enter key |
486+
| Press Key | text_field | q | # The letter 'q' |
487+
| Press Key | nav_console | ARROW_UP | # Named ARROW_UP key |
488+
| Press Key | login_button | \\\\13 | # ASCII code for Enter key |
489+
490+
It's recommended to use named keys over ascii escapes (.i.e ``ENTER`` over ``\\\\13``)
491+
492+
NAMED KEY value is new in Selenium2Library 1.7.3.
488493
"""
489-
if key.startswith('\\') and len(key) > 1:
490-
key = self._map_ascii_key_code_to_key(int(key[1:]))
491-
#if len(key) > 1:
492-
# raise ValueError("Key value '%s' is invalid.", key)
494+
if len(key) > 1:
495+
if key.startswith('\\'):
496+
key = self._map_ascii_key_code_to_key(int(key[1:]))
497+
else:
498+
key = self._map_named_key_code_to_special_key(key)
493499
element = self._element_find(locator, True, True)
494-
#select it
500+
# select it
495501
element.send_keys(key)
496502

497503
# Public, links
@@ -599,7 +605,7 @@ def get_matching_xpath_count(self, xpath):
599605
"""Returns number of elements matching `xpath`
600606
601607
One should not use the xpath= prefix for 'xpath'. XPath is assumed.
602-
608+
603609
Correct:
604610
| count = | Get Matching Xpath Count | //div[@id='sales-pop']
605611
Incorrect:
@@ -615,7 +621,7 @@ def xpath_should_match_x_times(self, xpath, expected_xpath_count, message='', lo
615621
"""Verifies that the page contains the given number of elements located by the given `xpath`.
616622
617623
One should not use the xpath= prefix for 'xpath'. XPath is assumed.
618-
624+
619625
Correct:
620626
| Xpath Should Match X Times | //div[@id='sales-pop'] | 1
621627
Incorrect:
@@ -748,6 +754,14 @@ def _map_ascii_key_code_to_key(self, key_code):
748754
key = chr(key_code)
749755
return key
750756

757+
def _map_named_key_code_to_special_key(self, key_name):
758+
try:
759+
return getattr(Keys, key_name)
760+
except AttributeError:
761+
message = "Unknown key named '%s'." % (key_name)
762+
self._debug(message)
763+
raise ValueError(message)
764+
751765
def _parse_attribute_locator(self, attribute_locator):
752766
parts = attribute_locator.rpartition('@')
753767
if len(parts[0]) == 0:
Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,50 @@
1-
*Setting*
2-
Variables variables.py
3-
Resource ../resource.robot
4-
Test Setup Go To Page "forms/prefilled_email_form.html"
5-
6-
7-
*Test Cases*
1+
*** Setting ***
2+
Test Setup Go To Page "forms/prefilled_email_form.html"
3+
Variables variables.py
4+
Resource ../resource.robot
85

6+
*** Test Cases ***
97
Get Value From Text Field
10-
${text} = Get Value name
11-
Should Be Equal ${text} Prefilled Name
12-
Clear Element Text name
13-
${text} = Get Value name
14-
Should Be Equal ${text} ${EMPTY}
15-
8+
${text} = Get Value name
9+
Should Be Equal ${text} Prefilled Name
10+
Clear Element Text name
11+
${text} = Get Value name
12+
Should Be Equal ${text} ${EMPTY}
13+
1614

1715
Input Unicode In Text Field
18-
Input Text name ${unic_text}
19-
${text} = Get Value name
20-
Should Be Equal ${text} ${unic_text}
16+
Input Text name ${unic_text}
17+
${text} = Get Value name
18+
Should Be Equal ${text} ${unic_text}
2119

2220
Input Password
23-
[Documentation] LOG 3 Typing password into text field 'password_field'
24-
[Setup] Go To Page "forms/login.html"
25-
Input Text username_field username
26-
Input Password password_field password
27-
Submit Form
28-
Verify Location Is "forms/submit.html"
21+
[Documentation] LOG 3 Typing password into text field 'password_field'
22+
[Setup] Go To Page "forms/login.html"
23+
Input Text username_field username
24+
Input Password password_field password
25+
Submit Form
26+
Verify Location Is "forms/submit.html"
2927

3028
Press Key
31-
[Setup] Go To Page "forms/login.html"
32-
Cannot Be Executed in IE
33-
Input Text username_field James Bond
34-
Press Key password_field f
35-
Press Key password_field \\9
36-
Press Key login_button \\10
37-
Verify Location Is "forms/submit.html"
29+
[Setup] Go To Page "forms/login.html"
30+
#Cannot Be Executed in IE
31+
Input Text username_field James Bond
32+
Press Key username_field HOME
33+
Press Key username_field END
34+
Press Key username_field ARROW_LEFT
35+
Press Key username_field ARROW_LEFT
36+
Press Key username_field ARROW_LEFT
37+
Press Key username_field DELETE
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
41+
Press Key username_field o
42+
${text} = Get Value username_field
43+
Should Be Equal ${text} James Blond
44+
Press Key password_field f
45+
Press Key password_field 9
46+
Press Key login_button ENTER
47+
Verify Location Is "forms/submit.html"
3848

3949
Attempt Clear Element Text On Non-Editable Field
40-
Run Keyword And Expect Error * Clear Element Text can_send_email
50+
Run Keyword And Expect Error * Clear Element Text can_send_email

0 commit comments

Comments
 (0)