Skip to content

Commit 34427a1

Browse files
authored
feat: Override send_keys without file upload function (appium#515)
* add send_keys_direct * override send_keys * tune * add unittest instead of functional test * tweak syntax
1 parent 4641b45 commit 34427a1

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

appium/webdriver/webelement.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
from selenium.webdriver.common.by import By
16+
from selenium.webdriver.common.utils import keys_to_typing
1617
from selenium.webdriver.remote.command import Command as RemoteCommand
1718

1819
from .extensions.search_context import AppiumWebElementSearchContext
@@ -204,3 +205,18 @@ def set_value(self, value):
204205
}
205206
self._execute(Command.SET_IMMEDIATE_VALUE, data)
206207
return self
208+
209+
# Override
210+
def send_keys(self, *value):
211+
"""Simulates typing into the element.
212+
213+
Args:
214+
value (str): A string for typing.
215+
216+
Returns:
217+
`appium.webdriver.webelement.WebElement`
218+
"""
219+
keys = keys_to_typing(value)
220+
self._execute(RemoteCommand.SEND_KEYS_TO_ELEMENT,
221+
{'text': ''.join(keys), 'value': keys})
222+
return self
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import json
16+
import os
17+
import tempfile
18+
19+
import httpretty
20+
21+
from appium.webdriver.webelement import WebElement as MobileWebElement
22+
from test.unit.helper.test_helper import (
23+
android_w3c_driver,
24+
appium_command,
25+
get_httpretty_request_body
26+
)
27+
28+
29+
class TestWebElement(object):
30+
31+
@httpretty.activate
32+
def test_send_key(self):
33+
driver = android_w3c_driver()
34+
httpretty.register_uri(
35+
httpretty.POST,
36+
appium_command('/session/1234567890/element/element_id/value')
37+
)
38+
39+
element = MobileWebElement(driver, 'element_id', w3c=True)
40+
element.send_keys('happy testing')
41+
42+
d = get_httpretty_request_body(httpretty.last_request())
43+
assert d['text'] == ''.join(d['value'])
44+
45+
@httpretty.activate
46+
def test_send_key_with_file(self):
47+
driver = android_w3c_driver()
48+
# Should not send this file
49+
tmp_f = tempfile.NamedTemporaryFile()
50+
httpretty.register_uri(
51+
httpretty.POST,
52+
appium_command('/session/1234567890/element/element_id/value')
53+
)
54+
55+
try:
56+
element = MobileWebElement(driver, 'element_id', w3c=True)
57+
element.send_keys(tmp_f.name)
58+
finally:
59+
tmp_f.close()
60+
61+
d = get_httpretty_request_body(httpretty.last_request())
62+
assert d['text'] == ''.join(d['value'])

0 commit comments

Comments
 (0)