From 8c4b361f244a760bf8a7a97afd0d150d42877d15 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sat, 11 Apr 2020 16:20:37 +0900 Subject: [PATCH 1/5] add send_keys_direct --- appium/webdriver/webelement.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/appium/webdriver/webelement.py b/appium/webdriver/webelement.py index bf8efc5b..af21528a 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,16 @@ def set_value(self, value): } self._execute(Command.SET_IMMEDIATE_VALUE, data) return self + + def send_keys_direct(self, *value): + """Simulates typing into the element. + + Please consider to use this method if WebElement#send_keys did not work. + WebElement#send_keys works uploading a local file, but this method simply + sends given string without such uploading. + + Argument is the same as WebElement#send_keys + """ + self._execute(RemoteCommand.SEND_KEYS_TO_ELEMENT, + {'text': "".join(keys_to_typing(value)), + 'value': keys_to_typing(value)}) From d140b510aca456a302e8cb4960bdcdc01d791e45 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 12 Apr 2020 00:13:06 +0900 Subject: [PATCH 2/5] override send_keys --- appium/webdriver/webelement.py | 12 +++++++----- test/functional/ios/keyboard_tests.py | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/appium/webdriver/webelement.py b/appium/webdriver/webelement.py index af21528a..4a02169f 100644 --- a/appium/webdriver/webelement.py +++ b/appium/webdriver/webelement.py @@ -206,15 +206,17 @@ def set_value(self, value): self._execute(Command.SET_IMMEDIATE_VALUE, data) return self - def send_keys_direct(self, *value): + # Override + def send_keys(self, *value): """Simulates typing into the element. - Please consider to use this method if WebElement#send_keys did not work. - WebElement#send_keys works uploading a local file, but this method simply - sends given string without such uploading. + Args: + value (str): A string for typing. - Argument is the same as WebElement#send_keys + Returns: + `appium.webdriver.webelement.WebElement` """ self._execute(RemoteCommand.SEND_KEYS_TO_ELEMENT, {'text': "".join(keys_to_typing(value)), 'value': keys_to_typing(value)}) + return self diff --git a/test/functional/ios/keyboard_tests.py b/test/functional/ios/keyboard_tests.py index 7ae62347..f081754c 100644 --- a/test/functional/ios/keyboard_tests.py +++ b/test/functional/ios/keyboard_tests.py @@ -52,7 +52,7 @@ def test_hide_keyboard_no_key_name(self): el = self.driver.find_elements_by_class_name('XCUIElementTypeTextField')[0] el.click() - el.set_value('Testing') + el.send_keys('Testing') el = self.driver.find_element_by_class_name('UIAKeyboard') self.assertTrue(el.is_displayed()) @@ -68,7 +68,7 @@ def test_is_keyboard_shown(self): el = self.driver.find_elements_by_class_name('XCUIElementTypeTextField')[0] el.click() - el.set_value('Testing') + el.send_keys('Testing') self.assertTrue(self.driver.is_keyboard_shown()) def _move_to_textbox(self): From ef3da36695b2e76c6bab10a107fc0c04a113ee23 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 12 Apr 2020 01:46:46 +0900 Subject: [PATCH 3/5] tune --- appium/webdriver/webelement.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appium/webdriver/webelement.py b/appium/webdriver/webelement.py index 4a02169f..78074628 100644 --- a/appium/webdriver/webelement.py +++ b/appium/webdriver/webelement.py @@ -216,7 +216,7 @@ def send_keys(self, *value): Returns: `appium.webdriver.webelement.WebElement` """ + _value = keys_to_typing(value) self._execute(RemoteCommand.SEND_KEYS_TO_ELEMENT, - {'text': "".join(keys_to_typing(value)), - 'value': keys_to_typing(value)}) + {'text': "".join(_value), 'value': _value}) return self From 07c1ce4624e32d3ceffc3561ecdf1d8135d1824c Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 12 Apr 2020 01:47:10 +0900 Subject: [PATCH 4/5] add unittest instead of functional test --- test/functional/ios/keyboard_tests.py | 4 +- test/unit/webdriver/webelement_test.py | 61 ++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 test/unit/webdriver/webelement_test.py diff --git a/test/functional/ios/keyboard_tests.py b/test/functional/ios/keyboard_tests.py index f081754c..7ae62347 100644 --- a/test/functional/ios/keyboard_tests.py +++ b/test/functional/ios/keyboard_tests.py @@ -52,7 +52,7 @@ def test_hide_keyboard_no_key_name(self): el = self.driver.find_elements_by_class_name('XCUIElementTypeTextField')[0] el.click() - el.send_keys('Testing') + el.set_value('Testing') el = self.driver.find_element_by_class_name('UIAKeyboard') self.assertTrue(el.is_displayed()) @@ -68,7 +68,7 @@ def test_is_keyboard_shown(self): el = self.driver.find_elements_by_class_name('XCUIElementTypeTextField')[0] el.click() - el.send_keys('Testing') + el.set_value('Testing') self.assertTrue(self.driver.is_keyboard_shown()) def _move_to_textbox(self): diff --git a/test/unit/webdriver/webelement_test.py b/test/unit/webdriver/webelement_test.py new file mode 100644 index 00000000..703e0d43 --- /dev/null +++ b/test/unit/webdriver/webelement_test.py @@ -0,0 +1,61 @@ +#!/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 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']) From a9acfabdbdd1589113d576e33c6fe7216908ad8c Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Sun, 12 Apr 2020 11:21:55 +0900 Subject: [PATCH 5/5] tweak syntax --- appium/webdriver/webelement.py | 4 ++-- test/unit/webdriver/webelement_test.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/appium/webdriver/webelement.py b/appium/webdriver/webelement.py index 78074628..9c28a6b3 100644 --- a/appium/webdriver/webelement.py +++ b/appium/webdriver/webelement.py @@ -216,7 +216,7 @@ def send_keys(self, *value): Returns: `appium.webdriver.webelement.WebElement` """ - _value = keys_to_typing(value) + keys = keys_to_typing(value) self._execute(RemoteCommand.SEND_KEYS_TO_ELEMENT, - {'text': "".join(_value), 'value': _value}) + {'text': ''.join(keys), 'value': keys}) return self diff --git a/test/unit/webdriver/webelement_test.py b/test/unit/webdriver/webelement_test.py index 703e0d43..06b62036 100644 --- a/test/unit/webdriver/webelement_test.py +++ b/test/unit/webdriver/webelement_test.py @@ -13,6 +13,7 @@ # limitations under the License. import json +import os import tempfile import httpretty