Problem: The app does not have a search button, and you need to click the search button on the keyboard to realize the search function.
Several solutions found, trial results
- Execute driver.press_keycode(66), use the keycode of the Enter key instead of the search button
Disadvantages: poor compatibility, some mobile phones do not work
- Install the specified input method on the mobile phone, such as Sogou input method, and then execute driver.press_keycode(66)
Disadvantages: When multiple devices are in parallel, each mobile phone needs to install the input method, and the installation verification of each mobile phone is different
How to simulate IME action generation
Looking through the official documents, I found that the soft keyboard action can be simulated
Android developers often use actionId
[onEditorAction] with parameters https://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html#onEditorAction (android.widget.TextView, int, android.view.KeyEvent) Callback to implement action handling, for example, when the Search
or button is pressed on the on-screen keyboard .Done
Versions of Appium from 1.9.2 onwards allow mobile:
the automatic generation of such actions by providing special commands.
mobile:performEditorAction
Executes the given editor action on the currently focused element.
action : The name or integer code of the editor action to be performed. The following action names are supported: normal, unspecified, none, go, search, send, next, done, previous
.
Method Description
Reference: https://blog.csdn.net/u010041075/article/details/65445043
Explanation: It should be noted that the setOnEditorActionListener method is not triggered when we click EditText, nor is it triggered when we edit EditText, but is triggered when we click various keys on the soft keyboard after editing.
Because the buttons in the lower right corner of the software disk can be controlled to display as different buttons through the imeOptions in the layout file. Therefore, it can realize various soft keyboard functions with EditorInfo.
各种属性对应:
imeOptions=”actionUnspecified” –> EditorInfo.IME_ACTION_UNSPECIFIED
imeOptions=”actionNone” –> EditorInfo.IME_ACTION_NONE
imeOptions=”actionGo” –> EditorInfo.IME_ACTION_GO
imeOptions=”actionSearch” –> EditorInfo.IME_ACTION_SEARCH
imeOptions=”actionSend” –> EditorInfo.IME_ACTION_SEND
imeOptions=”actionNext” –> EditorInfo.IME_ACTION_NEXT
imeOptions=”actionDone” –> EditorInfo.IME_ACTION_DONE
use case
def ime_actions_by_att(self, att):
"""
软键盘操作,重写Enter键完成搜索、完成等操作
normal, unspecified, none, go, search, send, next, done, previous
search 搜索;send 发送
"""
self.driver.execute_script('mobile: performEditorAction', {'action': att})
return self
# 点击搜索
self.driver.find_element(*example_home_page.tvSearchContent).click()
# 搜索
self.driver.find_element(*example_home_page.etSearch).send_keys('26')
example_action.ime_actions_by_att('search')