Airtest遇到模拟器无法输入中文的情况该如何处理?

此文章来源于项目官方公众号:"AirtestProject"

版权声明:允许转载,但转载必须保留原链接;请勿用作商业或者非法用途

1. 前言

最近有收到同学们的一些提问,使用Airtest的 text 接口,发现在部分模拟器上, text 无法输入中文,不知道该怎么处理。

今天我们就输入这个小问题,来详细聊一下。

2. Airtest的输入法简介

对于Android设备来说,Airtest的输入功能,由 Yosemite.apk 提供。有了这个apk,就可以支持我们在Android设备上输入中文,而不是局限于只能输入英文字母的 adb input text

所以在调用Airtest的 text 接口之前,请务必在Android设备上安装好 Yosemite.apk

3.在部分特殊设备上无法输入中文

也有一些情况,即使我们安装好了 Yosemite.apk ,使用 text 接口进行输入时,却只能输入英文,没法输入中文,我们以mumu12模拟器为例:

在给mumu12输入中文的时候,出现了1个报错:

swift 复制代码
airtest.core.error.AdbShellError: stdout[b''] stderr[b"\r\nException
occurred while executing 'text':\r\njava.lang.NullPointerException:
Attempt to get length of null array\r\n\tatcom.android.server.input.InputShellCommand.sendText(InputShellCommand.java:284)\r\n\tat 
com.android.server.input.InputShellCommand.runText(InputShellCommand.java:256)\r\n\tat
com.android.server.input.InputShellCommand.onCommand(InputShellCommand.java:197)\r\n\tat com.android.modules.utils.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:97)\r\n\tat
android.os.ShellCommand.exec(ShellCommand.java:38)\r\n\tat com.android.server.input.InputManagerService.onShellCommand(InputManagerService.java:3591)\r\n\tat
android.os.Binder.shellCommand(Binder.java:950)\r\n\tat
android.os.Binder.onTransact(Binder.java:834)\r\n\tat
android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:1167)\r\n\tat android.os.Binder.execTransactInternal(Binder.java:1184)\r\n\tat android.os.Binder.execTransact(Binder.java:1143)\r\n"]

这是因为模拟器没有真正把 Yosemite 输入法调起来导致的,我们可以到 Yosemite.apk 里面,找到输入法,启用 Yosemite 输入法,之后关闭掉其余的输入法:

之后再通过下述脚本的方式,给设备输入中文文本:

python 复制代码
# -*- encoding=utf8 -*-
__author__ = "AirtestProject"
​
from airtest.core.api import *
auto_setup(__file__)
​
shell('am broadcast -a INPUT_TEXT --es text "大话西游"')

4. 小结

今天主要讲述了,在部分特殊Android设备里,遇到 text 输入接口无法完成的一些输入事情的时候,我们还可以通过怎样的方式来实现我们的输入效果,本质还是调用了 Yosemite.apk 里面的功能。

如果还有其他关于输入感兴趣的问题,欢迎给我们私信/留言。


AirtestIDE下载airtest.netease.com/
Airtest 教程官网airtest.doc.io.netease.com/
搭建企业私有云服务airlab.163.com/b2b

官方答疑 Q 群:117973773

相关推荐
山烛7 分钟前
KNN 算法中的各种距离:从原理到应用
人工智能·python·算法·机器学习·knn·k近邻算法·距离公式
guozhetao20 分钟前
【ST表、倍增】P7167 [eJOI 2020] Fountain (Day1)
java·c++·python·算法·leetcode·深度优先·图论
墨染点香26 分钟前
第七章 Pytorch构建模型详解【构建CIFAR10模型结构】
人工智能·pytorch·python
阿什么名字不会重复呢1 小时前
在线工具+网页平台来学习和操作Python与Excel相关技能
python·数据分析
Vertira1 小时前
python 阿里云 安装 dashscope的简介、安装
开发语言·python
gc_22992 小时前
学习Python中Selenium模块的基本用法(1:简介)
python·selenium
先做个垃圾出来………3 小时前
2116. 判断一个括号字符串是否有效
python
兮℡檬,3 小时前
房价预测|Pytorch
人工智能·pytorch·python
im_AMBER7 小时前
学习日志19 python
python·学习
mortimer10 小时前
安装NVIDIA Parakeet时,我遇到的两个Pip“小插曲”
python·github