appium使用sendkeys输入银行卡卡号(每4个数字自动空一格)总是输入不正确的解决办法

appium使用sendkeys输入银行卡卡号(每4个数字自动空一格)总是输入不正确的解决办法

方法一:


public static void inputComsumeInfo(WebElement element, String cardnum) throws Exception
    {
        String actual = "";
        int i = 0;
        do
        {
            element.click();
            String[] str = cardnum.split("");
            for (int j = 0; j < str.length; j++)
            {
                pressKeyCode(Integer.parseInt(str[j]) + 7);
                // 如果当前长度超过预期的值说明多输入了一个数字,需要回删一个字符
                if (element.getText().replace(" ", "").length() > (j + 1))
                {
                    logger.info("长度输入超过预期,需要回删一个字符");
                    pressKeyCode(67);
                }
 
                if (j == str.length / 2)
                {
                    element.click(); // 点击元素,确保与Appium有交互,否则会超时自动关闭session
                    pressKeyCode(123); // 将光标移动到末尾
                }
            }
            actual = element.getText().replace(" ", "");
            logger.info("第 " + (i + 1) + " 次输入,期望值为: " + cardnum + " 实际值为:" + actual + " " + cardnum.equals(actual));
            i++;
            if (!actual.equals(cardnum))
            {
                element.clear();
            }
        } while (!actual.equals(cardnum) && i < CommonSetting.RETRY_TIMES);
        assertEquals(actual, cardnum);
    }
 
    /**
     * 模拟键盘输入
     * 
     * @param keyCode
     */
    private static void pressKeyCode(int keyCode)
    {
        String cmdStr = "adb shell input keyevent " + keyCode + "";
        try
        {
            Runtime.getRuntime().exec(cmdStr);
            sleep(800);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
 
    private static void sleep(long time)
    {
        try
        {
            Thread.sleep(time);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

方法二:

输入之前切换到latin输入法,输入完成之后再切换会appium输入法

public class SwitchTypewriting
{
    // 列出系统所有输入法
    private final static String LIST_ALL = "adb shell ime list -s";
 
    // 系统当前使用的输入法
    private final static String LIST_NOW = "adb shell settings get secure default_input_method";
 
    // Latin输入法
    private final static String LIST_LATIN = "adb shell ime set com.android.inputmethod.latin/.LatinIME";
 
    // Appium输入法
    private final static String LIST_APPIUM = "adb shell ime set io.appium.android.ime/.UnicodeIME";
 
    public static void switch2Latin()
    {
        execCmd(LIST_LATIN);
    }
 
    public static void switch2Appium()
    {
        execCmd(LIST_APPIUM);
    }
 
    /**
     * 模拟键盘输入
     * 
     * @param cmdStr
     */
    private static void execCmd(String cmdStr)
    {
        try
        {
            Runtime.getRuntime().exec(cmdStr);
            sleep(800);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
 
    private static void sleep(long time)
    {
        try
        {
            Thread.sleep(time);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值