Skip to content

Commit

Permalink
Merge pull request #344 from caofengbin/feature/assert_element_number
Browse files Browse the repository at this point in the history
feat:支持断言目标元素存在的数量
  • Loading branch information
ZhouYixun authored May 21, 2023
2 parents 22ecc94 + e7ebd9d commit 55a4313
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public class AndroidStepHandler {

private String targetPackage = "";

// 断言元素个数,三种元素类型的定义
private static final int ANDROID_ELEMENT_TYPE = 1001;
private static final int WEB_ELEMENT_TYPE = 1002;
private static final int POCO_ELEMENT_TYPE = 1003;

public String getTargetPackage() {
return targetPackage;
}
Expand Down Expand Up @@ -828,6 +833,60 @@ public void isExistEle(HandleContext handleContext, String des, String selector,
}
}

/**
* 断言元素存在个数的方法
*
* @param handleContext HandleContext
* @param des 元素名称
* @param selector 定位方式
* @param pathValue 定位值
* @param operation 操作类型
* @param expectedCount 期望数量
* @param elementType 元素的类型
*/
public void isExistEleNum(HandleContext handleContext, String des, String selector, String pathValue, String operation
, int expectedCount, int elementType) {
handleContext.setStepDes("判断控件 " + des + " 存在的个数");
List elementList = new ArrayList<>();
switch (elementType) {
case ANDROID_ELEMENT_TYPE:
try {
elementList = findEleList(selector, pathValue);
} catch (SonicRespException e) {
// 查找元素不存在时会抛异常
} catch (Exception ignored) {
}
break;
case WEB_ELEMENT_TYPE:
try {
elementList = findWebEleList(selector, pathValue);
} catch (SonicRespException e) {
// 查找元素不存在时会抛异常
} catch (Exception ignored) {
}
break;
case POCO_ELEMENT_TYPE:
try {
elementList = findPocoEleList(selector, pathValue);
} catch (Throwable e) {
// 查找元素不存在时会抛异常
}
break;
default:
handleContext.setE(new AssertionError("未知的元素类型" + elementType + ",无法断言元素个数"));
break;
}
String runDetail = "期望个数:" + operation + " " + expectedCount + ",实际个数:" + " " + (elementList == null ? 0 : elementList.size());
handleContext.setDetail(runDetail);
AssertUtil assertUtil = new AssertUtil();
boolean isSuccess = assertUtil.assertElementNum(operation, expectedCount, elementList);
try {
assertTrue(isSuccess);
} catch (AssertionError e) {
handleContext.setE(e);
}
}

public void getUrl(HandleContext handleContext, String expect) {
String title = chromeDriver.getCurrentUrl();
handleContext.setStepDes("验证网页网址");
Expand Down Expand Up @@ -1792,6 +1851,46 @@ public WebElement findWebEle(String selector, String pathValue) throws SonicResp
return we;
}

public List<WebElement> findWebEleList(String selector, String pathValue) throws SonicRespException {
List<WebElement> we = new ArrayList<>();
pathValue = TextHandler.replaceTrans(pathValue, globalParams);
int wait = 0;
String errMsg = "";
while (wait < retryWebInit) {
wait++;
try {
switch (selector) {
case "id" -> we = chromeDriver.findElements(By.id(pathValue));
case "name" -> we = chromeDriver.findElements(By.name(pathValue));
case "xpath" -> we = chromeDriver.findElements(By.xpath(pathValue));
case "cssSelector" -> we = chromeDriver.findElements(By.cssSelector(pathValue));
case "className" -> we = chromeDriver.findElements(By.className(pathValue));
case "tagName" -> we = chromeDriver.findElements(By.tagName(pathValue));
case "linkText" -> we = chromeDriver.findElements(By.linkText(pathValue));
case "partialLinkText" -> we = chromeDriver.findElements(By.partialLinkText(pathValue));
default ->
log.sendStepLog(StepType.ERROR, "查找控件元素失败", "这个控件元素类型: " + selector + " 不存在!!!");
}
if (we != null) {
break;
}
} catch (Throwable e) {
errMsg = e.getMessage();
}
if (wait < retryWebInit) {
try {
Thread.sleep(intervalWebInit);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (we == null) {
throw new SonicRespException(errMsg);
}
return we;
}

public AndroidElement findEle(String selector, String pathValue) throws SonicRespException {
AndroidElement we = null;
pathValue = TextHandler.replaceTrans(pathValue, globalParams);
Expand Down Expand Up @@ -2141,6 +2240,11 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "isExistEle" ->
isExistEle(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getBoolean("content"));
case "isExistEleNum" -> isExistEleNum(handleContext, eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"),
eleList.getJSONObject(0).getString("eleValue"),
step.getString("content"),
step.getInteger("text"), ANDROID_ELEMENT_TYPE);
case "clear" ->
clear(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"));
Expand Down Expand Up @@ -2199,6 +2303,10 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "isExistWebViewEle" ->
isExistWebViewEle(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getBoolean("content"));
case "isExistWebViewEleNum" -> isExistEleNum(handleContext,
eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"),
step.getInteger("text"), WEB_ELEMENT_TYPE);
case "webViewClear" ->
webViewClear(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"));
Expand All @@ -2223,6 +2331,10 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "isExistPocoEle" ->
isExistPocoEle(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getBoolean("content"));
case "isExistPocoEleNum" -> isExistEleNum(handleContext,
eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"),
step.getInteger("text"), POCO_ELEMENT_TYPE);
case "pocoClick" ->
pocoClick(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"));
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/cloud/sonic/agent/tests/handlers/AssertUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* sonic-agent Agent of Sonic Cloud Real Machine Platform.
* Copyright (C) 2023 SonicCloudOrg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.cloud.sonic.agent.tests.handlers;

import java.util.List;

/**
* Created by fengbincao on 2023/05/20
*/
public class AssertUtil {

/**
* 数量断言,泛型方法
*
* @param operation 操作类型
* @param expectedCount 期望数量
* @param elementList 元素列表
* @param <T> 范型参数
* @return 比较结果
*/
public <T> boolean assertElementNum(String operation, int expectedCount, List<T> elementList) {
boolean isSuccess;
if (elementList == null || elementList.size() == 0) {
isSuccess = switch (operation) {
case "<" -> expectedCount > 0;
case "<=" -> true;
case ">" -> false;
default -> expectedCount == 0;
};
} else {
isSuccess = switch (operation) {
case "<" -> elementList.size() < expectedCount;
case "<=" -> elementList.size() <= expectedCount;
case ">" -> elementList.size() > expectedCount;
case ">=" -> elementList.size() >= expectedCount;
default -> elementList.size() == expectedCount;
};
}
return isSuccess;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public class IOSStepHandler {
private int targetPort = 0;
private String targetPackage = "";

private static final int IOS_ELEMENT_TYPE = 1004;
private static final int POCO_ELEMENT_TYPE = 1005;

public String getTargetPackage() {
return targetPackage;
}
Expand Down Expand Up @@ -658,6 +661,52 @@ public void isExistEle(HandleContext handleContext, String des, String selector,
}
}

/**
* 断言元素存在个数的方法
*
* @param handleContext HandleContext
* @param des 元素名称
* @param selector 定位方式
* @param pathValue 定位值
* @param operation 操作类型
* @param expectedCount 期望数量
* @param elementType 元素的类型
*/
public void isExistEleNum(HandleContext handleContext, String des, String selector, String pathValue, String operation
, int expectedCount, int elementType) {
handleContext.setStepDes("判断控件 " + des + " 存在的个数");
List elementList = new ArrayList<>();
switch (elementType) {
case IOS_ELEMENT_TYPE:
try {
elementList = findEleList(selector, pathValue);
} catch (SonicRespException e) {
// 查找元素不存在时会抛异常
} catch (Exception ignored) {
}
break;
case POCO_ELEMENT_TYPE:
try {
elementList = findPocoEleList(selector, pathValue);
} catch (Throwable e) {
// 查找元素不存在时会抛异常
}
break;
default:
handleContext.setE(new AssertionError("未知的元素类型" + elementType + ",无法断言元素个数"));
break;
}
String runDetail = "期望个数:" + operation + " " + expectedCount + ",实际个数:" + " " + (elementList == null ? 0 : elementList.size());
handleContext.setDetail(runDetail);
AssertUtil assertUtil = new AssertUtil();
boolean isSuccess = assertUtil.assertElementNum(operation, expectedCount, elementList);
try {
assertTrue(isSuccess);
} catch (AssertionError e) {
handleContext.setE(e);
}
}

public void getElementAttr(HandleContext handleContext, String des, String selector, String pathValue, String attr, String expect) {
handleContext.setStepDes("验证控件 " + des + " 属性");
handleContext.setDetail("属性:" + attr + ",期望值:" + expect);
Expand Down Expand Up @@ -1463,6 +1512,11 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "isExistEle" ->
isExistEle(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getBoolean("content"));
case "isExistEleNum" -> isExistEleNum(handleContext, eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"),
eleList.getJSONObject(0).getString("eleValue"),
step.getString("content"),
step.getInteger("text"), IOS_ELEMENT_TYPE);
case "clear" ->
clear(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"));
Expand Down Expand Up @@ -1517,6 +1571,10 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "isExistPocoEle" ->
isExistPocoEle(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getBoolean("content"));
case "isExistPocoEleNum" -> isExistEleNum(handleContext,
eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("content"),
step.getInteger("text"), POCO_ELEMENT_TYPE);
case "pocoClick" ->
pocoClick(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"));
Expand Down

0 comments on commit 55a4313

Please sign in to comment.