diff --git a/Test/ReadXml/测试文件.xml b/Test/ReadXml/测试文件.xml new file mode 100644 index 0000000..d99f92e --- /dev/null +++ b/Test/ReadXml/测试文件.xml @@ -0,0 +1,19 @@ + + + + //iframe[contains(@src,'${src}')] + //li[text()='${mon}'] + + + + + + + + \ No newline at end of file diff --git a/src/main/java/pres/auxiliary/tool/randomstring/MobleNumberType.java b/src/main/java/pres/auxiliary/tool/randomstring/MobleNumberType.java new file mode 100644 index 0000000..ee6a9ca --- /dev/null +++ b/src/main/java/pres/auxiliary/tool/randomstring/MobleNumberType.java @@ -0,0 +1,43 @@ +package pres.auxiliary.tool.randomstring; + +import java.util.ArrayList; +import java.util.Arrays; + +public enum MobleNumberType { + /** + * 中国电信运营商号码段 + */ + CHINA_TELECOM(133, 1349, 149, 153, 162, 1700, 1701, 1702, 173, 17400, 17401, 17402, 17403, 17404, 17405, 177, 180, 181, 189, 191, 193, 199), + /** + * 中国移动运营商号码段 + */ + CHINA_MOBILE(1340, 1341, 1342, 1343, 1344, 1345, 1346, 135, 136, 137, 138, 139, 147, 148, 150, 151, 152, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577, 1578, 1579, 158, 159, 165, 1703, 1705, 1706, 172, 178, 182, 183, 184, 187, 188, 198), + /** + * 中国联通运营商号码段 + */ + CHINA_UNICOM(130, 131, 132, 145, 146, 155, 156, 166, 167, 1704, 1707, 1708, 1709, 171, 175, 176, 185, 186); + + /** + * 用于表示当前运营商号码段正则表达式 + */ + ArrayList numberList = new ArrayList<>(); + + /** + * 用于初始化数据 + * @param nums 号码段 + */ + private MobleNumberType(int...nums) { + //添加数据 + Arrays.stream(nums).forEach(num -> { + numberList.add(String.valueOf(num)); + }); + } + + /** + * 用于返回运营商号码段 + * @return 号码段 + */ + public ArrayList getRegex() { + return numberList; + } +} diff --git a/src/main/java/pres/auxiliary/tool/randomstring/PresetString.java b/src/main/java/pres/auxiliary/tool/randomstring/PresetString.java index 3f33f7c..4807f28 100644 --- a/src/main/java/pres/auxiliary/tool/randomstring/PresetString.java +++ b/src/main/java/pres/auxiliary/tool/randomstring/PresetString.java @@ -110,33 +110,47 @@ public class PresetString { * @return 生成的身份证号码 */ public static String IdentityCard() { - String s = "45030119"; - - rs.clear(); - rs.addMode(StringMode.NUM); - s += rs.toString(2); - - Random r = new Random(); - - // 生成月 - int i = (1 + r.nextInt(11)); - // 判断生成的随机字符串是否小于10,小于10则向s中补全一个0 - if (i < 10) { - s += "0"; + StringBuilder sb = new StringBuilder(); + RandomString rs = new RandomString("123456789"); + + //省份 + int[] province = {13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91}; + //城市 + String[] city = {"01", "02", "03"}; + //区县 + String[] county = {"01", "02", "03", "04", "05"}; + //生日年份前两位 + int[] year = {19, 18}; + + //加权数 + int[] factors = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; + //校验位 + String[] parity = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"}; + + //随机添加一个省份 + sb.append(province[new Random().nextInt(province.length)]); + //随机添加一个城市 + sb.append(city[new Random().nextInt(city.length)]); + //随机添加一个区县 + sb.append(county[new Random().nextInt(county.length)]); + //随机生日年份 + sb.append(year[new Random().nextInt(year.length)]).append(rs.toString(2)); + //随机生日月份及日子 + sb.append("0" + rs.toString(1)).append("0" + rs.toString(1)); + //随机三位数 + sb.append(rs.toString(3)); + + //计算加权数 + int sum = 0; + for (int i = 0; i < 17; i++) { + int code = Integer.valueOf(sb.substring(i, i + 1)); + int factor = Integer.valueOf(factors[i]); + sum += (code * factor); } - s += i; - - // 生成日 - i = (1 + r.nextInt(27)); - // 判断生成的随机字符串是否小于10,小于10则向s中补全一个0 - if (i < 10) { - s += "0"; - } - s += i; - - s += rs.toString(4); - - return s; + //根据加权数添加校验位 + sb.append(parity[(sum % 11)]); + + return sb.toString(); } /** @@ -176,6 +190,9 @@ public class PresetString { return s; } + } + + private static String getMobleNumber(MobleNumberType mobleNumberType) { } diff --git a/src/main/java/pres/auxiliary/tool/randomstring/RandomWord.java b/src/main/java/pres/auxiliary/tool/randomstring/RandomWord.java new file mode 100644 index 0000000..9573607 --- /dev/null +++ b/src/main/java/pres/auxiliary/tool/randomstring/RandomWord.java @@ -0,0 +1,127 @@ +package pres.auxiliary.tool.randomstring; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Random; + +public class RandomWord { + /** + * 用于存储需要随机返回的词语 + */ + private ArrayList returnWordList = new ArrayList<>(); + /** + * 用于存储默认返回的词语 + */ + private ArrayList conditionWordList = new ArrayList<>(); + + /** + * 指向生成的词语是否能重复 + */ + private boolean isRepeat = true; + + /** + * 用于向无需条件亦可直接返回的词语组中添加词语 + * @param words 词语组 + */ + public void addRetuenWord(String...words) { + returnWordList.addAll(Arrays.asList(words)); + } + + /** + * 用于向无需条件亦可直接返回的词语组中添加词语 + * @param wordLst 词语集合 + */ + public void addRetuenWord(ArrayList wordLst) { + conditionWordList.addAll(wordLst); + } + + /** + * 用于向符合条件后返回的词语组中添加词语 + * @param words 词语组 + */ + public void addConditionWord(String...words) { + conditionWordList.addAll(Arrays.asList(words)); + } + + /** + * 用于向符合条件后返回的词语组中添加词语 + * @param wordLst 词语集合 + */ + public void addConditionWord(ArrayList wordLst) { + returnWordList.addAll(wordLst); + } + + /** + * 用于设置词语是否可以重复生成 + * @param isRepeat 词语是否可重复 + */ + public void setRepeat(boolean isRepeat) { + this.isRepeat = isRepeat; + } + + /** + * 用于随机生成指定随机个数的词语 + * @param minLength 最少词语个数 + * @param maxLength 最多词语个数 + * @return 生成的词语集合 + */ + public ArrayList toWord(int minLength, int maxLength) { + return toWord(getLength(minLength, maxLength)); + } + + /** + * 用于随机生成指定个数的词语 + * @param length 输出的词语个数 + * @return 生成的词语集合 + */ + public ArrayList toWord(int length) { + //克隆returnWordList,便于对不能生成重复词语的情况下,移除集合中的元素,达到快速返回的目的 + @SuppressWarnings("unchecked") + ArrayList wordList = (ArrayList) returnWordList.clone(); + + //存储生成的词语 + ArrayList randomWordList = new ArrayList<>(); + //循环,生成相应的词语 + for (int i = 0; i < length; i++) { + //获取随机下标 + int index = getRandomIndex(wordList); + //存储下标对应的词语 + randomWordList.add(wordList.get(index)); + //若生成的词语不可重复,则直接移除wordList中对应的词语 + if (!isRepeat) { + wordList.remove(index); + } + } + + return wordList; + } + + /** + * 用于根据词语是否允许重复,返回实际的最大生成词语个数 + * @param maxLength 设置的最大词语生成个数 + * @return 实际最大词语生成个数 + */ + private int getLength(int minLength, int maxLength) { + //若词语不允许重复生成且maxLength大于returnWordList.size(),则将maxLength改为maxLength + maxLength = (isRepeat && maxLength > returnWordList.size()) ? returnWordList.size() : maxLength; + //为避免最小长度与最大长度传参相反,故此处进行一个调换 + int max = Math.max(minLength, maxLength); + int min = Math.min(minLength, maxLength); + + //返回指定长度下的一个随机长度 + return (new Random().nextInt(max - min + 1) + min); + } + + /** + * 根据指定的集合,返回随机的一个下标 + * @param wordList 词语集合 + * @return 集合元素的随机下标 + */ + private int getRandomIndex(ArrayList wordList) { + //根据条件获取集合的长度 + int maxLength = wordList.size(); + //生成最大长度范围内的随机数 + return new Random().nextInt(maxLength); + + } +} diff --git a/src/main/java/pres/auxiliary/work/selenium/datadriven/ListFileRead.java b/src/main/java/pres/auxiliary/work/selenium/datadriven/ListFileRead.java index 7302e7a..8b5abbd 100644 --- a/src/main/java/pres/auxiliary/work/selenium/datadriven/ListFileRead.java +++ b/src/main/java/pres/auxiliary/work/selenium/datadriven/ListFileRead.java @@ -137,9 +137,9 @@ public class ListFileRead { columnIndex = columnIndex < 0 ? 0 : columnIndex; //若传值大于最大maxRowNum时,则直接赋予maxRowNum - startRowIndex = (startRowIndex >= maxRowNum) ? maxRowNum - 1 : startRowIndex; + startRowIndex = (startRowIndex >= maxRowNum) ? maxRowNum : startRowIndex; startRowIndex = startRowIndex < 0 ? 0 : startRowIndex; - endRowIndex = (endRowIndex >= maxRowNum) ? maxRowNum - 1 : endRowIndex; + endRowIndex = (endRowIndex >= maxRowNum) ? maxRowNum : endRowIndex; endRowIndex = endRowIndex < 0 ? 0 : endRowIndex; //若两个传值相同,则endRowIndex+1 @@ -185,9 +185,9 @@ public class ListFileRead { endColumnIndex = endColumnIndex < 0 ? 0 : endColumnIndex; //若传值大于最大maxRowNum时,则直接赋予maxRowNum,小于0,则直接赋予0 - startRowIndex = (startRowIndex >= maxRowNum) ? maxRowNum - 1 : startRowIndex; + startRowIndex = (startRowIndex >= maxRowNum) ? maxRowNum : startRowIndex; startRowIndex = startRowIndex < 0 ? 0 : startRowIndex; - endRowIndex = (endRowIndex >= maxRowNum) ? maxRowNum - 1 : endRowIndex; + endRowIndex = (endRowIndex >= maxRowNum) ? maxRowNum : endRowIndex; endRowIndex = endRowIndex < 0 ? 0 : endRowIndex; //若两个传值相同,则endXXXIndex+1 diff --git a/src/main/java/pres/auxiliary/work/selenium/datadriven/NumberDataDriven.java b/src/main/java/pres/auxiliary/work/selenium/datadriven/NumberDataDriven.java deleted file mode 100644 index a1b3317..0000000 --- a/src/main/java/pres/auxiliary/work/selenium/datadriven/NumberDataDriven.java +++ /dev/null @@ -1,169 +0,0 @@ -package pres.auxiliary.work.selenium.datadriven; - -public class NumberDataDriven extends DataDriven { -// /** -// * 无参构造,不做任何操作,请使用{@link #setDataFile(File)}方法定义数据驱动文件 -// */ -// public NumberDataDriven() { -// super(); -// } -// -// /** -// * 用于指定数据驱动文件 -// * -// * @param dataFile -// * 数据驱动文件对象 -// */ -// public NumberDataDriven(File dataFile) { -// super(dataFile); -// } -// -// @Override -// public File createDataFile(String saveFileName, int minSize, int maxSize, int length) throws IOException { -// //用于存储生成的字符 -// StringBuilder sb = new StringBuilder(""); -// // 循环,向文件中添加随机数字 -// for (int i = 0; i < length; i++) { -// // 添加随机数字 -// sb.append(new RandomString(StringMode.NUM).toString(minSize, maxSize)); -// sb.append("\r\n"); -// } -// //删除最后一个换行符 -// sb.delete(sb.lastIndexOf("\r\n"), sb.lastIndexOf("\r\n") + "\r\n".length()); -// -// // 返回数据驱动文件 -// return createDataFile(saveFileName, sb); -// } -// -// @Override -// public File createDataFile(String saveFileName, int size, int length) throws IOException { -// //用于存储生成的字符 -// StringBuilder sb = new StringBuilder(""); -// // 循环,向文件中添加随机数字 -// for (int i = 0; i < length; i++) { -// // 添加随机数字 -// sb.append(new RandomString(StringMode.NUM).toString(size)); -// sb.append("\r\n"); -// } -// //删除最后一个换行符 -// sb.delete(sb.lastIndexOf("\r\n"), sb.lastIndexOf("\r\n") + "\r\n".length()); -// -// // 返回数据驱动文件 -// return createDataFile(saveFileName, sb); -// } -// -// /** -// * 该方法用于从数字1开始,每次增加1步长来生成自然顺序的数字,并写入文件中 -// * -// * @param saveFileName -// * 数据驱动文件的文件名,若为空或为null,则默认为NewDataDriven(文件的后缀为.txt) -// * @param length -// * 数据生成个数 -// * @return 生成的数据驱动文件对象 -// * @throws IOException -// * -// * @see #createDataFile(String, int, int) -// * @see #createDataFile(String, int, int, int) -// */ -// public File createContinuesNumberDataFile(String saveFileName, int length) throws IOException { -// //用于存储生成的字符 -// StringBuilder sb = new StringBuilder(""); -// // 循环,向文件中添加随机数字 -// for (int i = 1; i < length + 1; i++) { -// // 添加随机数字 -// sb.append("" + i); -// sb.append("\r\n"); -// } -// //删除最后一个换行符 -// sb.delete(sb.lastIndexOf("\r\n"), sb.lastIndexOf("\r\n") + "\r\n".length()); -// -// // 返回数据驱动文件 -// return createDataFile(saveFileName, sb); -// } -// -// /** -// * 该方法用于根据起始数字,通过增加一定的步长来生成自然顺序的数字,并写入文件中 -// * -// * @param saveFileName -// * 数据驱动的文件名 -// * @param startNum -// * 起始数字 -// * @param step -// * 步长 -// * @param length -// * 生成数字的个数 -// * @return 数据驱动文件对象 -// * @throws IOException -// */ -// public File createContinuesNumberDataFile(String saveFileName, int startNum, int step, int length) -// throws IOException { -// //用于存储生成的字符 -// StringBuilder sb = new StringBuilder(""); -// // 循环,向文件中添加随机数字 -// for (int i = 0; i < length; i++) { -// // 添加随机数字 -// sb.append("" + startNum); -// sb.append("\r\n"); -// -// // 数字增加一步 -// startNum += step; -// } -// //删除最后一个换行符 -// sb.delete(sb.lastIndexOf("\r\n"), sb.lastIndexOf("\r\n") + "\r\n".length()); -// -// // 返回数据驱动文件 -// return createDataFile(saveFileName, sb); -// } -// -// /** -// * 该方法用于向文件中添加身份证信息 -// * -// * @param saveFileName -// * 数据驱动的文件名 -// * @param length -// * 生成身份证的个数 -// * @return 数据驱动文件对象 -// * @throws IOException -// */ -// public File createIDCardDataFile(String saveFileName, int length) throws IOException { -// //用于存储生成的字符 -// StringBuilder sb = new StringBuilder(""); -// // 循环,向文件中添加随机数字 -// for (int i = 0; i < length; i++) { -// // 添加随机数字 -// sb.append(PresetString.IdentityCard()); -// sb.append("\r\n"); -// } -// //删除最后一个换行符 -// sb.delete(sb.lastIndexOf("\r\n"), sb.lastIndexOf("\r\n") + "\r\n".length()); -// -// // 返回数据驱动文件 -// return createDataFile(saveFileName, sb); -// } -// -// /** -// * 该方法用于向文件中添加手机号码信息 -// * -// * @param saveFileName -// * 数据驱动的文件名 -// * @param length -// * 生成手机号码的个数 -// * @return 数据驱动文件对象 -// * @throws IOException -// */ -// public File createPhoneDataFile(String saveFileName, int length) throws IOException { -// //用于存储生成的字符 -// StringBuilder sb = new StringBuilder(""); -// // 循环,向文件中添加随机数字 -// for (int i = 0; i < length; i++) { -// // 添加随机数字 -// sb.append("139" + new RandomString(StringMode.NUM).toString(8)); -// sb.append("\r\n"); -// } -// //删除最后一个换行符 -// sb.delete(sb.lastIndexOf("\r\n"), sb.lastIndexOf("\r\n") + "\r\n".length()); -// -// // 返回数据驱动文件 -// return createDataFile(saveFileName, sb); -// } -} diff --git a/src/main/java/pres/auxiliary/work/selenium/datadriven/StringDataDriven.java b/src/main/java/pres/auxiliary/work/selenium/datadriven/StringDataDriven.java deleted file mode 100644 index a769416..0000000 --- a/src/main/java/pres/auxiliary/work/selenium/datadriven/StringDataDriven.java +++ /dev/null @@ -1,41 +0,0 @@ -package pres.auxiliary.work.selenium.datadriven; - -public class StringDataDriven extends DataDriven { - - /* - @Override - public File createDataFile(String saveFileName, int minSize, int maxSize, int length) throws IOException, DataNotFoundException { - // 用于存储生成的字符 - StringBuilder sb = new StringBuilder(""); - // 循环,向文件中添加随机数字 - for (int i = 0; i < length; i++) { - // 添加随机字母 - sb.append(new RandomString(StringMode.LOW, StringMode.CAP).toString(minSize, maxSize)); - sb.append("\r\n"); - } - // 删除最后一个换行符 - sb.delete(sb.lastIndexOf("\r\n"), sb.lastIndexOf("\r\n") + "\r\n".length()); - - // 返回数据驱动文件 - return createDataFile(saveFileName, sb); - } - - @Override - public File createDataFile(String saveFileName, int size, int length) throws IOException { - // 用于存储生成的字符 - StringBuilder sb = new StringBuilder(""); - // 循环,向文件中添加随机数字 - for (int i = 0; i < length; i++) { - // 添加随机数字 - sb.append(new RandomString(StringMode.LOW, StringMode.CAP).toString(size)); - sb.append("\r\n"); - } - // 删除最后一个换行符 - sb.delete(sb.lastIndexOf("\r\n"), sb.lastIndexOf("\r\n") + "\r\n".length()); - - // 返回数据驱动文件 - return createDataFile(saveFileName, sb); - } - */ - -} diff --git a/src/main/java/pres/auxiliary/work/selenium/datadriven/TestNGDataDriver.java b/src/main/java/pres/auxiliary/work/selenium/datadriven/TestNGDataDriver.java index 1664e94..48c1355 100644 --- a/src/main/java/pres/auxiliary/work/selenium/datadriven/TestNGDataDriver.java +++ b/src/main/java/pres/auxiliary/work/selenium/datadriven/TestNGDataDriver.java @@ -55,7 +55,8 @@ public class TestNGDataDriver { /** * 用于存储读取到的数据 */ - private ArrayList dataList = new ArrayList<>(); +// private ArrayList dataList = new ArrayList<>(); + private HashMap> dataMap = new HashMap<>(16); /** * 用于存储自定义的公式 @@ -67,6 +68,11 @@ public class TestNGDataDriver { */ private Time time = new Time(); + /** + * 用于存储最大列的元素个数 + */ + private int maxColumnSize = -1; + /** * 构造类,并将默认的数据驱动函数({@link Functions})加载至类中 */ @@ -124,11 +130,14 @@ public class TestNGDataDriver { public void addDataDriver(File dataFile, String pattern, boolean isFirstTitle) throws IOException { //读取文件内的数据 ListFileRead data = new ListFileRead(dataFile, pattern); - //由于数据按照列的形式读取,故此处将数据转置 - data.tableTransposition(); + //获取当前标题行的总数 + int titleIndex = titleList.size(); - //若设定首行为标题行,则根据首行元素读取标题 + //若设定首行为标题行,则根据首行元素读取标题;若首行非标题行,则以列数进行命名 if (isFirstTitle) { + //将读取到的数据转置,得到以行的形式存储数据 + data.tableTransposition(); + //此时,返回data中第一列元素即为标题行的元素 //定位到首行 List dataTitleList = data.getColumn(0); @@ -148,22 +157,44 @@ public class TestNGDataDriver { titleList.add(title); } } + //存储标题后再重新转置,使data以列的形式进行返回 + data.tableTransposition(); + } else { + //若首行不是标题行,则获取data的最大列,按照“元素列 + 行号”的形式进行命名 + for (int index = 0; index < data.getMaxColumnNumber(); index++) { + titleList.add("元素列" + (titleIndex + index)); + } } //遍历所有行,读取所有的数据 - for (int rowIndex = isFirstTitle ? 1 : 0; rowIndex < data.getMaxColumnNumber(); rowIndex++) { - addFileRowData(isFirstTitle ? rowIndex - 1 : rowIndex, data.getColumn(rowIndex)); + for (int columnIndex = 0; columnIndex < data.getMaxColumnNumber(); columnIndex++) { + //将List转换为List + List objectList = new ArrayList<>(); + objectList.addAll(data.getColumn(columnIndex, isFirstTitle ? 1 : 0, data.getCoulumnSize(columnIndex))); + System.out.println(objectList); + //存储数据 + addData(titleList.get(columnIndex + titleIndex), objectList); } } - public void addData(String title, Object...objects) { - //根据列名是否存在,调用相应的添加元素的方法 - if (titleList.indexOf(title) > -1) { - addOriginalColumn(titleList.indexOf(title), objects); - } else { - titleList.add(title); - addNewColumn(objects); + /** + * 用于在非文件中添加数据驱动 + * @param title 数据列标题 + * @param objects 相应的元素 + */ + public void addDataDriver(String title, Object...objects) { + //判断title是否为空,若为空,则将其改为默认命名 + if (title == null || title.isEmpty()) { + title = "数据" + (1 + columnSize()); } + + //判断列表是否存在,若不存在,则重新添加标题 + if (titleList.indexOf(title) < 0) { + titleList.add(title); + } + + //添加数据 + addData(title, Arrays.asList(objects)); } /** @@ -179,13 +210,28 @@ public class TestNGDataDriver { * 一个{@link Data}类对象,通过该对象,对获取到的所有数据进行返回,以简化在编写脚本时 * 的传参 * - * @return TestNG数据驱动 + * @return TestNG形式的数据驱动 */ public Object[][] getDataDriver() { - Object[][] objs = new Object[dataList.size()][1]; + //构造Object[][] + Object[][] objs = new Object[maxColumnSize][1]; - for (int i = 0; i < dataList.size(); i++) { - objs[i][0] = dataList.get(i); + //遍历所有的行,按照最大行添加数据 + for (int i = 0; i < maxColumnSize; i++) { + //由于在lambda中不能直接引用变量i,故需要编写一个封装类 + AtomicInteger index = new AtomicInteger(i); + //遍历标题列表,按照标题列表的顺序,写入到data中 + Data data = new Data(); + titleList.forEach(title -> { + //由于列表长度不一致,导致存储时超出数组的元素无法读取,若抛出异常,则存储空串 + try { + data.addData(dataMap.get(title).get(index.get())); + } catch (Exception e) { + data.addData(""); + } + }); + //存储数据 + objs[i][0] = data; } return objs; @@ -196,117 +242,51 @@ public class TestNGDataDriver { * @return 数据驱动的总行数 */ public int getRowSize() { - return dataList.size(); + //最大列的元素个数即为最大的行数 + return maxColumnSize; } /** - * 用于根据列名称返回指定列的元素个数 + * 用于根据列名称返回指定列的元素个数,若标题不存在,则返回0 * @param title 列名称 * @return 列元素个数 */ public int getColumnSize(String title) { - int index = titleList.indexOf(title); - if (index > -1) { - return getColumnSize(index); + if (dataMap.containsKey(title)) { + return dataMap.get(title).size(); } else { return 0; } } /** - * 用于根据列下标返回指定列的元素个数 + * 用于根据列下标返回指定列的元素个数,若输入的参数不存在,则返回0 * @param index 列下标 * @return 列元素个数 */ public int getColumnSize(int index) { - AtomicInteger rowIndex = new AtomicInteger(0); - //遍历所有行的指定列元素 - dataList.forEach(data -> { - //判断当前元素是否为空,若为空,则结束循环,否则,其rowIndex+1 - if (data.getOriginalString(index).isEmpty()) { - return; - } else { - rowIndex.addAndGet(1); - } - }); - - return rowIndex.get(); + try { + return getColumnSize(titleList.get(index)); + } catch (Exception e) { + return 0; + } } /** - * 用于将一行的数据转为Data类对象 + * 将一行数据添加至相应的dataMap中 * @param rowDataList 一行数据 - * @return 返回一行数据的Data类对象 */ - private void addFileRowData(int rowInde, List rowDataList) { - //将所有的空元素替换为“${ }”,以表示该行存在元素,只是为空 - rowDataList.replaceAll(text -> { - return text.isEmpty() ? "${ }" : text; - }); - - //当rowInde大于dataList的总列数时,则表示未对该行进行存储,此时存在两种情况: - //1.第一次文件,此时dataList并无数据,调用方法时为初次添加数据,则按照正常方式添加 - //2.非第一次读取文件,此时dataList已存在上次读取的数据,当写入的数据行数大于存储的行数 - //时,直接添加数据会造成数据向左移,则需要读取原始列表的内容,将原来没有的数据补全后再 - //添加数据 - if (rowInde > dataList.size() - 1) { - Data data = new Data(); - //判断rowDataList元素与上一行元素个数一致,若一致,则表示当前为第一次读取 - if (dataList.size() != 0 && dataList.get(rowInde - 1).dataList.size() != rowDataList.size()) { - //若上一行的元素个数与rowDataList的元素个数不一致,则此时是非第一次读取文件,rowDataList存在数据,则 - //将rowDataList之前的列都置为空串 - int oldListSize = dataList.get(rowInde - 1).dataList.size(); - for (int index = 0; index < oldListSize - rowDataList.size(); index++) { - data.addData("${ }"); - } - } - - //存储数据 - rowDataList.forEach(data :: addData); - dataList.add(data); - } else { - rowDataList.forEach(dataList.get(rowInde) :: addData); + private void addData(String title, List columnDataList) { + //判断标题是否存在,若不存在,则进行构造 + if (!dataMap.containsKey(title)) { + dataMap.put(title, new ArrayList()); } - } - - /** - * 存储新的列元素 - * @param objects 元素组 - */ - private void addNewColumn(Object...objects) { - //获取当前数据驱动中的行数 - int maxRowSize = getRowSize(); - //获取当前行元素与需要添加的元素个数之间的差值 - int diff = objects.length - maxRowSize; + //对元素进行存储 + dataMap.get(title).addAll(columnDataList); - //为保证数据统一,按照当前元素最大行进行存储,若objects中的元素个数不足时,使用空函数对其占位,保证列表的统一 - for (int index = 0; index < maxRowSize; index++) { - try { - dataList.get(index).addData(objects[index].toString()); - }catch (Exception e) { - dataList.get(index).addData("${ }"); - } - } - - //判断差值是否大于0,若大于0,则按照已存在的行进行存储 - if (diff > 0) { - //存储剩余的元素 - Object[] obj = new Object[diff]; - for (int index = 0; index < diff; index++) { - obj[index] = objects[maxRowSize + index]; - } - - addOriginalColumn(titleList.size() - 1, obj); - } - } - - /** - * 在已存在的列中添加元素 - * @param index 列下标 - * @param objects 元素组 - */ - private void addOriginalColumn(int index, Object...objects) { - if () + //判断该列的元素个数是否大于最大列元素个数,若大于,则将其个数替换为最大元素个数 + int size = dataMap.get(title).size(); + maxColumnSize = size > maxColumnSize ? size : maxColumnSize; } /** @@ -339,7 +319,7 @@ public class TestNGDataDriver { /** * 用于存储数据 */ - private ArrayList dataList = new ArrayList<>(); + private ArrayList dataList = new ArrayList<>(); /** * 用于根据列名称,以字符串的形式返回数据 @@ -357,7 +337,7 @@ public class TestNGDataDriver { * @return 字符串形式的数据 */ public String getString(int index) { - return index < dataList.size() ? disposeContent(dataList.get(index)) : ""; + return index < dataList.size() ? disposeContent(dataList.get(index).toString()) : ""; // return index < dataList.size() ? dataList.get(index) : ""; //调试使用,输出存储的原始内容 } @@ -475,11 +455,29 @@ public class TestNGDataDriver { return new Time(getString(listName)); } + /** + * 用于根据列名称,以{@link Object}的形式对数据进行返回 + * @param listName 列表名称 + * @return {@link Object}类型的数据 + */ + public Object getObject(String listName) { + return dataList.get(titleList.indexOf(listName)); + } + + /** + * 用于根据列名称,以{@link Object}的形式对数据进行返回 + * @param listName 列表名称 + * @return {@link Object}类型的数据 + */ + public Object getObject(int index) { + return dataList.get(index); + } + /** * 用于添加元素 * @param content 元素内容 */ - private void addData(String content) { + private void addData(Object content) { dataList.add(content); } @@ -489,6 +487,7 @@ public class TestNGDataDriver { * @return 处理后的内容 */ private String disposeContent(String content) { + //TODO 想办法在处理时输出Object类 //判断传入的内容是否包含公式起始符号,若不包含,则返回原串 if (content.indexOf(FUNCTION_SIGN_START) < 0) { return content; @@ -543,14 +542,5 @@ public class TestNGDataDriver { //若内容不与任何正则匹配,则返回原始内容 return null; } - - /** - * 用于返回index对应列中存储的元素的原始内容,若列不存在,或无元素,则返回空 - * @param index 列下标 - * @return 列中的内容 - */ - private String getOriginalString(int index) { - return index < dataList.size() ? dataList.get(index) : ""; - } } } diff --git a/src/test/java/pres/auxiliary/work/selenium/datadriven/ListFileReadTest.java b/src/test/java/pres/auxiliary/work/selenium/datadriven/ListFileReadTest.java index c8b8474..cca2e9d 100644 --- a/src/test/java/pres/auxiliary/work/selenium/datadriven/ListFileReadTest.java +++ b/src/test/java/pres/auxiliary/work/selenium/datadriven/ListFileReadTest.java @@ -1,4 +1,4 @@ -package pres.auxiliary.tool.readfile; +package pres.auxiliary.work.selenium.datadriven; import java.io.File; import java.io.IOException; diff --git a/src/test/java/pres/auxiliary/work/selenium/datadriven/TestNGDataDriverTest.java b/src/test/java/pres/auxiliary/work/selenium/datadriven/TestNGDataDriverTest.java index 5977f7a..7c6d85c 100644 --- a/src/test/java/pres/auxiliary/work/selenium/datadriven/TestNGDataDriverTest.java +++ b/src/test/java/pres/auxiliary/work/selenium/datadriven/TestNGDataDriverTest.java @@ -79,4 +79,13 @@ public class TestNGDataDriverTest { testNGData.addFunction(Functions.getTime()); testNGData.addDataDriver(functionFile, "Sheet1", true); } + + /** + * 测试{@link TestNGDataDriver#addDataDriver(String, Object...)}方法 + */ + @Test + public void addDataDriverTest_String_Objects() { + testNGData.addDataDriver("123", "1", "2", "3"); + testNGData.addDataDriver("docx3", "1", "2", "3"); + } } diff --git a/src/test/java/pres/auxiliary/work/selenium/element/测试文件.xml b/src/test/java/pres/auxiliary/work/selenium/element/测试文件.xml index 2be9a7f..46bd990 100644 --- a/src/test/java/pres/auxiliary/work/selenium/element/测试文件.xml +++ b/src/test/java/pres/auxiliary/work/selenium/element/测试文件.xml @@ -3,6 +3,7 @@ //iframe[contains(@src,'${src}')] + //lable[contains(text(), '${condition}')]/../span/input[1] \ No newline at end of file diff --git a/src/test/java/pres/auxiliary/work/selenium/xml/测试用xml文件.xml b/src/test/java/pres/auxiliary/work/selenium/xml/测试用xml文件.xml index 36d4d3e..fc22b25 100644 --- a/src/test/java/pres/auxiliary/work/selenium/xml/测试用xml文件.xml +++ b/src/test/java/pres/auxiliary/work/selenium/xml/测试用xml文件.xml @@ -3,6 +3,9 @@ //XXX模板控件1[@X='${name}']/div/div[@${att}='${id}']/input http body ${tagName} + //XXX模板控件1[@X='${src}']/div[@name='${name}'] + //XXX模板控件1[@X='${src}']/div[@name='${name}']/div[@is='${str1}' and text()='${str1}'] + //XXX模板控件1[@X='${src}']/div[@name='${name}']/div[@is='${str1}' and text()='${src}']/span[text()='${str2}']/span[id='${aaaa}'] @@ -62,6 +65,25 @@ + + + + + + + + + \ No newline at end of file