Skip to content

Commit

Permalink
Merge pull request #450 from YeungHoiChiu/coordinate-globalParams-Sup…
Browse files Browse the repository at this point in the history
…port

feat: 自动化步骤中坐标相关操作的控件元素也可以使用参数化&安卓获取控件属性时可直接获取中心坐标值
  • Loading branch information
ZhouYixun authored Jun 7, 2024
2 parents 9e6b024 + 6791676 commit 342e80c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ public void getTextAndAssert(HandleContext handleContext, String des, String sel
}

public void longPressPoint(HandleContext handleContext, String des, String xy, int time) {
xy = TextHandler.replaceTrans(xy, globalParams);
double x = Double.parseDouble(xy.substring(0, xy.indexOf(",")));
double y = Double.parseDouble(xy.substring(xy.indexOf(",") + 1));
int[] point = computedPoint(x, y);
Expand Down Expand Up @@ -702,6 +703,7 @@ public void keyCode(HandleContext handleContext, int key) {
}

public void tap(HandleContext handleContext, String des, String xy) {
xy = TextHandler.replaceTrans(xy, globalParams);
double x = Double.parseDouble(xy.substring(0, xy.indexOf(",")));
double y = Double.parseDouble(xy.substring(xy.indexOf(",") + 1));
int[] point = computedPoint(x, y);
Expand All @@ -715,6 +717,9 @@ public void tap(HandleContext handleContext, String des, String xy) {
}

public void swipePoint(HandleContext handleContext, String des1, String xy1, String des2, String xy2) {
// 让坐标系也支持变量替换
xy1 = TextHandler.replaceTrans(xy1, globalParams);
xy2 = TextHandler.replaceTrans(xy2, globalParams);
double x1 = Double.parseDouble(xy1.substring(0, xy1.indexOf(",")));
double y1 = Double.parseDouble(xy1.substring(xy1.indexOf(",") + 1));
int[] point1 = computedPoint(x1, y1);
Expand Down Expand Up @@ -998,8 +1003,14 @@ public void getActivity(HandleContext handleContext, String expect) {
public void getElementAttr(HandleContext handleContext, String des, String selector, String pathValue, String attr, String expect) {
handleContext.setStepDes("验证控件 " + des + " 属性");
handleContext.setDetail("属性:" + attr + ",期望值:" + expect);
String attrValue;
try {
String attrValue = findEle(selector, pathValue).getAttribute(attr);
if (attr.equals("centerCoordinate")) {
String bounds = findEle(selector, pathValue).getAttribute("bounds"); // [x1,y1][x2,y2]
attrValue = getCenterCoordinate(bounds);
} else {
attrValue = findEle(selector, pathValue).getAttribute(attr);
}
log.sendStepLog(StepType.INFO, "", attr + " 属性获取结果: " + attrValue);
try {
assertEquals(attrValue, expect);
Expand All @@ -1015,15 +1026,29 @@ public void obtainElementAttr(HandleContext handleContext, String des, String se
String attr, String variable) {
handleContext.setStepDes("获取控件 " + des + " 属性到变量");
handleContext.setDetail("目标属性:" + attr + ",目标变量:" + variable);
String attrValue;
try {
String attrValue = findEle(selector, pathValue).getAttribute(attr);
// 自定义一个获取控件中心坐标的逻辑,方便通过控件获取一个坐标去做滑动、拖拽等操作
if (attr.equals("centerCoordinate")) {
String bounds = findEle(selector, pathValue).getAttribute("bounds"); // [x1,y1][x2,y2]
attrValue = getCenterCoordinate(bounds);
} else {
attrValue = findEle(selector, pathValue).getAttribute(attr);
}
log.sendStepLog(StepType.INFO, "", attr + " 属性获取结果: " + attrValue);
globalParams.put(variable, attrValue);
} catch (Exception e) {
handleContext.setE(e);
}
}

private String getCenterCoordinate(String bounds) {
String[] parts = bounds.split("]\\[");
String[] xy = parts[0].substring(1).split(",");
String[] xy2 = parts[1].substring(0, parts[1].length() - 1).split(",");
return (Integer.parseInt(xy2[0]) + Integer.parseInt(xy[0])) / 2 + "," + (Integer.parseInt(xy2[1]) + Integer.parseInt(xy[1])) / 2;
}

public void logElementAttr(HandleContext handleContext, String des, String selector, String pathValue, String attr) {
handleContext.setStepDes("日志输出控件 " + des + " 属性");
handleContext.setDetail("目标属性:" + attr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ public void getTextAndAssert(HandleContext handleContext, String des, String sel

public void longPressPoint(HandleContext handleContext, String des, String xy, int time) {
try {
xy = TextHandler.replaceTrans(xy, globalParams);
double x = Double.parseDouble(xy.substring(0, xy.indexOf(",")));
double y = Double.parseDouble(xy.substring(xy.indexOf(",") + 1));
int[] point = computedPoint(x, y);
Expand All @@ -530,6 +531,7 @@ public void keyCode(HandleContext handleContext, String key) {

public void tap(HandleContext handleContext, String des, String xy) {
try {
xy = TextHandler.replaceTrans(xy, globalParams);
double x = Double.parseDouble(xy.substring(0, xy.indexOf(",")));
double y = Double.parseDouble(xy.substring(xy.indexOf(",") + 1));
int[] point = computedPoint(x, y);
Expand All @@ -543,6 +545,8 @@ public void tap(HandleContext handleContext, String des, String xy) {

public void swipePoint(HandleContext handleContext, String des1, String xy1, String des2, String xy2) {
try {
xy1 = TextHandler.replaceTrans(xy1, globalParams);
xy2 = TextHandler.replaceTrans(xy2, globalParams);
double x1 = Double.parseDouble(xy1.substring(0, xy1.indexOf(",")));
double y1 = Double.parseDouble(xy1.substring(xy1.indexOf(",") + 1));
int[] point1 = computedPoint(x1, y1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public void test() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello", "world");
jsonObject.put("hello2", "world2");
jsonObject.put("xy", "1,2");
jsonObject.put("x", "0.3");
jsonObject.put("y", "0.4");
String s = TextHandler.replaceTrans("{{hello}}", jsonObject);
Assert.assertEquals("world", s);
s = TextHandler.replaceTrans("ss{{hello}}ss", jsonObject);
Expand All @@ -32,5 +35,9 @@ public void test() {
Assert.assertTrue(Integer.parseInt(s) <= 6 && Integer.parseInt(s) >= 3);
s = TextHandler.replaceTrans("{{random[h|2|3]}}", jsonObject);
Assert.assertEquals(1, s.length());
s = TextHandler.replaceTrans("{{x}},{{y}}", jsonObject);
Assert.assertEquals("0.3,0.4", s);
s = TextHandler.replaceTrans("{{xy}}", jsonObject);
Assert.assertEquals("1,2", s);
}
}

0 comments on commit 342e80c

Please sign in to comment.