问题:app没有搜索按钮,需要点击键盘上的搜索按钮来实现搜索功能。
查到的几种解决办法,试用结果
- 执行driver.press_keycode(66),使用回车键的keycode来替代搜索按钮
缺点:兼容性差,有些手机不管用
- 手机上安装指定输入法,如搜狗输入法,再执行driver.press_keycode(66)
缺点:多设备并行时,每个手机都要安装输入法,每个手机安装校验都不一样
如何模拟IME动作生成
翻阅官方文档,发现可以模拟软键盘动作
Android开发人员经常使用带有actionId
参数的[onEditorAction]https://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html#onEditorAction(android.widget.TextView, int, android.view.KeyEvent)回调来实现动作处理,例如,在屏幕键盘上按下Search
或Done
按钮的时候。
从1.9.2以后版本的Appium允许通过提供特殊mobile:
命令来自动生成此类动作。
mobile:performEditorAction
对当前关注的元素执行给定的编辑器动作。
action:待执行的编辑器动作的名称或整数代码。支持以下操作名称:normal, unspecified, none, go, search, send, next, done, previous
。
方法说明
参考:https://blog.csdn.net/u010041075/article/details/65445043
说明:需要注意的是 setOnEditorActionListener这个方法,并不是在我们点击EditText的时候触发,也不是在我们对EditText进行编辑时触发,而是在我们编辑完之后点击软键盘上的各种键才会触发。
因为通过布局文件中的imeOptions可以控制软件盘右下角的按钮显示为不同按钮。所以和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
使用实例
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')