From 75c8fb41491b0f3b7402e6130ad446cd95112d89 Mon Sep 17 00:00:00 2001 From: Isaac Murchie Date: Wed, 20 Aug 2014 14:10:03 -0700 Subject: [PATCH] Add set_text method for Android --- appium/webdriver/mobilecommand.py | 1 + appium/webdriver/webdriver.py | 2 ++ appium/webdriver/webelement.py | 19 +++++++++++++++++++ test/functional/android/appium_tests.py | 22 ++++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/appium/webdriver/mobilecommand.py b/appium/webdriver/mobilecommand.py index 2db3f537..8afd03a7 100644 --- a/appium/webdriver/mobilecommand.py +++ b/appium/webdriver/mobilecommand.py @@ -49,3 +49,4 @@ class MobileCommand(object): SHAKE = 'shake' RESET = 'reset' HIDE_KEYBOARD = 'hideKeyboard' + REPLACE_KEYS = 'replaceKeys' diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index cc7696f2..5adeb3b3 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -694,6 +694,8 @@ def _addCommands(self): ('POST', '/session/$sessionId/ime/deactivate') self.command_executor._commands[Command.GET_ACTIVE_IME_ENGINE] = \ ('GET', '/session/$sessionId/ime/active_engine') + self.command_executor._commands[Command.REPLACE_KEYS] = \ + ('POST', '/session/$sessionId/appium/element/$elementId/replace_value') # monkeypatched method for WebElement diff --git a/appium/webdriver/webelement.py b/appium/webdriver/webelement.py index a05924ab..01b52a3b 100644 --- a/appium/webdriver/webelement.py +++ b/appium/webdriver/webelement.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from .mobilecommand import MobileCommand as Command + from selenium.webdriver.common.by import By from selenium.webdriver.remote.webelement import WebElement as SeleniumWebElement @@ -84,3 +86,20 @@ def find_elements_by_accessibility_id(self, id): driver.find_elements_by_accessibility_id() """ return self.find_elements(by=By.ACCESSIBILITY_ID, value=id) + + def set_text(self, keys=''): + """Sends text to the element. Previous text is removed. + Android only. + + :Args: + - keys - the text to be sent to the element. + + :Usage: + element.set_text('some text') + """ + data = { + 'elementId': self._id, + 'value': [keys] + } + self._execute(Command.REPLACE_KEYS, data) + return self diff --git a/test/functional/android/appium_tests.py b/test/functional/android/appium_tests.py index c74ad6a7..de1713c6 100644 --- a/test/functional/android/appium_tests.py +++ b/test/functional/android/appium_tests.py @@ -173,6 +173,28 @@ def test_open_notifications(self): sleep(1) self.driver.find_element_by_android_uiautomator('new UiSelector().text(":-|")') + def test_set_text(self): + self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Views").instance(0));').click() + self.driver.find_element_by_name('Controls').click() + self.driver.find_element_by_name('1. Light Theme').click() + + el = self.driver.find_element_by_class_name('android.widget.EditText') + el.send_keys('original text') + el.set_text('new text') + + self.assertEqual('new text', el.text) + + def test_send_keys(self): + self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Views").instance(0));').click() + self.driver.find_element_by_name('Controls').click() + self.driver.find_element_by_name('1. Light Theme').click() + + el = self.driver.find_element_by_class_name('android.widget.EditText') + el.send_keys('original text') + el.send_keys(' and new text') + + self.assertEqual('original text and new text', el.text) + if __name__ == "__main__": suite = unittest.TestLoader().loadTestsFromTestCase(AppiumTests)