diff --git a/appium/webdriver/webelement.py b/appium/webdriver/webelement.py index bf8efc5b..9c28a6b3 100644 --- a/appium/webdriver/webelement.py +++ b/appium/webdriver/webelement.py @@ -13,6 +13,7 @@ # limitations under the License. from selenium.webdriver.common.by import By +from selenium.webdriver.common.utils import keys_to_typing from selenium.webdriver.remote.command import Command as RemoteCommand from .extensions.search_context import AppiumWebElementSearchContext @@ -204,3 +205,18 @@ def set_value(self, value): } self._execute(Command.SET_IMMEDIATE_VALUE, data) return self + + # Override + def send_keys(self, *value): + """Simulates typing into the element. + + Args: + value (str): A string for typing. + + Returns: + `appium.webdriver.webelement.WebElement` + """ + keys = keys_to_typing(value) + self._execute(RemoteCommand.SEND_KEYS_TO_ELEMENT, + {'text': ''.join(keys), 'value': keys}) + return self diff --git a/test/unit/webdriver/webelement_test.py b/test/unit/webdriver/webelement_test.py new file mode 100644 index 00000000..06b62036 --- /dev/null +++ b/test/unit/webdriver/webelement_test.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import os +import tempfile + +import httpretty + +from appium.webdriver.webelement import WebElement as MobileWebElement +from test.unit.helper.test_helper import ( + android_w3c_driver, + appium_command, + get_httpretty_request_body +) + + +class TestWebElement(object): + + @httpretty.activate + def test_send_key(self): + driver = android_w3c_driver() + httpretty.register_uri( + httpretty.POST, + appium_command('/session/1234567890/element/element_id/value') + ) + + element = MobileWebElement(driver, 'element_id', w3c=True) + element.send_keys('happy testing') + + d = get_httpretty_request_body(httpretty.last_request()) + assert d['text'] == ''.join(d['value']) + + @httpretty.activate + def test_send_key_with_file(self): + driver = android_w3c_driver() + # Should not send this file + tmp_f = tempfile.NamedTemporaryFile() + httpretty.register_uri( + httpretty.POST, + appium_command('/session/1234567890/element/element_id/value') + ) + + try: + element = MobileWebElement(driver, 'element_id', w3c=True) + element.send_keys(tmp_f.name) + finally: + tmp_f.close() + + d = get_httpretty_request_body(httpretty.last_request()) + assert d['text'] == ''.join(d['value'])