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

相关推荐
执风挽^8 分钟前
Python基础编程题2
开发语言·python·算法·visual studio code
纤纡.23 分钟前
PyTorch 入门精讲:从框架选择到 MNIST 手写数字识别实战
人工智能·pytorch·python
kjkdd35 分钟前
6.1 核心组件(Agent)
python·ai·语言模型·langchain·ai编程
小镇敲码人41 分钟前
剖析CANN框架中Samples仓库:从示例到实战的AI开发指南
c++·人工智能·python·华为·acl·cann
萧鼎43 分钟前
Python 包管理的“超音速”革命:全面上手 uv 工具链
开发语言·python·uv
alvin_20051 小时前
python之OpenGL应用(二)Hello Triangle
python·opengl
铁蛋AI编程实战1 小时前
通义千问 3.5 Turbo GGUF 量化版本地部署教程:4G 显存即可运行,数据永不泄露
java·人工智能·python
jiang_changsheng2 小时前
RTX 2080 Ti魔改22GB显卡的最优解ComfyUI教程
python·comfyui
0思必得02 小时前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
沈浩(种子思维作者)3 小时前
系统要活起来就必须开放包容去中心化
人工智能·python·flask·量子计算