From 96a7cfdbc2c6b2e8f5b14e671ddb6ae75f34b4b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E5=AE=87=E7=90=A6?= <465645774@qq.com> Date: Thu, 29 Oct 2020 08:28:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90json=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E5=85=83=E7=B4=A0=E5=AE=9A=E4=BD=8D=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E8=AF=BB=E5=8F=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pres/auxiliary/report/TestReport.java | 2 +- .../work/selenium/element/AbstractBy.java | 6 +- .../work/selenium/element/ElementData.java | 14 +- ...bstractRead.java => AbstractLocation.java} | 50 ++- .../work/selenium/location/JsonLocation.java | 284 ++++++++++++++++++ .../location/UndefinedElementException.java | 19 +- .../{ReadXml.java => XmlLocation.java} | 41 +-- .../selenium/datadriven/TestDataDriven.java | 158 ---------- .../selenium/element/ElementDataTest.java | 8 +- .../selenium/location/JsonLocationTest.java | 162 ++++++++++ ...{ReadXmlTest.java => XmlLocationTest.java} | 18 +- .../selenium/location/测试用json文件.txt | 261 ++++++++++++++++ .../work/selenium/xml/ReadXmlTest.java | 34 +-- 13 files changed, 819 insertions(+), 238 deletions(-) rename src/main/java/pres/auxiliary/work/selenium/location/{AbstractRead.java => AbstractLocation.java} (64%) create mode 100644 src/main/java/pres/auxiliary/work/selenium/location/JsonLocation.java rename src/main/java/pres/auxiliary/work/selenium/location/{ReadXml.java => XmlLocation.java} (84%) delete mode 100644 src/test/java/pres/auxiliary/work/selenium/datadriven/TestDataDriven.java create mode 100644 src/test/java/pres/auxiliary/work/selenium/location/JsonLocationTest.java rename src/test/java/pres/auxiliary/work/selenium/location/{ReadXmlTest.java => XmlLocationTest.java} (81%) create mode 100644 src/test/java/pres/auxiliary/work/selenium/location/测试用json文件.txt diff --git a/src/main/java/pres/auxiliary/report/TestReport.java b/src/main/java/pres/auxiliary/report/TestReport.java index 4951dee..58b2c30 100644 --- a/src/main/java/pres/auxiliary/report/TestReport.java +++ b/src/main/java/pres/auxiliary/report/TestReport.java @@ -1023,7 +1023,7 @@ public class TestReport extends AbstractReport { // 定义Control对象,读取禅道元素定位信息配置文件 Event event = Event.newInstance(d); event.setXmlFile(new File(xpath)); - ReadXml r = new ReadXml(new File(xpath)); + XmlLocation r = new XmlLocation(new File(xpath)); // 操作浏览器 event.getTextEvent().input("用户名", username); event.getTextEvent().input("密码", password); diff --git a/src/main/java/pres/auxiliary/work/selenium/element/AbstractBy.java b/src/main/java/pres/auxiliary/work/selenium/element/AbstractBy.java index b42e788..43e97d3 100644 --- a/src/main/java/pres/auxiliary/work/selenium/element/AbstractBy.java +++ b/src/main/java/pres/auxiliary/work/selenium/element/AbstractBy.java @@ -11,7 +11,7 @@ import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.WebDriverWait; import pres.auxiliary.work.selenium.brower.AbstractBrower; -import pres.auxiliary.work.selenium.location.AbstractRead; +import pres.auxiliary.work.selenium.location.AbstractLocation; import pres.auxiliary.work.selenium.location.ByType; /** @@ -39,7 +39,7 @@ public abstract class AbstractBy { /** * 存储元素的定位方式读取类对象,由于多个子类之间需要互通,故此处标记为static */ - protected static AbstractRead read; + protected static AbstractLocation read; /** * 用于存储浏览器对象 @@ -111,7 +111,7 @@ public abstract class AbstractBy { * @param xmlFile 存储元素定位方式的xml文件对象 * @param isBreakRootFrame 是否需要将窗体切回到顶层 */ - public void setReadMode(AbstractRead read, boolean isBreakRootFrame) { + public void setReadMode(AbstractLocation read, boolean isBreakRootFrame) { AbstractBy.read = read; if (isBreakRootFrame) { diff --git a/src/main/java/pres/auxiliary/work/selenium/element/ElementData.java b/src/main/java/pres/auxiliary/work/selenium/element/ElementData.java index b3f1f9d..5ae4dd8 100644 --- a/src/main/java/pres/auxiliary/work/selenium/element/ElementData.java +++ b/src/main/java/pres/auxiliary/work/selenium/element/ElementData.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; -import pres.auxiliary.work.selenium.location.AbstractRead; +import pres.auxiliary.work.selenium.location.AbstractLocation; import pres.auxiliary.work.selenium.location.ByType; /** @@ -51,9 +51,9 @@ public class ElementData { /** * 根据元素名称,在配置文件中查找元素,将元素的信息进行存储 * @param name 元素名称 - * @param AbstractRead 配置文件类对象 + * @param AbstractLocation 配置文件类对象 */ - public ElementData(String name, AbstractRead read) { + public ElementData(String name, AbstractLocation read) { //存储元素名称 this.name = name; @@ -90,7 +90,7 @@ public class ElementData { if (!linkWordList.isEmpty()) { for (int i = 0; i < valueList.size(); i++) { //判断字符串是否包含替换词语的开始标志,若不包含,则进行不进行替换操作 - if (!valueList.get(i).contains(AbstractRead.START_SIGN)) { + if (!valueList.get(i).contains(AbstractLocation.START_SIGN)) { continue; } @@ -99,10 +99,10 @@ public class ElementData { //存储当前定位内容文本 StringBuilder value = new StringBuilder(valueList.get(i)); //循环,替换当前定位内容中所有需要替换的词语,直到无词语替换或定位内容不存在需要替换的词语为止 - while(linkWordIter.hasNext() && value.indexOf(AbstractRead.START_SIGN) > -1) { + while(linkWordIter.hasNext() && value.indexOf(AbstractLocation.START_SIGN) > -1) { //存储替换符的开始和结束位置 - int replaceStartIndex = value.indexOf(AbstractRead.START_SIGN); - int replaceEndIndex = value.indexOf(AbstractRead.END_SIGN); + int replaceStartIndex = value.indexOf(AbstractLocation.START_SIGN); + int replaceEndIndex = value.indexOf(AbstractLocation.END_SIGN); //对当前位置的词语进行替换 value.replace(replaceStartIndex, replaceEndIndex + 1, linkWordIter.next()); diff --git a/src/main/java/pres/auxiliary/work/selenium/location/AbstractRead.java b/src/main/java/pres/auxiliary/work/selenium/location/AbstractLocation.java similarity index 64% rename from src/main/java/pres/auxiliary/work/selenium/location/AbstractRead.java rename to src/main/java/pres/auxiliary/work/selenium/location/AbstractLocation.java index 4cb45b5..611a01b 100644 --- a/src/main/java/pres/auxiliary/work/selenium/location/AbstractRead.java +++ b/src/main/java/pres/auxiliary/work/selenium/location/AbstractLocation.java @@ -15,7 +15,7 @@ import pres.auxiliary.work.selenium.element.ElementType; * @version Ver1.0 * */ -public abstract class AbstractRead { +public abstract class AbstractLocation { /** * 定义用于正则的替换符开始标记 */ @@ -71,8 +71,6 @@ public abstract class AbstractRead { protected ElementType toElementType(String value) { //转换元素类型枚举,并返回 switch (value) { - case "0": - return ElementType.COMMON_ELEMENT; case "1": return ElementType.DATA_LIST_ELEMENT; case "2": @@ -81,8 +79,52 @@ public abstract class AbstractRead { return ElementType.SELECT_OPTION_ELEMENT; case "4": return ElementType.IFRAME_ELEMENT; + case "0": default: - return null; + return ElementType.COMMON_ELEMENT; + } + } + + /** + * 该方法用于根据标签的名称,返回相应的定位方式枚举 + * @param labelName 标签名称 + * @return {@link ByType}枚举 + */ + protected ByType toByType(String labelName) { + switch (labelName) { + case "xpath": + return ByType.XPATH; + case "css": + return ByType.CSS; + case "classname": + return ByType.CLASSNAME; + case "id": + return ByType.ID; + case "linktext": + return ByType.LINKTEXT; + case "name": + return ByType.NAME; + case "tagname": + return ByType.TAGNAME; + default: + throw new IllegalArgumentException("不存在的定位方式: " + labelName); + } + } + + /** + * 用于对等待时间进行转换 + * @param text 获取的文本 + * @return 转换后的等待时间 + */ + protected long toWaitTime(String text) { + //获取元素存储等待时间属性值,并转换为long类型 + try { + //将属性值进行转换,若属性值不存在,则赋为-1 + long time = Long.valueOf(text == null ? "-1" : text); + //若转换的时间小于0,则返回-1 + return time < 0 ? -1L : time; + } catch (NumberFormatException e) { + return -1L; } } } diff --git a/src/main/java/pres/auxiliary/work/selenium/location/JsonLocation.java b/src/main/java/pres/auxiliary/work/selenium/location/JsonLocation.java new file mode 100644 index 0000000..95686b4 --- /dev/null +++ b/src/main/java/pres/auxiliary/work/selenium/location/JsonLocation.java @@ -0,0 +1,284 @@ +package pres.auxiliary.work.selenium.location; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; + +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; + +/** + *
文件名:JsonLocation.java
+ *用途: + * 用于读取以json形式的读取到的元素定位信息 + *
+ *编码时间:2020年10月28日上午8:24:56
+ *修改时间:2020年10月28日上午8:24:56
+ * @author 彭宇琦 + * @version Ver1.0 + * + */ +public class JsonLocation extends AbstractLocation { + /** + * 指向json中的模板key值 + */ + protected final String KEY_TEMPLETE = "templet"; + /** + * 指向json中的元素key值 + */ + protected final String KEY_ELEMENT = "element"; + /** + * 指向json中的元素定位方式key值 + */ + protected final String KEY_LOCATION = "location"; + /** + * 指向json中的元素定位模板key值 + */ + protected final String KEY_TEMP = "temp"; + /** + * 指向json中的元素定位模板key值 + */ + protected final String KEY_VALUE = "value"; + /** + * 指向json中的元素定位类型key值 + */ + protected final String KEY_TYPE = "type"; + /** + * 指向json中的元素等待时间key值 + */ + protected final String KEY_WAIT = "wait"; + protected final String KEY_IFRAME = "iframe"; + + /** + * 存储转换后获得到的模板json对象 + */ + JSONObject templateJson; + /** + * 存储转换后获得的元素json对象 + */ + JSONObject elementJson; + + /** + * 通过写在文件中的json文本类对象对类进行构造 + * @param jsonTextFile 存储json的文件类对象 + */ + public JsonLocation(File jsonTextFile) { + //读取json文件中的内容 + StringBuilder jsonText = new StringBuilder(); + try(BufferedReader br = new BufferedReader(new FileReader(jsonTextFile))) { + String text = ""; + while((text = br.readLine()) != null) { + jsonText.append(text); + } + } catch (IOException e) { + throw new IncorrectFileException("json文件异常,文件位置:" + jsonTextFile.getAbsolutePath()); + } + + //转换读取的json内容 + toJsonObject(jsonText.toString()); + } + + /** + * 通过json文本对类进行构造 + * @param json json文本 + */ + public JsonLocation(String jsonText) { + toJsonObject(jsonText); + } + + @Override + public ArrayList文件名:UndefinedElementException.java
*用途:用于查找xml文件中元素不存在时抛出的异常
*编码时间:2019年10月25日上午8:30:20
- *修改时间:2019年10月25日上午8:30:20
+ *修改时间:2020年10月28日上午11:31:51
* @author 彭宇琦 * @version Ver1.0 - * @since JDK 12 + * @since JDK 8 * */ public class UndefinedElementException extends RuntimeException { @@ -32,7 +32,22 @@ public class UndefinedElementException extends RuntimeException { public UndefinedElementException(Throwable cause) { super(cause); } + + public UndefinedElementException(String elemenetName, ExceptionElementType exceptionElementType) { + super("不存在的" + exceptionElementType.name+ "名称:" + elemenetName); + } + private static final long serialVersionUID = 1L; + + public enum ExceptionElementType { + ELEMENT("元素"), + TEMPLET("模板") + ; + String name; + private ExceptionElementType(String name) { + this.name = name; + } + } } diff --git a/src/main/java/pres/auxiliary/work/selenium/location/ReadXml.java b/src/main/java/pres/auxiliary/work/selenium/location/XmlLocation.java similarity index 84% rename from src/main/java/pres/auxiliary/work/selenium/location/ReadXml.java rename to src/main/java/pres/auxiliary/work/selenium/location/XmlLocation.java index 2fd4d34..0bf2eb8 100644 --- a/src/main/java/pres/auxiliary/work/selenium/location/ReadXml.java +++ b/src/main/java/pres/auxiliary/work/selenium/location/XmlLocation.java @@ -11,6 +11,7 @@ import org.dom4j.Element; import org.dom4j.io.SAXReader; import pres.auxiliary.work.selenium.element.ElementType; +import pres.auxiliary.work.selenium.location.UndefinedElementException.ExceptionElementType; /** *@@ -41,7 +42,7 @@ import pres.auxiliary.work.selenium.element.ElementType; * @since JDK 8 * */ -public class ReadXml extends AbstractRead { +public class XmlLocation extends AbstractLocation { /** * 存储构造后的Document类对象,以读取xml文件中的内容 */ @@ -52,7 +53,7 @@ public class ReadXml extends AbstractRead { * @param xmlFile xml文件对象 * @throws IncorrectFileException xml文件有误时抛出的异常 */ - public ReadXml(File xmlFile) { + public XmlLocation(File xmlFile) { //将编译时异常转换为运行时异常 try { dom = new SAXReader().read(xmlFile); @@ -66,7 +67,7 @@ public class ReadXml extends AbstractRead { * 根据xml文件的{@link Document}对象进行构造 * @param dom {@link Document}对象 */ - public ReadXml(Document dom) { + public XmlLocation(Document dom) { this.dom = dom; } @@ -78,7 +79,7 @@ public class ReadXml extends AbstractRead { //遍历元素下所有的定位标签,并将其转换为相应的ByType枚举,存储至byTypeList中 for (Object byElement : element.elements()) { - byTypeList.add(getByType(((Element)byElement).getName())); + byTypeList.add(toByType(((Element)byElement).getName())); } return byTypeList; @@ -101,7 +102,7 @@ public class ReadXml extends AbstractRead { //判断元素是否启用模板,若启用模板,则获取模板内容,并将定位内容进行转换 String tempId = ((Element) byElement).attributeValue("temp_id"); String value = tempId != null ? - getTemplateValue(tempId, getByType(((Element) byElement).getName())) : + getTemplateValue(tempId, toByType(((Element) byElement).getName())) : ((Element)byElement).getText(); valueList.add(replaceValue(((Element) byElement), value)); @@ -164,32 +165,6 @@ public class ReadXml extends AbstractRead { } } - /** - * 该方法用于根据标签的名称,返回相应的定位方式枚举 - * @param labelName 标签名称 - * @return {@link ByType}枚举 - */ - private ByType getByType(String labelName) { - switch (labelName) { - case "xpath": - return ByType.XPATH; - case "css": - return ByType.CSS; - case "classname": - return ByType.CLASSNAME; - case "id": - return ByType.ID; - case "linktext": - return ByType.LINKTEXT; - case "name": - return ByType.NAME; - case "tagname": - return ByType.TAGNAME; - default: - throw new IllegalArgumentException("不存在的定位方式: " + labelName); - } - } - /** * 用于返回元素标签对象 * @param name 元素名称 @@ -204,7 +179,7 @@ public class ReadXml extends AbstractRead { if (element != null) { return element; } else { - throw new UndefinedElementException("不存在的元素名称:" + name); + throw new UndefinedElementException(name, ExceptionElementType.ELEMENT); } } @@ -222,7 +197,7 @@ public class ReadXml extends AbstractRead { if (element != null) { return element.getText(); } else { - throw new UndefinedElementException("不存在的模板:" + selectTempXpath); + throw new UndefinedElementException(tempId, ExceptionElementType.TEMPLET); } } diff --git a/src/test/java/pres/auxiliary/work/selenium/datadriven/TestDataDriven.java b/src/test/java/pres/auxiliary/work/selenium/datadriven/TestDataDriven.java deleted file mode 100644 index df2fa3a..0000000 --- a/src/test/java/pres/auxiliary/work/selenium/datadriven/TestDataDriven.java +++ /dev/null @@ -1,158 +0,0 @@ -package pres.auxiliary.work.selenium.datadriven; - -import java.io.File; - -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import pres.auxiliary.work.selenium.datadriven.DataNotFoundException; - -public class TestDataDriven { - DataDriven dataList = new DataDriven(); - - @After - public void after() { - while(dataList.hasNext()) { - System.out.println("*******************************"); - System.out.println("正在读取第" + (dataList.getNowIndex() + 1) + "行数据:"); - int i = 0; - for (String data : dataList.next()) { - System.out.println("第" + (i + 1) + "列(列名:" + dataList.getReadListName()[i++] + ")数据:" + data); - } - } - System.out.println("*******************************"); - System.out.println("整体数据:"); - System.out.println("[" + dataList.toString() + "]"); - } - - @Before - public void readFile() throws DataNotFoundException { -// dataList.setEmptyCell(true); -// test_04_ReadTxtFile(); -// dataList.addDataFile(new File("src/test/java/pres/auxiliary/selenium/datadriven/Test2.txt"), true); - - /* - for ( int i = 0; i < dataList.getColumnLength(); i++ ) { - dataList.replaceListName(i, ("测试数据第" + (i + 1) + "列")); - } - */ - } - - @Ignore - @Test - public void test_01_ReadCsvFile() throws DataNotFoundException { - dataList.addDataFile(new File("src/test/java/pres/auxiliary/selenium/datadriven/Test.csv"), true); - } - - @Ignore - @Test - public void test_02_ReadXlsxFile() throws DataNotFoundException { - dataList.addDataFile(new File("src/test/java/pres/auxiliary/selenium/datadriven/Test.xlsx"), true); - } - - @Ignore - @Test - public void test_03_ReadXlsFile() throws DataNotFoundException { - dataList.addDataFile(new File("src/test/java/pres/auxiliary/selenium/datadriven/Test.xls"), true); - } - - @Ignore - @Test - public void test_04_ReadTxtFile() throws DataNotFoundException { - dataList.addDataFile(new File("src/test/java/pres/auxiliary/selenium/datadriven/Test.txt"), true); - } - - @Ignore - @Test - public void test_05_setEmptyCell() throws DataNotFoundException { - dataList.setEmptyCell(false); - dataList.addDataFile(new File("src/test/java/pres/auxiliary/selenium/datadriven/Test.xls"), true); - } - - @Ignore - @Test - public void test_06_next() { - int index = 0; - while(dataList.hasNext()) { - System.out.println("*******************************"); - System.out.println("正在读取第" + (index + 1) + "行数据:"); - int i = 0; - for (String data : dataList.next()) { - System.out.println("第" + (i + 1) + "列(列名:" + dataList.getListName(i++) + ")数据:" + data); - } - - index++; - } - } - - @Test - public void test_07_setReadListName() { - dataList.setReadListName("测试1", "测试3"); - } - - @Test - public void test_08_setReadListId() { - dataList.setReadListId(1, 2); - } - - @Test - public void test_09_readFiles() throws DataNotFoundException { - dataList.addDataFile(new File("src/test/java/pres/auxiliary/selenium/datadriven/Test2.txt"), true); - test_01_ReadCsvFile(); - test_02_ReadXlsxFile(); - test_03_ReadXlsFile(); - test_04_ReadTxtFile(); - dataList.setReadListName("测试1", "xls1", "测试4", "csv1", "xlsx1"); - } - - @Test - public void test_10_setNowIndex() { - dataList.setNowIndex(3); - } - - @Test - public void test_11_getRowAndsetReadAllList() { - dataList.setReadListId(1, 3, 5); - int index = 1; - for (String data : dataList.getRow(2)) { - System.out.println("第" + (index++) + "行数据:" + data); - } - dataList.setReadAllList(); - } - - @Test - public void test_12_getColumn() { - int index = 1; - for (String data : dataList.getColumn(3)) { - System.out.println("第" + (index++) + "行数据:" + data); - } - System.out.println("******************************"); - index = 1; - for (String data : dataList.getColumn("测试2")) { - System.out.println("第" + (index++) + "行数据:" + data); - } - dataList.setReadAllList(); - } - - @Test - public void test_13_saveData() throws Exception { - dataList.setEmptyCell(false); - dataList.addDataFile(new File("src/test/java/pres/auxiliary/selenium/datadriven/Test2.txt"), true); - test_01_ReadCsvFile(); - test_02_ReadXlsxFile(); - test_03_ReadXlsFile(); - test_04_ReadTxtFile(); - - test_01_ReadCsvFile(); - test_02_ReadXlsxFile(); - test_03_ReadXlsFile(); - test_04_ReadTxtFile(); - - dataList.saveData(new File("D:\\测试\\test.txt")); - dataList.saveData(new File("D:\\测试\\test.csv")); - dataList.saveData(new File("D:\\测试\\test.xls")); - dataList.saveData(new File("D:\\测试\\test.xlsx")); - } -} diff --git a/src/test/java/pres/auxiliary/work/selenium/element/ElementDataTest.java b/src/test/java/pres/auxiliary/work/selenium/element/ElementDataTest.java index 11e1e09..585382d 100644 --- a/src/test/java/pres/auxiliary/work/selenium/element/ElementDataTest.java +++ b/src/test/java/pres/auxiliary/work/selenium/element/ElementDataTest.java @@ -6,8 +6,8 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import pres.auxiliary.work.selenium.location.AbstractRead; -import pres.auxiliary.work.selenium.location.ReadXml; +import pres.auxiliary.work.selenium.location.AbstractLocation; +import pres.auxiliary.work.selenium.location.XmlLocation; /** *
文件名:ElementDataTest.java
@@ -21,14 +21,14 @@ import pres.auxiliary.work.selenium.location.ReadXml; * */ public class ElementDataTest { - AbstractRead ar; + AbstractLocation ar; ElementData test; final File XML_FILE = new File("src/test/java/pres/auxiliary/work/selenium/element/测试用xml文件.xml"); @BeforeClass public void init() { - ar = new ReadXml(XML_FILE); + ar = new XmlLocation(XML_FILE); } @BeforeMethod diff --git a/src/test/java/pres/auxiliary/work/selenium/location/JsonLocationTest.java b/src/test/java/pres/auxiliary/work/selenium/location/JsonLocationTest.java new file mode 100644 index 0000000..cd9d5aa --- /dev/null +++ b/src/test/java/pres/auxiliary/work/selenium/location/JsonLocationTest.java @@ -0,0 +1,162 @@ +package pres.auxiliary.work.selenium.location; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import com.alibaba.fastjson.JSONObject; + +/** + *文件名:JsonLocationTest.java
+ *用途: + * 对{@link JsonLocation}类进行单元测试 + *
+ *编码时间:2020年10月28日上午7:55:07
+ *修改时间:2020年10月28日上午7:55:07
+ * @author 彭宇琦 + * @version Ver1.0 + * + */ +public class JsonLocationTest { + private final File JSON_FILE = new File("src/test/java/pres/auxiliary/work/selenium/location/测试用json文件.txt"); + /** + * 定义类 + */ + JsonLocation test; + + /** + * 初始化数据 + */ + @BeforeClass + public void init() { + test = new JsonLocation(JSON_FILE); + } + + /** + * 测试读取文件中的json,并测试其是否能被转换为json + */ + @Test + public void readJson() { + StringBuilder jsonText = new StringBuilder(); + try(BufferedReader br = new BufferedReader(new FileReader(JSON_FILE))) { + String text = ""; + while((text = br.readLine()) != null) { + jsonText.append(text); + } + } catch (Exception e) { + } + +// System.out.println(jsonText.toString()); + JSONObject json = JSONObject.parseObject(jsonText.toString()); + System.out.println(json.toJSONString()); + +// System.out.println(new JSONObject().isEmpty()); + System.out.println(json.getJSONArray("test")); + } + + /** + * 用于测试{@link JsonLocation#findElementByTypeList(String)}方法文件名:ReadXmlTest.java
- *用途:用于对{@link ReadXml}类进行测试
+ *用途:用于对{@link XmlLocation}类进行测试
*编码时间:2020年10月8日 下午11:19:40
*修改时间:2020年10月8日 下午11:19:40
* @author 彭宇琦 * @version Ver1.0 * @since JDK 12 */ -public class ReadXmlTest { +public class XmlLocationTest { private final File XML_FILE = new File("src/test/java/pres/auxiliary/work/selenium/location/测试用xml文件.xml"); /** * 定义类 */ - ReadXml rx; + XmlLocation rx; /** * 初始化数据 */ @BeforeClass public void init() { - rx = new ReadXml(XML_FILE); + rx = new XmlLocation(XML_FILE); } /** - * 测试{@link ReadXml#findElementByTypeList(String)}方法 + * 测试{@link XmlLocation#findElementByTypeList(String)}方法 * 预期: * XPATH * CSS @@ -42,7 +42,7 @@ public class ReadXmlTest { } /** - * 测试{@link ReadXml#findElementType(String)}方法 + * 测试{@link XmlLocation#findElementType(String)}方法 * 预期: * COMMON_ELEMENT * COMMON_ELEMENT @@ -62,7 +62,7 @@ public class ReadXmlTest { } /** - * 测试{@link ReadXml#findIframeNameList(String)}方法 + * 测试{@link XmlLocation#findIframeNameList(String)}方法 * 预期: * 窗体1 * 窗体1.1 @@ -74,7 +74,7 @@ public class ReadXmlTest { } /** - * 测试{@link ReadXml#findWaitTime(String)}方法 + * 测试{@link XmlLocation#findWaitTime(String)}方法 * 预期: * -1 * 100 @@ -94,7 +94,7 @@ public class ReadXmlTest { } /** - * 测试{@link ReadXml#findValueList(String)}方法 + * 测试{@link XmlLocation#findValueList(String)}方法 * 预期: * //XXX模板控件1[@X='XX控件11']/div/div[@src='Test']/input * http body div diff --git a/src/test/java/pres/auxiliary/work/selenium/location/测试用json文件.txt b/src/test/java/pres/auxiliary/work/selenium/location/测试用json文件.txt new file mode 100644 index 0000000..0c0d8a1 --- /dev/null +++ b/src/test/java/pres/auxiliary/work/selenium/location/测试用json文件.txt @@ -0,0 +1,261 @@ +{ + "templet":{ + "1":"//XXX模板控件1[@X='${name}']/div/div[@${att}='${id}']/input", + "2":"http body ${tag_name}", + "3":"//XXX模板控件1[@X='${src}']/div[@name='${name}']", + "4":"//XXX模板控件1[@X='${src}']/div[@name='${name}']/div[@is='${str1}' and text()='${str1}']", + "5":"//XXX模板控件1[@X='${src}']/div[@name='${name}']/div[@is='${str1}' and text()='${src}']/span[text()='${str2}']/span[id='${aaaa}']", + "6":"${name}" + }, + "element":{ + "XX控件1":{ + "location":[ + { + "type":"xpath", + "value":"//XX控件1[@X='XXXX']" + } + ], + "type":"0", + "wait":"5" + }, + "窗体1":{ + "location":[ + { + "type":"xpath", + "value":"//窗体1[@X='XXXX']" + }, + { + "temp":"2", + "type":"css", + "tag_name":"iframe" + } + ], + "type":"1", + "wait":"5" + }, + "XX控件2":{ + "location":[ + { + "type":"xpath", + "value":"//XX控件2[@X='XXXX']" + } + ], + "type":"0", + "wait":"5", + "iframe":"窗体1" + }, + "窗体1.1":{ + "location":[ + { + "type":"xpath", + "value":"//窗体1.1[@X='XXXX']" + } + ], + "type":"1", + "iframe":"窗体1" + }, + "XX控件3":{ + "location":[ + { + "type":"xpath", + "value":"//XX控件3[@X='XXXX']" + } + ], + "type":"0", + "wait":"5", + "iframe":"窗体1.1" + }, + "窗体1.1.1":{ + "location":[ + { + "type":"xpath", + "value":"//窗体1.1.1[@X='XXXX']" + } + ], + "type":"1", + "iframe":"窗体1.1" + }, + "XX控件3.1":{ + "location":[ + { + "type":"xpath", + "value":"//XX控件3[@X='XXXX']" + } + ], + "type":"0", + "wait":"5", + "iframe":"窗体1.1.1" + }, + "XX控件3.2":{ + "location":[ + { + "type":"xpath", + "value":"//XX控件3[@X='XXXX']" + } + ], + "type":"0", + "wait":"5", + "iframe":"窗体1.1.8" + }, + "窗体1.2":{ + "location":[ + { + "type":"xpath", + "value":"//窗体1.2[@X='XXXX']" + } + ], + "iframe":"窗体1" + }, + "XX控件4":{ + "location":[ + { + "type":"xpath", + "value":"//XX控件4[@X='XXXX']" + } + ] + }, + "窗体2":{ + "location":[ + { + "type":"xpath", + "value":"//窗体2[@X='XXXX']" + } + ] + }, + "XX控件5":{ + "location":[ + { + "temp":"1", + "type":"xpath", + "id":"Test", + "att":"src" + }, + { + "temp":"2", + "type":"css", + "tag_name":"div", + "att":"src" + }, + { + "temp":"3", + "type":"xpath" + }, + { + "temp":"6", + "type":"id", + "name":"测试控件55555" + } + ], + "iframe":"窗体2" + }, + "窗体3":{ + "location":[ + { + "type":"xpath", + "value":"//窗体3[@X='${ccc}']" + } + ] + }, + "XX控件13":{ + "location":[ + { + "temp":"3", + "type":"xpath" + } + ], + "iframe":"窗体3" + }, + "XX控件14":{ + "location":[ + { + "temp":"4", + "type":"xpath" + } + ], + "iframe":"窗体3" + }, + "XX控件15":{ + "location":[ + { + "temp":"5", + "type":"xpath" + } + ], + "iframe":"窗体3", + "wait":100 + }, + "XX控件16":{ + "location":[ + { + "temp":"1", + "type":"xpath", + "id":"Test", + "att":"src" + } + ], + "iframe":"窗体3", + "type":0, + "wait":1 + }, + "XX控件17":{ + "location":[ + { + "temp":"1", + "type":"xpath" + } + ], + "iframe":"窗体3", + "type":1, + "wait":0 + }, + "XX控件18":{ + "location":[ + { + "temp":"5", + "type":"xpath", + "src":"Test", + "str2":"string" + } + ], + "iframe":"窗体3", + "type":2, + "wait":-11 + }, + "XX控件19":{ + "location":[ + { + "temp":"5", + "type":"xpath", + "src":"Test", + "str2":"string" + } + ], + "iframe":"窗体3", + "type":"sadasd", + }, + "XX控件21":{ + "location":[ + { + "temp":"5", + "type":"xpath", + "src":"Test", + "str2":"string" + } + ], + "iframe":"窗体3", + "type":2000, + "wait":"" + }, + "XX控件20":{ + "location":[ + { + "temp":"测试", + "type":"xpath" + } + ], + "iframe":"窗体3", + "type":3, + "wait":"dasd" + } + } +} \ No newline at end of file diff --git a/src/test/java/pres/auxiliary/work/selenium/xml/ReadXmlTest.java b/src/test/java/pres/auxiliary/work/selenium/xml/ReadXmlTest.java index 9efe833..510e431 100644 --- a/src/test/java/pres/auxiliary/work/selenium/xml/ReadXmlTest.java +++ b/src/test/java/pres/auxiliary/work/selenium/xml/ReadXmlTest.java @@ -9,14 +9,14 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class ReadXmlTest { -ReadXml r; +XmlLocation r; /** * 初始化数据 */ @BeforeClass public void newReadXML() { - r = new ReadXml(new File("src/test/java/pres/auxiliary/work/selenium/xml/测试用xml文件.xml")); + r = new XmlLocation(new File("src/test/java/pres/auxiliary/work/selenium/xml/测试用xml文件.xml")); } @AfterMethod @@ -25,7 +25,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getBy(String, ByType)}方法,获取普通元素 + * 用于测试{@link XmlLocation#getBy(String, ByType)}方法,获取普通元素 */ @Test public void getByTest_Element() { @@ -33,7 +33,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getBy(String, ByType)}方法,获取窗体元素 + * 用于测试{@link XmlLocation#getBy(String, ByType)}方法,获取窗体元素 */ @Test public void getByTest_Iframe() { @@ -41,7 +41,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getBy(String, ByType)}方法,获取模板元素 + * 用于测试{@link XmlLocation#getBy(String, ByType)}方法,获取模板元素 */ @Test public void getByTest_Templet() { @@ -50,7 +50,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getBy(String, ByType)}方法,获取顶层元素 + * 用于测试{@link XmlLocation#getBy(String, ByType)}方法,获取顶层元素 */ @Test public void getByTest_RootElement() { @@ -58,7 +58,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getValue(String, ByType)}方法,获取普通元素 + * 用于测试{@link XmlLocation#getValue(String, ByType)}方法,获取普通元素 */ @Test public void getElementValueTest_Element() { @@ -66,7 +66,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getValue(String, ByType)}方法,获取窗体元素 + * 用于测试{@link XmlLocation#getValue(String, ByType)}方法,获取窗体元素 */ @Test public void getElementValueTest_Iframe() { @@ -74,7 +74,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getValue(String, ByType)}方法,获取模板元素 + * 用于测试{@link XmlLocation#getValue(String, ByType)}方法,获取模板元素 */ @Test public void getElementValueTest_Templet() { @@ -83,7 +83,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getValue(String, ByType)}方法,获取顶层元素 + * 用于测试{@link XmlLocation#getValue(String, ByType)}方法,获取顶层元素 */ @Test public void getElementValueTest_RootElement() { @@ -91,7 +91,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getIframeName(String, ByType)}方法,获取普通元素 + * 用于测试{@link XmlLocation#getIframeName(String, ByType)}方法,获取普通元素 */ @Test public void getIframeNameTest_Element() { @@ -99,7 +99,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getIframeName(String, ByType)}方法,获取窗体元素 + * 用于测试{@link XmlLocation#getIframeName(String, ByType)}方法,获取窗体元素 */ @Test public void getIframeNameTest_Iframe() { @@ -107,7 +107,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getIframeName(String, ByType)}方法,获取模板元素 + * 用于测试{@link XmlLocation#getIframeName(String, ByType)}方法,获取模板元素 */ @Test public void getIframeNameTest_Templet() { @@ -116,7 +116,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getIframeName(String)}方法,获取顶层元素 + * 用于测试{@link XmlLocation#getIframeName(String)}方法,获取顶层元素 */ @Test public void getIframeNameTest_RootElement() { @@ -124,7 +124,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getIframeName(String)}方法,获取模板元素 + * 用于测试{@link XmlLocation#getIframeName(String)}方法,获取模板元素 */ @Test public void getIframeNameTest_NoPram() { @@ -132,7 +132,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getValue(String, ByType)}方法,未查找到替换的属性 + * 用于测试{@link XmlLocation#getValue(String, ByType)}方法,未查找到替换的属性 */ @Test public void getValueTest_NoPram() { @@ -141,7 +141,7 @@ ReadXml r; } /** - * 用于测试{@link ReadXml#getValue(String, ByType)}方法,外链关键词 + * 用于测试{@link XmlLocation#getValue(String, ByType)}方法,外链关键词 */ @Test public void getValueTest_Link() {