Skip to content

Commit 19531cb

Browse files
committed
Merge pull request #38 from appium/isaac-ime
Add methods for Android ime access
2 parents 0a48aee + e5e2025 commit 19531cb

File tree

3 files changed

+132
-2
lines changed

3 files changed

+132
-2
lines changed

appium/webdriver/mobilecommand.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ class MobileCommand(object):
2020
TOUCH_ACTION = 'touchAction'
2121
MULTI_ACTION = 'multiAction'
2222
OPEN_NOTIFICATIONS = 'openNotifications'
23+
GET_NETWORK_CONNECTION = 'getNetworkConnection'
24+
SET_NETWORK_CONNECTION = 'setNetworkConnection'
25+
GET_AVAILABLE_IME_ENGINES = 'getAvailableIMEEngines'
26+
IS_IME_ACTIVE = 'isIMEActive'
27+
ACTIVATE_IME_ENGINE = 'activateIMEEngine'
28+
DEACTIVATE_IME_ENGINE = 'deactivateIMEEngine'
29+
GET_ACTIVE_IME_ENGINE = 'getActiveEngine'
2330

2431
# Appium Commands
2532
GET_APP_STRINGS = 'getAppStrings'
@@ -44,5 +51,3 @@ class MobileCommand(object):
4451
SHAKE = 'shake'
4552
RESET = 'reset'
4653
HIDE_KEYBOARD = 'hideKeyboard'
47-
GET_NETWORK_CONNECTION = 'getNetworkConnection'
48-
SET_NETWORK_CONNECTION = 'setNetworkConnection'

appium/webdriver/webdriver.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,50 @@ def set_network_connection(self, connectionType):
600600
}
601601
return self.execute(Command.SET_NETWORK_CONNECTION, data)['value']
602602

603+
@property
604+
def available_ime_engines(self):
605+
"""Get the available input methods for an Android device. Package and
606+
activity are returned (e.g., ['com.android.inputmethod.latin/.LatinIME'])
607+
Android only.
608+
"""
609+
return self.execute(Command.GET_AVAILABLE_IME_ENGINES, {})['value']
610+
611+
def is_ime_active(self):
612+
"""Checks whether the device has IME service active. Returns True/False.
613+
Android only.
614+
"""
615+
return self.execute(Command.IS_IME_ACTIVE, {})['value']
616+
617+
def activate_ime_engine(self, engine):
618+
"""Activates the given IME engine on the device.
619+
Android only.
620+
621+
:Args:
622+
- engine - the package and activity of the IME engine to activate (e.g.,
623+
'com.android.inputmethod.latin/.LatinIME')
624+
"""
625+
data = {
626+
'engine': engine
627+
}
628+
self.execute(Command.ACTIVATE_IME_ENGINE, data)
629+
return self
630+
631+
def deactivate_ime_engine(self):
632+
"""Deactivates the currently active IME engine on the device.
633+
Android only.
634+
"""
635+
self.execute(Command.DEACTIVATE_IME_ENGINE, {})
636+
return self
637+
638+
@property
639+
def active_ime_engine(self):
640+
"""Returns the activity and package of the currently active IME engine (e.g.,
641+
'com.android.inputmethod.latin/.LatinIME').
642+
Android only.
643+
"""
644+
return self.execute(Command.GET_ACTIVE_IME_ENGINE, {})['value']
645+
646+
603647
def _addCommands(self):
604648
self.command_executor._commands[Command.CONTEXTS] = \
605649
('GET', '/session/$sessionId/contexts')
@@ -660,6 +704,16 @@ def _addCommands(self):
660704
('GET', '/session/$sessionId/network_connection')
661705
self.command_executor._commands[Command.SET_NETWORK_CONNECTION] = \
662706
('POST', '/session/$sessionId/network_connection')
707+
self.command_executor._commands[Command.GET_AVAILABLE_IME_ENGINES] = \
708+
('GET', '/session/$sessionId/ime/available_engines')
709+
self.command_executor._commands[Command.IS_IME_ACTIVE] = \
710+
('GET', '/session/$sessionId/ime/activated')
711+
self.command_executor._commands[Command.ACTIVATE_IME_ENGINE] = \
712+
('POST', '/session/$sessionId/ime/activate')
713+
self.command_executor._commands[Command.DEACTIVATE_IME_ENGINE] = \
714+
('POST', '/session/$sessionId/ime/deactivate')
715+
self.command_executor._commands[Command.GET_ACTIVE_IME_ENGINE] = \
716+
('GET', '/session/$sessionId/ime/active_engine')
663717

664718

665719
# monkeypatched method for WebElement

test/functional/android/ime_tests.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import unittest
17+
from time import sleep
18+
19+
from selenium.common.exceptions import NoSuchElementException
20+
21+
from appium import webdriver
22+
import desired_capabilities
23+
24+
25+
# the emulator is sometimes slow and needs time to think
26+
SLEEPY_TIME = 1
27+
28+
LATIN_IME = u'com.android.inputmethod.latin/.LatinIME'
29+
30+
31+
class IMETests(unittest.TestCase):
32+
def setUp(self):
33+
desired_caps = desired_capabilities.get_desired_capabilities('ApiDemos-debug.apk')
34+
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
35+
36+
def tearDown(self):
37+
self.driver.quit()
38+
39+
40+
def test_available_ime_engines(self):
41+
engines = self.driver.available_ime_engines
42+
self.assertIsInstance(engines, list)
43+
self.assertTrue(LATIN_IME in engines)
44+
45+
def test_is_ime_active(self):
46+
self.assertTrue(self.driver.is_ime_active())
47+
48+
def test_active_ime_engine(self):
49+
self.assertIsInstance(self.driver.active_ime_engine, unicode)
50+
51+
def test_activate_ime_engine(self):
52+
engines = self.driver.available_ime_engines
53+
active_engine = self.driver.active_ime_engine
54+
55+
self.driver.activate_ime_engine(engines[-1])
56+
self.assertEqual(self.driver.active_ime_engine, engines[-1])
57+
58+
def test_deactivate_ime_engine(self):
59+
engines = self.driver.available_ime_engines
60+
self.driver.activate_ime_engine(engines[-1])
61+
62+
self.assertEqual(self.driver.active_ime_engine, engines[-1])
63+
64+
self.driver.deactivate_ime_engine()
65+
sleep(1)
66+
self.assertNotEqual(self.driver.active_ime_engine, engines[-1])
67+
68+
69+
if __name__ == "__main__":
70+
suite = unittest.TestLoader().loadTestsFromTestCase(IMETests)
71+
unittest.TextTestRunner(verbosity=2).run(suite)

0 commit comments

Comments
 (0)