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,将原有的“验证文本”改为“断言文本”,允许指定比较方式为等于,不等于,包含,不包含四种模式,满足更丰富的应用场景 #348

Merged
merged 5 commits into from
May 22, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ public void isExistEleNum(HandleContext handleContext, String des, String select
}
break;
default:
handleContext.setE(new AssertionError("未知的元素类型" + elementType + ",无法断言元素个数"));
handleContext.setE(new AssertionError("未知的元素类型" + elementType + "无法断言元素个数"));
break;
}
String runDetail = "期望个数:" + operation + " " + expectedCount + ",实际个数:" + " " + (elementList == null ? 0 : elementList.size());
Expand Down Expand Up @@ -2199,6 +2199,55 @@ public void switchTouchMode(HandleContext handleContext, String mode) {
AndroidTouchHandler.switchTouchMode(iDevice, AndroidTouchHandler.TouchMode.valueOf(mode));
}

/**
* >2.5.0版本,增强型的文本断言能力,支持指定断言的方式
*
* @param handleContext HandleContext
* @param des 元素名
* @param selector 元素定位方式
* @param pathValue 定位方式值
* @param operation 断言类型(equal,notEqual,contain,notContain)
* @param expect 期望值
* @param elementType 元素类型(原生,web,poco)
*/
public void getElementTextAndAssertWithOperation(HandleContext handleContext, String des, String selector,
String pathValue, String operation, String expect, int elementType) {
try {
String realValue = switch (elementType) {
case ANDROID_ELEMENT_TYPE -> getText(handleContext, des, selector, pathValue);
case WEB_ELEMENT_TYPE -> getWebViewText(handleContext, des, selector, pathValue);
case POCO_ELEMENT_TYPE -> getPocoText(handleContext, des, selector, pathValue);
default -> throw new SonicRespException("未支持的元素类型" + elementType + ",无法进行文本断言");
};
if (handleContext.getE() != null) {
return;
}
handleContext.setStepDes("断言" + des + "文本");
try {
expect = TextHandler.replaceTrans(expect, globalParams);
AssertUtil assertUtil = new AssertUtil();
StringBuilder sbLog = new StringBuilder("真实值:");
sbLog.append(realValue);
sbLog.append(",期望 ");
sbLog.append(assertUtil.getAssertDesc(operation));
sbLog.append(" ");
sbLog.append(expect);
handleContext.setDetail(sbLog.toString());
switch (operation) {
case "equal" -> assertEquals(realValue, expect);
case "notEqual" -> assertNotEquals(realValue, expect);
case "contain" -> assertTrue(realValue.contains(expect));
case "notContain" -> assertFalse(realValue.contains(expect));
default -> throw new SonicRespException("未支持的文本断言操作类型" + operation + ",无法进行文本断言");
}
} catch (AssertionError e) {
handleContext.setE(e);
}
} catch (Exception e) {
handleContext.setE(e);
}
}

public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Throwable {
JSONObject step = stepJSON.getJSONObject("step");
// 兼容childSteps
Expand Down Expand Up @@ -2234,9 +2283,6 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "sendKeysByActions" ->
sendKeysByActions(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
case "getText" ->
getTextAndAssert(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
case "isExistEle" ->
isExistEle(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getBoolean("content"));
Expand Down Expand Up @@ -2295,9 +2341,6 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "publicStep" -> publicStep(handleContext, step.getString("content"), step.getJSONArray("pubSteps"));
case "setDefaultFindWebViewElementInterval" ->
setDefaultFindWebViewElementInterval(handleContext, step.getInteger("content"), step.getInteger("text"));
case "getWebViewText" ->
getWebViewTextAndAssert(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
case "webElementScrollToView" ->
webElementScrollToView(handleContext, step.getString("text"), step.getString("content"), step.getString("content"));
case "isExistWebViewEle" ->
Expand Down Expand Up @@ -2356,9 +2399,6 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "getPocoTextValue" ->
globalParams.put(step.getString("content"), getPocoText(handleContext, eleList.getJSONObject(0).getString("eleName")
, eleList.getJSONObject(0).getString("eleType"), eleList.getJSONObject(0).getString("eleValue")));
case "getPocoText" ->
getPocoTextAndAssert(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
case "freezeSource" -> freezeSource(handleContext);
case "thawSource" -> thawSource(handleContext);
case "closePocoDriver" -> closePocoDriver(handleContext);
Expand All @@ -2375,6 +2415,35 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "getClipperByKeyboard" ->
globalParams.put(step.getString("content"), getClipperByKeyboard(handleContext));
case "setClipperByKeyboard" -> setClipperByKeyboard(handleContext, step.getString("content"));
// <= 2.5版本的文本断言语法(包括原生,webView,Poco三类),保留做兼容,老版本升级上来的存量用例继续可用
case "getText" ->
getTextAndAssert(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
case "getWebViewText" ->
getWebViewTextAndAssert(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
case "getPocoText" ->
getPocoTextAndAssert(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
// > 2.5版本的文本断言语法,支持指定断言的方式
case "assertText" -> getElementTextAndAssertWithOperation(handleContext,
eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"),
eleList.getJSONObject(0).getString("eleValue"),
step.getString("content"), step.getString("text"),
ANDROID_ELEMENT_TYPE);
case "assertWebViewText" -> getElementTextAndAssertWithOperation(handleContext,
eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"),
eleList.getJSONObject(0).getString("eleValue"),
step.getString("content"), step.getString("text"),
WEB_ELEMENT_TYPE);
case "assertPocoText" -> getElementTextAndAssertWithOperation(handleContext,
eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"),
eleList.getJSONObject(0).getString("eleValue"),
step.getString("content"), step.getString("text"),
POCO_ELEMENT_TYPE);
}
switchType(step, handleContext);
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/cloud/sonic/agent/tests/handlers/AssertUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,23 @@ public <T> boolean assertElementNum(String operation, int expectedCount, List<T>
return isSuccess;
}

public String getAssertDesc(String originOpe) {
switch (originOpe) {
case "equal" -> {
return "等于";
}
case "notEqual" -> {
return "不等于";
}
case "contain" -> {
return "包含";
}
case "notContain" -> {
return "不包含";
}
ZhouYixun marked this conversation as resolved.
Show resolved Hide resolved
default -> {
return "未知类型";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ public void isExistEleNum(HandleContext handleContext, String des, String select
}
break;
default:
handleContext.setE(new AssertionError("未知的元素类型" + elementType + ",无法断言元素个数"));
handleContext.setE(new AssertionError("未知的元素类型" + elementType + "无法断言元素个数"));
break;
}
String runDetail = "期望个数:" + operation + " " + expectedCount + ",实际个数:" + " " + (elementList == null ? 0 : elementList.size());
Expand Down Expand Up @@ -1480,6 +1480,53 @@ public void iteratorIOSElement(HandleContext handleContext, String des, String s
}
}

/**
* >2.5.0版本,增强型的文本断言能力,支持指定断言的方式
*
* @param handleContext HandleContext
* @param des 元素名
* @param selector 元素定位方式
* @param pathValue 定位方式值
* @param operation 断言类型(equal,notEqual,contain,notContain)
* @param expect 期望值
* @param elementType 元素类型(原生,web,poco)
*/
public void getElementTextAndAssertWithOperation(HandleContext handleContext, String des, String selector,
String pathValue, String operation, String expect, int elementType) {
try {
String realValue = switch (elementType) {
case IOS_ELEMENT_TYPE -> getText(handleContext, des, selector, pathValue);
case POCO_ELEMENT_TYPE -> getPocoText(handleContext, des, selector, pathValue);
default -> throw new SonicRespException("未支持的元素类型" + elementType + ",无法进行文本断言");
};
ZhouYixun marked this conversation as resolved.
Show resolved Hide resolved
if (handleContext.getE() != null) {
return;
}
handleContext.setStepDes("断言" + des + "文本");
try {
expect = TextHandler.replaceTrans(expect, globalParams);
AssertUtil assertUtil = new AssertUtil();
StringBuilder sbLog = new StringBuilder("真实值:");
sbLog.append(realValue);
sbLog.append(",期望 ");
sbLog.append(assertUtil.getAssertDesc(operation));
sbLog.append(" ");
sbLog.append(expect);
handleContext.setDetail(sbLog.toString());
switch (operation) {
case "equal" -> assertEquals(realValue, expect);
case "notEqual" -> assertNotEquals(realValue, expect);
case "contain" -> assertTrue(realValue.contains(expect));
case "notContain" -> assertFalse(realValue.contains(expect));
default -> throw new SonicRespException("未支持的文本断言操作类型" + operation + ",无法进行文本断言");
}
} catch (AssertionError e) {
handleContext.setE(e);
}
} catch (Exception e) {
handleContext.setE(e);
}
}

public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Throwable {
JSONObject step = stepJSON.getJSONObject("step");
Expand All @@ -1506,9 +1553,6 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "sendKeys" ->
sendKeys(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
case "getText" ->
getTextAndAssert(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
case "isExistEle" ->
isExistEle(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getBoolean("content"));
Expand Down Expand Up @@ -1596,9 +1640,6 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "getPocoTextValue" ->
globalParams.put(step.getString("content"), getPocoText(handleContext, eleList.getJSONObject(0).getString("eleName")
, eleList.getJSONObject(0).getString("eleType"), eleList.getJSONObject(0).getString("eleValue")));
case "getPocoText" ->
getPocoTextAndAssert(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
case "freezeSource" -> freezeSource(handleContext);
case "thawSource" -> thawSource(handleContext);
case "closePocoDriver" -> closePocoDriver(handleContext);
Expand All @@ -1608,6 +1649,26 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "iteratorIOSElement" ->
iteratorIOSElement(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"));
// <= 2.5版本的文本断言语法(包括原生,webView,Poco三类),保留做兼容,老版本升级上来的存量用例继续可用
case "getText" ->
getTextAndAssert(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
case "getPocoText" ->
getPocoTextAndAssert(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"));
// > 2.5版本的文本断言语法,支持指定断言的方式
case "assertText" -> getElementTextAndAssertWithOperation(handleContext,
eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"),
eleList.getJSONObject(0).getString("eleValue"),
step.getString("content"), step.getString("text"),
IOS_ELEMENT_TYPE);
case "assertPocoText" -> getElementTextAndAssertWithOperation(handleContext,
eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"),
eleList.getJSONObject(0).getString("eleValue"),
step.getString("content"), step.getString("text"),
POCO_ELEMENT_TYPE);
}
switchType(step, handleContext);
}
Expand Down