From 1f4d10eacbbd454a5530d549ff0517e1dd0581ce Mon Sep 17 00:00:00 2001 From: caofengbin <1050430934@qq.com> Date: Sat, 20 May 2023 16:24:11 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=AD=E8=A8=80Android?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E4=B8=8A=E5=AD=98=E5=9C=A8=E7=9A=84=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/handlers/AndroidStepHandler.java | 47 +++++++++++++++ .../agent/tests/handlers/AssertUtil.java | 57 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100755 src/main/java/org/cloud/sonic/agent/tests/handlers/AssertUtil.java diff --git a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java index e248609a..3fa3c0da 100755 --- a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java +++ b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java @@ -95,6 +95,9 @@ public class AndroidStepHandler { private String targetPackage = ""; + // 断言元素个数,三种元素类型的定义 + private static final int ANDROID_ELEMENT_TYPE = 1001; + public String getTargetPackage() { return targetPackage; } @@ -828,6 +831,45 @@ 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; + 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("验证网页网址"); @@ -2141,6 +2183,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")); diff --git a/src/main/java/org/cloud/sonic/agent/tests/handlers/AssertUtil.java b/src/main/java/org/cloud/sonic/agent/tests/handlers/AssertUtil.java new file mode 100755 index 00000000..eb5e6799 --- /dev/null +++ b/src/main/java/org/cloud/sonic/agent/tests/handlers/AssertUtil.java @@ -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 . + */ +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 范型参数 + * @return 比较结果 + */ + public boolean assertElementNum(String operation, int expectedCount, List 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; + } + +} From 32b0163dde8f6f0240cc776411491ed5a845ceb6 Mon Sep 17 00:00:00 2001 From: caofengbin <1050430934@qq.com> Date: Sat, 20 May 2023 16:48:40 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=96=AD=E8=A8=80=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E4=B8=AA=E6=95=B0=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81web=E5=B9=B3=E5=8F=B0=EF=BC=8Cagent=E7=AB=AF=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BF=AE=E6=94=B9=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/handlers/AndroidStepHandler.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java index 3fa3c0da..64702aaa 100755 --- a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java +++ b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java @@ -97,6 +97,7 @@ public class AndroidStepHandler { // 断言元素个数,三种元素类型的定义 private static final int ANDROID_ELEMENT_TYPE = 1001; + private static final int WEB_ELEMENT_TYPE = 1002; public String getTargetPackage() { return targetPackage; @@ -855,6 +856,14 @@ public void isExistEleNum(HandleContext handleContext, String des, String select } catch (Exception ignored) { } break; + case WEB_ELEMENT_TYPE: + try { + elementList = findWebEleList(selector, pathValue); + } catch (SonicRespException e) { + // 查找元素不存在时会抛异常 + } catch (Exception ignored) { + } + break; default: handleContext.setE(new AssertionError("未知的元素类型" + elementType + ",无法断言元素个数")); break; @@ -1834,6 +1843,46 @@ public WebElement findWebEle(String selector, String pathValue) throws SonicResp return we; } + public List findWebEleList(String selector, String pathValue) throws SonicRespException { + List 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); @@ -2246,6 +2295,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")); From 465df6dfe7857f9761cd0f4235f5fcf3123555c1 Mon Sep 17 00:00:00 2001 From: caofengbin <1050430934@qq.com> Date: Sat, 20 May 2023 17:02:09 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=96=AD=E8=A8=80=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E4=B8=AA=E6=95=B0=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81iOS=E5=B9=B3=E5=8F=B0=EF=BC=8Cagent=E7=AB=AF=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BF=AE=E6=94=B9=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent/tests/handlers/IOSStepHandler.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/main/java/org/cloud/sonic/agent/tests/handlers/IOSStepHandler.java b/src/main/java/org/cloud/sonic/agent/tests/handlers/IOSStepHandler.java index d42bd0e7..53982256 100755 --- a/src/main/java/org/cloud/sonic/agent/tests/handlers/IOSStepHandler.java +++ b/src/main/java/org/cloud/sonic/agent/tests/handlers/IOSStepHandler.java @@ -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 WEB_ELEMENT_TYPE = 1002; + public String getTargetPackage() { return targetPackage; } @@ -658,6 +661,45 @@ 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; + 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); @@ -1463,6 +1505,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")); From e7ebd9d2bb62b804b02d5238395c011e41a15484 Mon Sep 17 00:00:00 2001 From: caofengbin <1050430934@qq.com> Date: Sat, 20 May 2023 17:20:00 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=96=AD=E8=A8=80=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E4=B8=AA=E6=95=B0=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81POCO=E5=AE=9A=E4=BD=8D=E6=96=B9=E5=BC=8F=EF=BC=8Cagent?= =?UTF-8?q?=E7=AB=AF=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent/tests/handlers/AndroidStepHandler.java | 12 ++++++++++++ .../sonic/agent/tests/handlers/IOSStepHandler.java | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java index 64702aaa..8a681ef5 100755 --- a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java +++ b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java @@ -98,6 +98,7 @@ public class AndroidStepHandler { // 断言元素个数,三种元素类型的定义 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; @@ -864,6 +865,13 @@ public void isExistEleNum(HandleContext handleContext, String des, String select } catch (Exception ignored) { } break; + case POCO_ELEMENT_TYPE: + try { + elementList = findPocoEleList(selector, pathValue); + } catch (Throwable e) { + // 查找元素不存在时会抛异常 + } + break; default: handleContext.setE(new AssertionError("未知的元素类型" + elementType + ",无法断言元素个数")); break; @@ -2323,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")); diff --git a/src/main/java/org/cloud/sonic/agent/tests/handlers/IOSStepHandler.java b/src/main/java/org/cloud/sonic/agent/tests/handlers/IOSStepHandler.java index 53982256..022ca1cb 100755 --- a/src/main/java/org/cloud/sonic/agent/tests/handlers/IOSStepHandler.java +++ b/src/main/java/org/cloud/sonic/agent/tests/handlers/IOSStepHandler.java @@ -87,7 +87,7 @@ public class IOSStepHandler { private String targetPackage = ""; private static final int IOS_ELEMENT_TYPE = 1004; - private static final int WEB_ELEMENT_TYPE = 1002; + private static final int POCO_ELEMENT_TYPE = 1005; public String getTargetPackage() { return targetPackage; @@ -685,6 +685,13 @@ public void isExistEleNum(HandleContext handleContext, String des, String select } catch (Exception ignored) { } break; + case POCO_ELEMENT_TYPE: + try { + elementList = findPocoEleList(selector, pathValue); + } catch (Throwable e) { + // 查找元素不存在时会抛异常 + } + break; default: handleContext.setE(new AssertionError("未知的元素类型" + elementType + ",无法断言元素个数")); break; @@ -1564,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"));