From d8818efdc176935467456ca9bb3450f174fac7ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=BD=AD=E5=AE=87=E7=90=A6?= <465645774@qq.com>
Date: Mon, 2 Nov 2020 18:59:27 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=97=A0=E6=96=87=E4=BB=B6?=
=?UTF-8?q?=E5=BD=A2=E5=BC=8F=E8=AF=BB=E5=8F=96=E5=85=83=E7=B4=A0=E5=AE=9A?=
=?UTF-8?q?=E4=BD=8D=E6=96=B9=E5=BC=8F=E7=B1=BB=E5=AF=B9=E8=B1=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../selenium/location/NoFileLocation.java | 79 +++++++++++-----
.../location/UndefinedModeException.java | 40 ---------
.../location/WriteTempletLocation.java | 5 +-
.../selenium/location/NoFileLocationTest.java | 89 +++++++++++++++++--
4 files changed, 144 insertions(+), 69 deletions(-)
delete mode 100644 src/main/java/pres/auxiliary/work/selenium/location/UndefinedModeException.java
diff --git a/src/main/java/pres/auxiliary/work/selenium/location/NoFileLocation.java b/src/main/java/pres/auxiliary/work/selenium/location/NoFileLocation.java
index 7f633e4..400b686 100644
--- a/src/main/java/pres/auxiliary/work/selenium/location/NoFileLocation.java
+++ b/src/main/java/pres/auxiliary/work/selenium/location/NoFileLocation.java
@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import pres.auxiliary.work.selenium.element.ElementType;
+import pres.auxiliary.work.selenium.location.UndefinedElementException.ExceptionElementType;
/**
*
文件名:NoFileLocation.java
@@ -40,10 +41,9 @@ public class NoFileLocation extends AbstractLocation implements WriteLocation, W
nowLocationJson = new JSONObject();
nowLocationJson.put(JsonLocation.KEY_TEMPLETE, new JSONObject());
nowLocationJson.put(JsonLocation.KEY_ELEMENT, new JSONObject());
- //克隆当前元素json信息
- newLocationJson = (JSONObject) nowLocationJson.clone();
+ //复制当前元素json信息
+ newLocationJson = JSONObject.parseObject(nowLocationJson.toJSONString());
- //初始化json定位类对象
jsonLocation = new JsonLocation(nowLocationJson.toJSONString());
}
@@ -108,45 +108,61 @@ public class NoFileLocation extends AbstractLocation implements WriteLocation, W
@Override
public void putTempletReplaceKey(String name, String templetId, String key, String value) {
- // TODO Auto-generated method stub
+ //获取元素定位方式列表
+ JSONObject elementJson = getElementJson(name);
+ //获取元素定位方式集合
+ JSONArray locationList = elementJson.getJSONArray(JsonLocation.KEY_LOCATION);
+ //若当前元素不存在定位方式集合,则抛出异常
+ if (locationList == null) {
+ throw new UndefinedElementException(templetId, ExceptionElementType.ELEMENT);
+ }
+ //遍历集合,获取与当前指定的模板id一致的模板,在其下记录相应的key与value
+ for (int i = 0; i < locationList.size(); i++) {
+ JSONObject locationJson = locationList.getJSONObject(i);
+ //判断当前json是否包含模板Id的key
+ if (locationJson.containsKey(JsonLocation.KEY_TEMP)) {
+ String tempId = locationJson.getString(JsonLocation.KEY_TEMP);
+ //判断当前id是否与所传的id一致,若一致,则在json下记录相应的属性值,并结束当前方法
+ if (templetId.equals(tempId)) {
+ locationJson.put(key, value);
+ return;
+ }
+ }
+ }
+
+ //若循环完毕后仍未找到模板,则抛出异常
+ throw new UndefinedElementException(templetId, ExceptionElementType.ELEMENT);
}
@Override
public ArrayList findElementByTypeList(String name) {
- // TODO Auto-generated method stub
- return null;
+ //对json定位方式读取类进行重构判断,并调用相应的方法
+ return getJsonLocation().findElementByTypeList(name);
}
@Override
public ArrayList findValueList(String name) {
- // TODO Auto-generated method stub
- return null;
+ //对json定位方式读取类进行重构判断,并调用相应的方法
+ return getJsonLocation().findValueList(name);
}
@Override
public ElementType findElementType(String name) {
- // TODO Auto-generated method stub
- return null;
+ //对json定位方式读取类进行重构判断,并调用相应的方法
+ return getJsonLocation().findElementType(name);
}
@Override
public ArrayList findIframeNameList(String name) {
- // TODO Auto-generated method stub
- return null;
+ //对json定位方式读取类进行重构判断,并调用相应的方法
+ return getJsonLocation().findIframeNameList(name);
}
@Override
public long findWaitTime(String name) {
- // TODO Auto-generated method stub
- return 0;
- }
-
- /**
- * TODO 测试使用,返回元素信息
- */
- public String getJson() {
- return newLocationJson.toJSONString();
+ //对json定位方式读取类进行重构判断,并调用相应的方法
+ return getJsonLocation().findWaitTime(name);
}
/**
@@ -162,4 +178,25 @@ public class NoFileLocation extends AbstractLocation implements WriteLocation, W
return newLocationJson.getJSONObject(JsonLocation.KEY_ELEMENT).getJSONObject(name);
}
+
+ /**
+ * 用于构建JsonLocation类,若当前json未改变,则不重新构造;反之,则重新构造元素定位方式
+ * @return 返回JsonLocation类
+ */
+ private JsonLocation getJsonLocation() {
+ if (!isJsonChange()) {
+ jsonLocation.analysisJson(newLocationJson.toJSONString());
+ nowLocationJson = JSONObject.parseObject(newLocationJson.toJSONString());
+ }
+
+ return jsonLocation;
+ }
+
+ /**
+ * 用于判断当前json是否有变化,即是否对当前的json进行过变更
+ * @return json是否存在变化
+ */
+ private boolean isJsonChange() {
+ return nowLocationJson.equals(newLocationJson);
+ }
}
diff --git a/src/main/java/pres/auxiliary/work/selenium/location/UndefinedModeException.java b/src/main/java/pres/auxiliary/work/selenium/location/UndefinedModeException.java
deleted file mode 100644
index 31abf8f..0000000
--- a/src/main/java/pres/auxiliary/work/selenium/location/UndefinedModeException.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package pres.auxiliary.work.selenium.location;
-
-/**
- * 若传入的模型参数有误时抛出的异常
- * @author 彭宇琦
- * @version V1.0
- *
- */
-public class UndefinedModeException extends RuntimeException {
-
- private static final long serialVersionUID = 1L;
-
- public UndefinedModeException() {
- super();
- // TODO Auto-generated constructor stub
- }
-
- public UndefinedModeException(String arg0, Throwable arg1, boolean arg2,
- boolean arg3) {
- super(arg0, arg1, arg2, arg3);
- // TODO Auto-generated constructor stub
- }
-
- public UndefinedModeException(String arg0, Throwable arg1) {
- super(arg0, arg1);
- // TODO Auto-generated constructor stub
- }
-
- public UndefinedModeException(String arg0) {
- super(arg0);
- // TODO Auto-generated constructor stub
- }
-
- public UndefinedModeException(Throwable arg0) {
- super(arg0);
- // TODO Auto-generated constructor stub
- }
-
-
-}
diff --git a/src/main/java/pres/auxiliary/work/selenium/location/WriteTempletLocation.java b/src/main/java/pres/auxiliary/work/selenium/location/WriteTempletLocation.java
index 2e43f04..de56cc2 100644
--- a/src/main/java/pres/auxiliary/work/selenium/location/WriteTempletLocation.java
+++ b/src/main/java/pres/auxiliary/work/selenium/location/WriteTempletLocation.java
@@ -20,16 +20,17 @@ public interface WriteTempletLocation {
public abstract void putTemplet(String templetId, String templetValue);
/**
- * 用于设置元素调用模板时,该内容允许多次设置,将存储多个替换模板的词语
+ * 用于设置元素调用模板时,该内容允许多次设置,将存储多个替换模板的词语。若元素不存在相应的模板,则抛出异常
* @param name 元素名称
* @param templetId 模板id
* @param key 被替换的关键词
* @param value 替换的内容
+ * @throws UndefinedElementException 模板id不存在时抛出的异常
*/
public abstract void putTempletReplaceKey(String name, String templetId, String key, String value);
/**
- * 用于设置元素调用模板的id,该内容允许多次设置,将存储多个元素定位方式模板
+ * 用于设置元素调用模板的id,该内容允许多次设置,将存储多个元素定位方式模板。若元素名称不存在,则创建元素
* @param name 元素名称
* @param byType 元素定位类型{@link ByType}枚举
* @param templetId 模板id
diff --git a/src/test/java/pres/auxiliary/work/selenium/location/NoFileLocationTest.java b/src/test/java/pres/auxiliary/work/selenium/location/NoFileLocationTest.java
index 0971525..e34584d 100644
--- a/src/test/java/pres/auxiliary/work/selenium/location/NoFileLocationTest.java
+++ b/src/test/java/pres/auxiliary/work/selenium/location/NoFileLocationTest.java
@@ -1,6 +1,5 @@
package pres.auxiliary.work.selenium.location;
-import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@@ -20,11 +19,6 @@ public class NoFileLocationTest {
test = new NoFileLocation();
}
- @AfterClass
- public void showData() {
- System.out.println(test.getJson());
- }
-
/**
* 用于测试{@link NoFileLocation#putElementLocation(String, ByType, String)}方法
*/
@@ -89,4 +83,87 @@ public class NoFileLocationTest {
public void putTempletTest() {
test.putTemplet("模板1", "//*[text()='${name}']");
}
+
+ /**
+ * 用于测试{@link NoFileLocation#putTempletReplaceKey(String, String, String, String)}方法
+ * 预期:
+ *
+ */
+ @Test
+ public void putTempletReplaceKeyTest_NoException() {
+ test.putElementLocation("测试控件8", ByType.LINKTEXT, "测试控件");
+ test.putElementTempletLocation("测试控件8", ByType.XPATH, "模板1");
+ test.putElementTempletLocation("测试控件8", ByType.CLASSNAME, "模板2");
+
+ test.putTempletReplaceKey("测试控件8", "模板1", "id", "test");
+ }
+
+ /**
+ * 用于测试{@link NoFileLocation#putTempletReplaceKey(String, String, String, String)}方法
+ * 预期:
+ *
+ */
+ @Test
+ public void putTempletReplaceKeyTest_Exception() {
+ try {
+ test.putTempletReplaceKey("测试控件8", "模板1", "id", "test");
+ } catch (Exception e) {
+ System.out.println("抛出异常,无元素定位方式");
+ }
+
+ test.putElementLocation("测试控件8", ByType.LINKTEXT, "测试控件");
+ test.putElementTempletLocation("测试控件8", ByType.XPATH, "模板1");
+ test.putElementTempletLocation("测试控件8", ByType.CLASSNAME, "模板2");
+
+ try {
+ test.putTempletReplaceKey("测试控件8", "模板3", "id", "test");
+ } catch (Exception e) {
+ System.out.println("抛出异常,不存在相应的元素定位模板");
+ }
+ }
+
+ /**
+ * 由于返回方法调用{@link JsonLocation}类的返回方法,故统一进行测试
+ */
+ @Test
+ public void getElementLocationTest() {
+ String name = "测试控件8";
+
+ //添加定位方式
+ test.putElementLocation(name, ByType.LINKTEXT, "测试控件");
+ test.putElementTempletLocation(name, ByType.XPATH, "模板1");
+ //添加模板
+ test.putTemplet("模板1", "//*[text()='${name}']/span[@id=${id}]/span[text()='${key}']");
+ //添加定位属性
+ test.putTempletReplaceKey(name, "模板1", "id", "test");
+ test.putTempletReplaceKey(name, "模板1", "name", "控件8");
+ //添加元素
+ test.putElementType(name, ElementType.COMMON_ELEMENT);
+ //添加元素所在窗体
+ test.putIframeNameList(name, "窗体1");
+ //添加元素等待时间
+ test.putWaitTime(name, 10);
+
+ //添加窗体
+ test.putElementLocation("窗体1", ByType.LINKTEXT, "窗体");
+
+ //读取元素信息
+ System.out.println("元素定位内容:" + test.findValueList(name));
+ System.out.println("元素定位方式:" + test.findElementByTypeList(name));
+ System.out.println("元素类型:" + test.findElementType(name));
+ System.out.println("元素所在窗体:" + test.findIframeNameList(name));
+ System.out.println("元素等待时间:" + test.findWaitTime(name));
+ }
+
+ /**
+ * 测试json改变与不改变时,是否有重新构建读取类对象(需要断点)
+ */
+ @Test
+ public void changeJsonGetElementTest() {
+ test.putElementLocation("测试控件1", ByType.LINKTEXT, "测试控件1");
+ test.putElementLocation("测试控件2", ByType.LINKTEXT, "测试控件2");
+
+ System.out.println("元素定位内容:" + test.findValueList("测试控件1"));
+ System.out.println("元素定位内容:" + test.findValueList("测试控件2"));
+ }
}