From bfb53a51a545cd0a03e03eab485ee712b88ae906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E5=AE=87=E7=90=A6?= <465645774@qq.com> Date: Fri, 30 Oct 2020 19:08:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=AF=BB=E5=8F=96=E6=97=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=AD=98=E5=82=A8=E5=85=83=E7=B4=A0=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../selenium/location/NoFileLocation.java | 165 ++++++++++++++++++ .../selenium/location/NoFileLocationTest.java | 92 ++++++++++ 2 files changed, 257 insertions(+) create mode 100644 src/main/java/pres/auxiliary/work/selenium/location/NoFileLocation.java create mode 100644 src/test/java/pres/auxiliary/work/selenium/location/NoFileLocationTest.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 new file mode 100644 index 0000000..7f633e4 --- /dev/null +++ b/src/main/java/pres/auxiliary/work/selenium/location/NoFileLocation.java @@ -0,0 +1,165 @@ +package pres.auxiliary.work.selenium.location; + +import java.util.ArrayList; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import pres.auxiliary.work.selenium.element.ElementType; + +/** + *

文件名:NoFileLocation.java

+ *

用途: + * 提供在不编写元素定位文件的情况下读取元素定位方法,通过该类,可存储一些临时添加的元素定位 + * 方式,并在编写脚本过程中进行调用 + *

+ *

编码时间:2020年10月29日上午8:38:24

+ *

修改时间:2020年10月29日上午8:38:24

+ * @author 彭宇琦 + * @version Ver1.0 + */ +public class NoFileLocation extends AbstractLocation implements WriteLocation, WriteTempletLocation { + /** + * 存储json的读取方式 + */ + private JsonLocation jsonLocation; + /** + * 用于存储当前通过方法添加的元素信息 + */ + private JSONObject nowLocationJson; + /** + * 用于存储通过新增方法添加的元素信息 + */ + private JSONObject newLocationJson; + + /** + * 构造类对象 + */ + public NoFileLocation() { + //初始化json参数 + nowLocationJson = new JSONObject(); + nowLocationJson.put(JsonLocation.KEY_TEMPLETE, new JSONObject()); + nowLocationJson.put(JsonLocation.KEY_ELEMENT, new JSONObject()); + //克隆当前元素json信息 + newLocationJson = (JSONObject) nowLocationJson.clone(); + + //初始化json定位类对象 + jsonLocation = new JsonLocation(nowLocationJson.toJSONString()); + } + + @Override + public void putElementLocation(String name, ByType byType, String value) { + //获取元素对象 + JSONObject elementJson = getElementJson(name); + //获取对象的location键值,若键值不存在,则添加键值 + if (!elementJson.containsKey(JsonLocation.KEY_LOCATION)) { + elementJson.put(JsonLocation.KEY_LOCATION, new JSONArray()); + } + + //封装定位方式 + JSONObject locationJson = new JSONObject(); + locationJson.put(JsonLocation.KEY_TYPE, byType.getValue()); + locationJson.put(JsonLocation.KEY_VALUE, value); + + //向定位方式组中添加定位方式 + elementJson.getJSONArray(JsonLocation.KEY_LOCATION).add(locationJson); + } + + @Override + public void putElementTempletLocation(String name, ByType byType, String templetId) { + //获取元素对象 + JSONObject elementJson = getElementJson(name); + //获取对象的location键值,若键值不存在,则添加键值 + if (!elementJson.containsKey(JsonLocation.KEY_LOCATION)) { + elementJson.put(JsonLocation.KEY_LOCATION, new JSONArray()); + } + + //封装定位方式 + JSONObject locationJson = new JSONObject(); + locationJson.put(JsonLocation.KEY_TEMP, templetId); + locationJson.put(JsonLocation.KEY_TYPE, byType.getValue()); + + //向定位方式组中添加定位方式 + elementJson.getJSONArray(JsonLocation.KEY_LOCATION).add(locationJson); + } + + @Override + public void putElementType(String name, ElementType elementType) { + //获取元素对象,并存储键值 + getElementJson(name).put(JsonLocation.KEY_TYPE, elementType.getValue()); + } + + @Override + public void putIframeNameList(String name, String iframeName) { + //获取元素对象,并存储键值 + getElementJson(name).put(JsonLocation.KEY_IFRAME, iframeName); + } + + @Override + public void putWaitTime(String name, long waitTime) { + //获取元素对象,并存储键值 + getElementJson(name).put(JsonLocation.KEY_WAIT, waitTime); + } + + @Override + public void putTemplet(String templetId, String templetValue) { + newLocationJson.getJSONObject((JsonLocation.KEY_TEMPLETE)).put(templetId, templetValue); + } + + @Override + public void putTempletReplaceKey(String name, String templetId, String key, String value) { + // TODO Auto-generated method stub + + } + + @Override + public ArrayList findElementByTypeList(String name) { + // TODO Auto-generated method stub + return null; + } + + @Override + public ArrayList findValueList(String name) { + // TODO Auto-generated method stub + return null; + } + + @Override + public ElementType findElementType(String name) { + // TODO Auto-generated method stub + return null; + } + + @Override + public ArrayList findIframeNameList(String name) { + // TODO Auto-generated method stub + return null; + } + + @Override + public long findWaitTime(String name) { + // TODO Auto-generated method stub + return 0; + } + + /** + * TODO 测试使用,返回元素信息 + */ + public String getJson() { + return newLocationJson.toJSONString(); + } + + /** + * 用于返回元素名称对应的元素json对象 + * @param name 元素名称 + * @return 元素名称对应的json对象 + */ + private JSONObject getElementJson(String name) { + //获取元素对象,若元素对象不存在,则创建相应的元素json,并返回 + if (!newLocationJson.getJSONObject(JsonLocation.KEY_ELEMENT).containsKey(name)) { + newLocationJson.getJSONObject(JsonLocation.KEY_ELEMENT).put(name, new JSONObject()); + } + + return newLocationJson.getJSONObject(JsonLocation.KEY_ELEMENT).getJSONObject(name); + } +} diff --git a/src/test/java/pres/auxiliary/work/selenium/location/NoFileLocationTest.java b/src/test/java/pres/auxiliary/work/selenium/location/NoFileLocationTest.java new file mode 100644 index 0000000..0971525 --- /dev/null +++ b/src/test/java/pres/auxiliary/work/selenium/location/NoFileLocationTest.java @@ -0,0 +1,92 @@ +package pres.auxiliary.work.selenium.location; + +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import pres.auxiliary.work.selenium.element.ElementType; + +public class NoFileLocationTest { + /** + * 定义类 + */ + NoFileLocation test; + + /** + * 初始化数据 + */ + @BeforeClass + public void init() { + test = new NoFileLocation(); + } + + @AfterClass + public void showData() { + System.out.println(test.getJson()); + } + + /** + * 用于测试{@link NoFileLocation#putElementLocation(String, ByType, String)}方法
+ */ + @Test + public void putElementLocationTest() { + test.putElementLocation("测试控件1", ByType.NAME, "测试控件11111"); + test.putElementLocation("测试控件2", ByType.CLASSNAME, "test_class"); + test.putElementLocation("测试控件3", ByType.CSS, "html body div"); + test.putElementLocation("测试控件4", ByType.ID, "test"); + test.putElementLocation("测试控件5", ByType.LINKTEXT, "测试控件"); + test.putElementLocation("测试控件6", ByType.TAGNAME, "span"); + test.putElementLocation("测试控件7", ByType.XPATH, "//*[text()='测试控件7']"); + test.putElementLocation("测试控件7", ByType.ID, "test7"); + test.putElementLocation("测试控件8", ByType.LINKTEXT, "测试控件"); + } + + /** + * 用于测试{@link NoFileLocation#putElementTempletLocation(String, ByType, String)}方法
+ */ + @Test + public void putElementTempletLocationTest() { + test.putElementTempletLocation("测试控件8", ByType.XPATH, "模板1"); + test.putElementTempletLocation("测试控件8", ByType.CLASSNAME, "模板2"); + } + + /** + * 用于测试{@link NoFileLocation#putElementType(String, pres.auxiliary.work.selenium.element.ElementType)}方法
+ * 预期:
+ * + */ + @Test + public void putElementTypeTest() { + test.putElementType("测试控件8", ElementType.COMMON_ELEMENT); + } + + /** + * 用于测试{@link NoFileLocation#putIframeNameList(String, String)}方法
+ * 预期:
+ * + */ + @Test + public void putIframeNameListTest() { + test.putIframeNameList("测试控件8", "窗体1"); + } + + /** + * 用于测试{@link NoFileLocation#putWaitTime(String, long)}方法
+ * 预期:
+ * + */ + @Test + public void putWaitTimeTest() { + test.putWaitTime("测试控件8", 10); + } + + /** + * 用于测试{@link NoFileLocation#putTemplet(String, String)}方法
+ * 预期:
+ * + */ + @Test + public void putTempletTest() { + test.putTemplet("模板1", "//*[text()='${name}']"); + } +}