增加执行安卓adb命令方法

修复公共参数中,KEY值前N个字符一样,导致替换值不对的BUG
This commit is contained in:
seagull 2018-09-14 10:53:29 +08:00
parent bcea1f67ef
commit dc27b500ad
2 changed files with 177 additions and 141 deletions

View File

@ -196,8 +196,13 @@ public class AndroidEncapsulateOperation {
result = "执行JS...【" + operationValue + "";
luckyclient.publicclass.LogUtil.APP.info("执行JS...【" + operationValue + "");
break;
// 模拟手机键盘
case "exAdbShell":
Runtime.getRuntime().exec(operationValue);
result = "执行安卓adb命令...【" + operationValue + "";
luckyclient.publicclass.LogUtil.APP.info("执行安卓adb命令...【" + operationValue + "");
break;
case "keycode":
// 模拟手机键盘
if (ChangString.isNumeric(operationValue)) {
appium.pressKeyCode(Integer.valueOf(operationValue));
result = "模拟手机键盘发送指令...keycode【" + operationValue + "";

View File

@ -1,8 +1,14 @@
package luckyclient.publicclass;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import com.alibaba.fastjson.JSONArray;
@ -11,10 +17,8 @@ import com.alibaba.fastjson.JSONObject;
/**
* =================================================================
* 这是一个受限制的自由软件您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途也不允许对程序代码修改后以任何形式任何目的的再发布
* 为了尊重作者的劳动成果LuckyFrame关键版权信息严禁篡改
* 有任何疑问欢迎联系作者讨论 QQ:1573584944 seagull1985
* 为了尊重作者的劳动成果LuckyFrame关键版权信息严禁篡改 有任何疑问欢迎联系作者讨论 QQ:1573584944 seagull1985
* =================================================================
*
* @author seagull
* @date 2017年12月1日 上午9:29:40
*/
@ -22,180 +26,207 @@ public class ChangString {
/**
* 替换变量中的字符
*
* @param str
* @param variable
* @param changname
* @return
*/
public static String changparams(String str, Map<String, String> variable, String changname) {
try {
if (null == str) {
return null;
}
str = str.replace("&quot;", "\"");
str = str.replace("&#39;", "\'");
//@@用来注释@的引用作用
int varcount = counter(str, "@") - counter(str, "@@") * 2;
public static String changparams(String str, Map<String, String> variable, String changname) {
try {
if (null == str) {
return null;
}
str = str.replace("&quot;", "\"");
str = str.replace("&#39;", "\'");
// @@用来注释@的引用作用
int varcount = counter(str, "@") - counter(str, "@@") * 2;
//如果存在传参进行处理
if (varcount > 0) {
luckyclient.publicclass.LogUtil.APP.info("" + changname + "" + str + "】中找到" + varcount + "个可替换参数");
int changcount = 0;
//从参数列表中查找匹配变量
for (Map.Entry<String, String> entry : variable.entrySet()) {
if (str.contains("@" + entry.getKey())) {
if (str.contains("@@" + entry.getKey())) {
str = str.replace("@@" + entry.getKey(), "////CHANG////");
}
//用来替换字符串中带了\"或是\'会导致\消失的问题
//entry.setValue(entry.getValue().replaceAll("\\\\\"", "\\&quot;"));
//entry.setValue(entry.getValue().replaceAll("\\\\\'", "\\\\&#39;"));
int viewcount = counter(str, "@" + entry.getKey());
str = str.replace("@" + entry.getKey(), entry.getValue());
luckyclient.publicclass.LogUtil.APP.info("" + changname + "引用变量【@" + entry.getKey() + "】替换成值【" + entry.getValue() + "");
str = str.replace("////CHANG////", "@@" + entry.getKey());
changcount = changcount + viewcount;
}
}
// 如果存在传参进行处理
if (varcount > 0) {
luckyclient.publicclass.LogUtil.APP.info("" + changname + "" + str + "】中找到" + varcount + "个可替换参数");
int changcount = 0;
if (varcount != changcount) {
luckyclient.publicclass.LogUtil.APP.error(changname + "有引用变量未在参数列中找到,请检查!处理结果【" + str + "");
}
}
str = str.replace("@@", "@");
//用来恢复字符串中带了\"或是\'会导致\消失的问题
//str = str.replaceAll("\\&quot;", "\\\\\"");
//str = str.replaceAll("\\&#39;", "\\\\\'");
return str;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
// 准备将HASHMAP换成LINKMAP对KEY进行排序解决要先替换最长KEY的问题
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(variable.entrySet());
// 然后通过比较器来实现排序
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
// 按KEY长度降序排序
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
return o2.getKey().length() - o1.getKey().length();
}
});
/**
* 统计字符
* @param str1
* @param str2
* @return
*/
public static int counter(String str1, String str2) {
int total = 0;
for (String tmp = str1; tmp != null && tmp.length() >= str2.length(); ) {
if (tmp.indexOf(str2) == 0) {
total++;
tmp = tmp.substring(str2.length());
} else {
tmp = tmp.substring(1);
}
}
return total;
}
Map<String, String> aMap2 = new LinkedHashMap<String, String>();
for (Map.Entry<String, String> mapping : list) {
aMap2.put(mapping.getKey(), mapping.getValue());
}
/**
* 判断是否是数字
* @param str
* @return
*/
public static boolean isNumeric(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
// 从参数列表中查找匹配变量
for (Map.Entry<String, String> entry : aMap2.entrySet()) {
if (str.contains("@" + entry.getKey())) {
if (str.contains("@@" + entry.getKey())) {
str = str.replace("@@" + entry.getKey(), "////CHANG////");
}
// 用来替换字符串中带了\"或是\'会导致\消失的问题
// entry.setValue(entry.getValue().replaceAll("\\\\\"",
// "\\&quot;"));
// entry.setValue(entry.getValue().replaceAll("\\\\\'",
// "\\\\&#39;"));
int viewcount = counter(str, "@" + entry.getKey());
str = str.replace("@" + entry.getKey(), entry.getValue());
luckyclient.publicclass.LogUtil.APP
.info("" + changname + "引用变量【@" + entry.getKey() + "】替换成值【" + entry.getValue() + "");
str = str.replace("////CHANG////", "@@" + entry.getKey());
changcount = changcount + viewcount;
}
}
/**
* 判断是否是整数
* @param str
* @return
*/
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
if (varcount != changcount) {
luckyclient.publicclass.LogUtil.APP.error(changname + "有引用变量未在参数列中找到,请检查!处理结果【" + str + "");
}
}
str = str.replace("@@", "@");
// 用来恢复字符串中带了\"或是\'会导致\消失的问题
// str = str.replaceAll("\\&quot;", "\\\\\"");
// str = str.replaceAll("\\&#39;", "\\\\\'");
return str;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 替换变量类型
* @param object
* @param str
* @return
*/
public static Object settype(Object object,String str){
if(object instanceof Integer){
return Integer.valueOf(str);
}else if(object instanceof Boolean){
return Boolean.valueOf(str);
}else if(object instanceof Long){
return Long.valueOf(str);
}else if(object instanceof Timestamp){
return Timestamp.valueOf(str);
}else if(object instanceof JSONObject){
return JSONObject.parseObject(str);
}else if(object instanceof JSONArray){
return JSONArray.parseArray(str);
}else{
return str;
}
}
/**
* 替换json中的变量
* @param json
* @param key
* @param value
* @return
*/
public static Map<String,String> changjson(String json, String key, String value) {
Map<String,String> map=new HashMap<String,String>(0);
/**
* 统计字符
*
* @param str1
* @param str2
* @return
*/
public static int counter(String str1, String str2) {
int total = 0;
for (String tmp = str1; tmp != null && tmp.length() >= str2.length();) {
if (tmp.indexOf(str2) == 0) {
total++;
tmp = tmp.substring(str2.length());
} else {
tmp = tmp.substring(1);
}
}
return total;
}
/**
* 判断是否是数字
*
* @param str
* @return
*/
public static boolean isNumeric(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* 判断是否是整数
*
* @param str
* @return
*/
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
/**
* 替换变量类型
*
* @param object
* @param str
* @return
*/
public static Object settype(Object object, String str) {
if (object instanceof Integer) {
return Integer.valueOf(str);
} else if (object instanceof Boolean) {
return Boolean.valueOf(str);
} else if (object instanceof Long) {
return Long.valueOf(str);
} else if (object instanceof Timestamp) {
return Timestamp.valueOf(str);
} else if (object instanceof JSONObject) {
return JSONObject.parseObject(str);
} else if (object instanceof JSONArray) {
return JSONArray.parseArray(str);
} else {
return str;
}
}
/**
* 替换json中的变量
*
* @param json
* @param key
* @param value
* @return
*/
public static Map<String, String> changjson(String json, String key, String value) {
Map<String, String> map = new HashMap<String, String>(0);
map.put("boolean", "false");
map.put("json", json);
if (json.startsWith("{") && json.endsWith("}")) {
try {
JSONObject jsonStr = JSONObject.parseObject(json);
if(jsonStr.containsKey(key)){
jsonStr.put(key, settype(jsonStr.get(key),value));
if (jsonStr.containsKey(key)) {
jsonStr.put(key, settype(jsonStr.get(key), value));
map.put("boolean", "true");
map.put("json", jsonStr.toJSONString());
luckyclient.publicclass.LogUtil.APP.info("JSON字符串替换成功原始JSON:【"+json+"】 新JSON:【"+jsonStr.toJSONString()+"");
luckyclient.publicclass.LogUtil.APP
.info("JSON字符串替换成功原始JSON:【" + json + "】 新JSON:【" + jsonStr.toJSONString() + "");
}
} catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error("格式化成JSON异常请检查参数"+json,e);
luckyclient.publicclass.LogUtil.APP.error("格式化成JSON异常请检查参数" + json, e);
return map;
}
} else if (json.startsWith("[") && json.endsWith("]")) {
try {
JSONArray jsonarr = JSONArray.parseArray(json);
JSONObject jsonStr=new JSONObject();
int index=0;
if(key.indexOf("[")>=0 && key.endsWith("]")){
index=Integer.valueOf(key.substring(key.lastIndexOf("[")+1,key.lastIndexOf("]")));
key=key.substring(0, key.lastIndexOf("["));
jsonStr = jsonarr.getJSONObject(index);
luckyclient.publicclass.LogUtil.APP.info("准备替换JSONArray中的参数值未检测到指定参数名序号默认替换第1个参数...");
}else{
JSONObject jsonStr = new JSONObject();
int index = 0;
if (key.indexOf("[") >= 0 && key.endsWith("]")) {
index = Integer.valueOf(key.substring(key.lastIndexOf("[") + 1, key.lastIndexOf("]")));
key = key.substring(0, key.lastIndexOf("["));
jsonStr = jsonarr.getJSONObject(index);
luckyclient.publicclass.LogUtil.APP.info("准备替换JSONArray中的参数值替换指定第"+index+"个参数...");
luckyclient.publicclass.LogUtil.APP.info("准备替换JSONArray中的参数值未检测到指定参数名序号默认替换第1个参数...");
} else {
jsonStr = jsonarr.getJSONObject(index);
luckyclient.publicclass.LogUtil.APP.info("准备替换JSONArray中的参数值替换指定第" + index + "个参数...");
}
if(jsonStr.containsKey(key)){
jsonStr.put(key, settype(jsonStr.get(key),value));
if (jsonStr.containsKey(key)) {
jsonStr.put(key, settype(jsonStr.get(key), value));
jsonarr.set(index, jsonStr);
map.put("boolean", "true");
map.put("json", jsonarr.toJSONString());
}
luckyclient.publicclass.LogUtil.APP.info("JSONARRAY字符串替换成功原始JSONARRAY:【"+json+"】 新JSONARRAY:【"+jsonarr.toJSONString()+"");
luckyclient.publicclass.LogUtil.APP.info(
"JSONARRAY字符串替换成功原始JSONARRAY:【" + json + "】 新JSONARRAY:【" + jsonarr.toJSONString() + "");
} catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error("格式化成JSONArray异常请检查参数"+json,e);
luckyclient.publicclass.LogUtil.APP.error("格式化成JSONArray异常请检查参数" + json, e);
return map;
}
}
return map;
}
public static void main(String[] args) {
}
public static void main(String[] args) {
}
}