Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 自动化步骤中坐标相关操作的控件元素也可以使用参数化&安卓获取控件属性时可直接获取中心坐标值 #450

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading