Appium Appium 中 sendKeys 二次输入的号码不一致

ivy520 · 2016年07月12日 · 最后由 折鹤 回复于 2016年08月30日 · 2136 次阅读

1、代码如下:

operateBase.sendKeys(elements_my.bankNo,"输入绑卡卡号","6217900800001197084");
TimeUnit.SECONDS.sleep(3);
operateBase.sendKeys(elements_my.bankNoConfirm,"再次输入绑卡卡号","6217900800001197084"); 

封装的sendKeys方法如下:
  public void sendKeys(final WebElement element, String LogText, CharSequence... charSequences) {
        Log.logStep("[" + LogText + "  " + char2String(charSequences) + "]");
        Log.logInfo("[输入字符 " + element.toString().substring(element.toString().indexOf("->")));
        element.sendKeys(charSequences);
    }

其实就是使用的 Appium 中的 sendKeys(charSequences) 方法, 但是实际在 APP 中 2 次输入的卡号不一致
日志如下:

手机页面如下图

本人已经百度,群中问过了,没有找到相关答案,望各位高人指点。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 20 条回复 时间 点赞

你用的是啥输入法?不同输入法对 keycode 映射有点区别,容易引起这类问题。建议最好用 appium 给的或者 google 拼音输入法。

如果后台实现用的是 adb shell input text 倒是有可能乱序

@chenhengjie123 我用的是 Appium 给的输入法

@among29 谢谢。 看了回帖,有了思路。 我试验了切换键盘,发现切换键盘后其他地方的输入出现了混乱。然后我用了另外一个方法解决了 。思路是: 使用 do {} while() 循环, 如果发现不一致,就重新输入,直到一致就结束循环。

#5 楼 @ivy520 最好多种方式结合,避免进入死循环。
可以试验几次,不行,就换方式。再不行,再换。还是不行,就只有抛出异常,报错了。。。

类似这种我之前用 adb 输入解决了

对于字符串输入,可以先转化为 keycode,再进行输入

—— 来自 TesterHome 官方 安卓客户端

大家在 appium 中,使用 sendKeys 输入时候,是不是很慢,有时候就执行个输入就要 10s 时间,很郁闷

这是 appium 之前的 bug,连续两次 sendkeys 操作就会出现输入的内容不是自己想要的情况
所以可以先 sendkeys , click, sendkeys 这样的操作应该就可以避免

在输入前把 appium 提供的输入法禁掉,然后再输入就可以了,这个估计是 appium 输入法的 bug

@kuroky 请问如何把 appium 提供的输入法禁掉呢?禁掉之后又怎么再开启呢?

#12 楼 @kaige201314

/**
     * 禁用appium输入法
     */
    public void disableUnicodeIME() {
        //
        log.info("disable io.appium.android.ime/.UnicodeIME");
        ExecuteAdbCmd("shell", "ime", "disable",
                "io.appium.android.ime/.UnicodeIME");
        // ExecuteAdbCmd("shell","ime","set","com.android.inputmethod.latin/.LatinIME");

    }

/**
     * 启用appium输入法,并设置为当前输入法
     */
    public void enableUnicodeIME() {
        log.info("enable io.appium.android.ime/.UnicodeIME");
        ExecuteAdbCmd("shell", "ime", "enable",
                "io.appium.android.ime/.UnicodeIME");
        log.info("set io.appium.android.ime/.UnicodeIME");
        ExecuteAdbCmd("shell", "ime", "set",
                "io.appium.android.ime/.UnicodeIME");
    }

@kuroky 非常感谢,但是我用的语言是 python,不知道执行 adb 命令,可以使用 ExecuteAdbCmd("shell", "ime", "enable",
"io.appium.android.ime/.UnicodeIME") 这种方式吗?

#14 楼 @kaige201314 可以用 python 执行 adb 命令 如 adb shell ime enable io.appium.android.ime/.UnicodeIME
这个和语言无关

@kuroky 我自己写了一个 python 的切换输入法的方法类,直接调用就可以了,现在分享出来:

文件名:inputMethod.py

#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

import os

command0 ='adb shell ime list -s'
command1 ='adb shell settings get secure default_input_method'
command2 ='adb shell ime set com.android.inputmethod.latin/.LatinIME'
command3 ='adb shell ime set io.appium.android.ime/.UnicodeIME'

#列出系统现在所安装的所有输入法
#os.system(command0)

#打印系统当前默认的输入法
#os.system(command1)

#切换latin输入法为当前输入法
#os.system(command2)

#切换appium输入法为当前输入法
#os.system(command3)

class InputMethod:

  #切换latin输入法为当前输入法
  def enableLatinIME(self):
    os.system(command2)        

  #切换appium输入法为当前输入法
  def enableAppiumUnicodeIME(self):
    os.system(command3)

需要调用时,方法如下:(注意要先引包)

#切换latin输入法为当前输入法
inputMethod.InputMethod().enableLatinIME()
#切换appium输入法为当前输入法
inputMethod.InputMethod().enableAppiumUnicodeIME()

kaige201314 [该话题已被删除] 中提及了此贴 07月20日 19:11
kaige201314 [该话题已被删除] 中提及了此贴 07月20日 20:08

#16 楼 @kaige201314

首先感谢分享,但是用 adb 命令的方式来切换不是一个好的解决方法。
一是多设备时,命令失效。
二是 remote dirver 时不适用。

appium 有自带的输入法管理的 api。

ivy520 #21 · 2016年08月02日 Author

@among29 你好,appium 有自带的输入法管理的 api, 有相关 API 文档吗?我没找到。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册