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

来源:互联网 发布:淘宝网点照明设计软件 编辑:程序博客网 时间:2024/06/08 11:19

原地址:http://blog.csdn.net/wsbl52006/article/details/51995338

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


方法一:

[java] view plain copy
  1. public static void inputComsumeInfo(WebElement element, String cardnum) throws Exception  
  2.     {  
  3.         String actual = "";  
  4.         int i = 0;  
  5.         do  
  6.         {  
  7.             element.click();  
  8.             String[] str = cardnum.split("");  
  9.             for (int j = 0; j < str.length; j++)  
  10.             {  
  11.                 pressKeyCode(Integer.parseInt(str[j]) + 7);  
  12.                 // 如果当前长度超过预期的值说明多输入了一个数字,需要回删一个字符  
  13.                 if (element.getText().replace(" """).length() > (j + 1))  
  14.                 {  
  15.                     logger.info("长度输入超过预期,需要回删一个字符");  
  16.                     pressKeyCode(67);  
  17.                 }  
  18.   
  19.                 if (j == str.length / 2)  
  20.                 {  
  21.                     element.click(); // 点击元素,确保与Appium有交互,否则会超时自动关闭session  
  22.                     pressKeyCode(123); // 将光标移动到末尾  
  23.                 }  
  24.             }  
  25.             actual = element.getText().replace(" """);  
  26.             logger.info("第 " + (i + 1) + " 次输入,期望值为: " + cardnum + " 实际值为:" + actual + " " + cardnum.equals(actual));  
  27.             i++;  
  28.             if (!actual.equals(cardnum))  
  29.             {  
  30.                 element.clear();  
  31.             }  
  32.         } while (!actual.equals(cardnum) && i < CommonSetting.RETRY_TIMES);  
  33.         assertEquals(actual, cardnum);  
  34.     }  
  35.   
  36.     /** 
  37.      * 模拟键盘输入 
  38.      *  
  39.      * @param keyCode 
  40.      */  
  41.     private static void pressKeyCode(int keyCode)  
  42.     {  
  43.         String cmdStr = "adb shell input keyevent " + keyCode + "";  
  44.         try  
  45.         {  
  46.             Runtime.getRuntime().exec(cmdStr);  
  47.             sleep(800);  
  48.         } catch (IOException e)  
  49.         {  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  
  53.   
  54.     private static void sleep(long time)  
  55.     {  
  56.         try  
  57.         {  
  58.             Thread.sleep(time);  
  59.         } catch (InterruptedException e)  
  60.         {  
  61.             e.printStackTrace();  
  62.         }  
  63.     }  


方法二:

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

[java] view plain copy
  1. public class SwitchTypewriting  
  2. {  
  3.     // 列出系统所有输入法  
  4.     private final static String LIST_ALL = "adb shell ime list -s";  
  5.   
  6.     // 系统当前使用的输入法  
  7.     private final static String LIST_NOW = "adb shell settings get secure default_input_method";  
  8.   
  9.     // Latin输入法  
  10.     private final static String LIST_LATIN = "adb shell ime set com.android.inputmethod.latin/.LatinIME";  
  11.   
  12.     // Appium输入法  
  13.     private final static String LIST_APPIUM = "adb shell ime set io.appium.android.ime/.UnicodeIME";  
  14.   
  15.     public static void switch2Latin()  
  16.     {  
  17.         execCmd(LIST_LATIN);  
  18.     }  
  19.   
  20.     public static void switch2Appium()  
  21.     {  
  22.         execCmd(LIST_APPIUM);  
  23.     }  
  24.   
  25.     /** 
  26.      * 模拟键盘输入 
  27.      *  
  28.      * @param cmdStr 
  29.      */  
  30.     private static void execCmd(String cmdStr)  
  31.     {  
  32.         try  
  33.         {  
  34.             Runtime.getRuntime().exec(cmdStr);  
  35.             sleep(800);  
  36.         } catch (IOException e)  
  37.         {  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41.   
  42.     private static void sleep(long time)  
  43.     {  
  44.         try  
  45.         {  
  46.             Thread.sleep(time);  
  47.         } catch (InterruptedException e)  
  48.         {  
  49.             e.printStackTrace();  
  50.         }  
  51.     }  
  52. }  
0 0
原创粉丝点击