添加xml文本获取方法,并更新设计图
This commit is contained in:
parent
cface753ac
commit
a67f52aa9b
|
@ -56,6 +56,11 @@ public abstract class Case {
|
||||||
*/
|
*/
|
||||||
final String END_SIGN = "}*";
|
final String END_SIGN = "}*";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于存储传入到正则表达式中的开始标记
|
||||||
|
*/
|
||||||
|
final String START_SIGN_REGIX = "\\*\\{";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 优先级
|
* 优先级
|
||||||
*/
|
*/
|
||||||
|
@ -78,6 +83,9 @@ public abstract class Case {
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Case(File configXmlFile) {
|
public Case(File configXmlFile) {
|
||||||
|
//定义能获取到文本的属性,以便于后续的调整
|
||||||
|
String textAttribute = "value";
|
||||||
|
|
||||||
// 判断传入的configurationFile是否为一个文件类对象,若非文件类对象,则抛出异常
|
// 判断传入的configurationFile是否为一个文件类对象,若非文件类对象,则抛出异常
|
||||||
try {
|
try {
|
||||||
configXml = new SAXReader().read(configXmlFile);
|
configXml = new SAXReader().read(configXmlFile);
|
||||||
|
@ -86,17 +94,17 @@ public abstract class Case {
|
||||||
}
|
}
|
||||||
|
|
||||||
//获取xml中包含value的元素,并将其中包含需要替换的词语存储至textMap\
|
//获取xml中包含value的元素,并将其中包含需要替换的词语存储至textMap\
|
||||||
List<Element> textElement = configXml.selectNodes("//*[@value]");
|
List<Element> textElement = configXml.selectNodes("//*[@" + textAttribute + "]");
|
||||||
textElement.stream().
|
textElement.stream().
|
||||||
//获取元素的value属性,将其转换为文本对象
|
//获取元素的value属性,将其转换为文本对象
|
||||||
map(e -> e.attributeValue("value")).
|
map(e -> e.attributeValue(textAttribute)).
|
||||||
//筛选包含*{的文本
|
//筛选包含*{的文本
|
||||||
filter(e -> e.indexOf("*{") > -1).forEach(e -> {
|
filter(e -> e.indexOf(START_SIGN) > -1).forEach(e -> {
|
||||||
//对文本按照*{切割,并筛选包含}*的文本
|
//对文本按照*{切割,并筛选包含}*的文本
|
||||||
Arrays.asList(e.split("\\*\\{")).stream().filter(s -> s.indexOf("}*") > -1).
|
Arrays.asList(e.split(START_SIGN_REGIX)).stream().filter(s -> s.indexOf(END_SIGN) > -1).
|
||||||
forEach(s -> {
|
forEach(s -> {
|
||||||
//将需要存储的替换词语存入textMap中
|
//将需要存储的替换词语存入textMap中
|
||||||
textMap.put(s.substring(0, s.indexOf("}*")), "");
|
textMap.put(s.substring(0, s.indexOf(END_SIGN)), "");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -202,13 +210,13 @@ public abstract class Case {
|
||||||
* @param word 测试用例xml库中需要替换的词语
|
* @param word 测试用例xml库中需要替换的词语
|
||||||
* @param value 被替换的词语
|
* @param value 被替换的词语
|
||||||
*/
|
*/
|
||||||
public void setReplaceWord(String word, String value) {
|
public void setReplaceWord(String word, String text) {
|
||||||
//判断该词语是否存在于textMap中,若不存在,则抛出异常
|
//判断该词语是否存在于textMap中,若不存在,则抛出异常
|
||||||
if (!textMap.containsKey(word)) {
|
if (!textMap.containsKey(word)) {
|
||||||
throw new IncorrectFileException("未找到需要替换的词语:" + word);
|
throw new IncorrectFileException("未找到需要替换的词语:" + word);
|
||||||
}
|
}
|
||||||
//存储替换的词语
|
//存储替换的词语
|
||||||
textMap.put(word, value);
|
textMap.put(word, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -230,4 +238,34 @@ public abstract class Case {
|
||||||
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于获取用例xml中对应用例的标签内的文本
|
||||||
|
* @param caseName 用例名称
|
||||||
|
* @param label 标签枚举
|
||||||
|
* @param id 对应标签的id属性
|
||||||
|
* @return 标签中存储的值
|
||||||
|
*/
|
||||||
|
String getText(String caseName, LabelType label, int id) {
|
||||||
|
//定位case标签的名称属性名
|
||||||
|
String caseLabelNameAttribute = "name";
|
||||||
|
String labelIdAttribute = "id";
|
||||||
|
String labelValueAttribute = "value";
|
||||||
|
|
||||||
|
//拼接xpath,规则"//case[@name='caseName']//标签名称[@id='id']"
|
||||||
|
String xpath = "//" + LabelType.CASE.getName() +
|
||||||
|
"[@" + caseLabelNameAttribute + "='" +
|
||||||
|
caseName + "']//" + label.getName() +
|
||||||
|
"[@" + labelIdAttribute + "='" + id +"']";
|
||||||
|
|
||||||
|
//获取相应的文本内容
|
||||||
|
String text = ((Element)(configXml.selectSingleNode(xpath))).attributeValue(labelValueAttribute);
|
||||||
|
//判断获取的内容是否为空,为空则跑出异常
|
||||||
|
if (text == null) {
|
||||||
|
throw new LabelNotFoundException("不存在的标签:" + xpath);
|
||||||
|
}
|
||||||
|
|
||||||
|
//返回相应的文本
|
||||||
|
return text;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,9 @@ import pres.auxiliary.work.testcase.writecase.InputType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p><b>文件名:</b>InformationCase.java</p>
|
* <p><b>文件名:</b>InformationCase.java</p>
|
||||||
* <p><b>用途:</b>用于输出与页面新增或编辑信息相关的用例</p>
|
* <p><b>用途:</b>用于输出与页面新增或编辑信息相关的用例,类中提供部分模板中会使用到
|
||||||
|
* 待替换的词语,可通过类名获取相应的文本,传入{@link #setReplaceWord(String, String)}方法
|
||||||
|
* 的第一个参数中</p>
|
||||||
* <p><b>编码时间:</b>2020年3月5日上午8:30:12</p>
|
* <p><b>编码时间:</b>2020年3月5日上午8:30:12</p>
|
||||||
* <p><b>修改时间:</b>2020年3月5日上午8:30:12</p>
|
* <p><b>修改时间:</b>2020年3月5日上午8:30:12</p>
|
||||||
* @author 彭宇琦
|
* @author 彭宇琦
|
||||||
|
@ -17,7 +19,30 @@ import pres.auxiliary.work.testcase.writecase.InputType;
|
||||||
* @since JDK 12
|
* @since JDK 12
|
||||||
*/
|
*/
|
||||||
public class InformationCase extends Case {
|
public class InformationCase extends Case {
|
||||||
private final String BUTTON_NAME = "按钮名称";
|
/**
|
||||||
|
* 用于标记提交按钮名称
|
||||||
|
*/
|
||||||
|
public static final String BUTTON_NAME = "按钮名称";
|
||||||
|
/**
|
||||||
|
* 用于标记需要添加的信息
|
||||||
|
*/
|
||||||
|
public static final String ADD_INFORMATION = "信息";
|
||||||
|
/**
|
||||||
|
* 用于标记添加成功预期的前文
|
||||||
|
*/
|
||||||
|
public static final String SUCCESS_EXCEPT_TEXT_START = "成功预期前文";
|
||||||
|
/**
|
||||||
|
* 用于标记添加成功预期的后文
|
||||||
|
*/
|
||||||
|
public static final String SUCCESS_EXCEPT_TEXT_END = "成功预期后文";
|
||||||
|
/**
|
||||||
|
* 用于标记添加失败预期前文
|
||||||
|
*/
|
||||||
|
public static final String FAIL_EXCEPT_TEXT_START = "失败预期前文";
|
||||||
|
/**
|
||||||
|
* 用于标记添加失败预期后文
|
||||||
|
*/
|
||||||
|
public static final String FAIL_EXCEPT_TEXT_END = "失败预期后文";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过测试用例模板库的xml配置文件来构造InformationCase对象
|
* 通过测试用例模板库的xml配置文件来构造InformationCase对象
|
||||||
|
@ -27,130 +52,60 @@ public class InformationCase extends Case {
|
||||||
super(configXmlFile);
|
super(configXmlFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setReplaceWord(String word, String text) {
|
||||||
|
//由于预期前后文是直接在文本上拼接,故为保证语句通顺,则自动为其添加
|
||||||
|
//若传入的参数为成功或失败预期的前文时,则在文本后加上逗号
|
||||||
|
if (SUCCESS_EXCEPT_TEXT_START.equals(word) || FAIL_EXCEPT_TEXT_START.equals(word)) {
|
||||||
|
//判断文本最后一个字符是否为逗号,若不是逗号,则将逗号拼接上
|
||||||
|
if (text.lastIndexOf(",") == text.length() - 1) {
|
||||||
|
text += ",";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//若传入的参数为成功或失败预期的后文时,则在文本前加上逗号
|
||||||
|
if (SUCCESS_EXCEPT_TEXT_END.equals(word) || FAIL_EXCEPT_TEXT_END.equals(word)) {
|
||||||
|
//判断文本第一个字符是否为逗号,若不是逗号,则将逗号拼接上
|
||||||
|
if (text.indexOf(",") == 0) {
|
||||||
|
text = "," + text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
super.setReplaceWord(word, text);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 该方法用于生成针对文本框的测试用例
|
* 该方法用于生成正确填写所有信息的用例
|
||||||
*
|
*
|
||||||
* @param name
|
|
||||||
* 文本框的名称
|
|
||||||
* @param isMust
|
|
||||||
* 是否必填
|
|
||||||
* @param isRepeat
|
|
||||||
* 是否可重复
|
|
||||||
* @param inputConfine
|
|
||||||
* 输入限制
|
|
||||||
* @param lengthConfine
|
|
||||||
* 输入长度限制
|
|
||||||
* @param numConfine
|
|
||||||
* 数字大小限制
|
|
||||||
* @return Tab对象
|
* @return Tab对象
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public Tab addTextboxCase(String name, boolean isMust, boolean isRepeat, char[] inputConfine, int[] lengthConfine,
|
// public Case addWholeInformationCase() throws IOException {
|
||||||
int[] numConfine) throws IOException {
|
// // 存储方法名
|
||||||
//用于存储步骤
|
// String methodName = "addWholeInformationCase";
|
||||||
ArrayList<String> step = new ArrayList<>();
|
// // 存储需要使用的变量
|
||||||
//用于存储预期
|
// textMap.put("buttonName", getButtonName());
|
||||||
ArrayList<String> except = new ArrayList<>();
|
// // 用于存储读取测试用例的id号
|
||||||
|
// ArrayList<Integer> l = new ArrayList<>();
|
||||||
// 存储方法名
|
//
|
||||||
String methodName = "addTextboxCase";
|
// // 清空步骤与预期中存储的信息
|
||||||
// 存储需要使用的变量
|
// //st.delete(0, st.length());
|
||||||
textMap.put("name", name);
|
// ex.delete(0, ex.length());
|
||||||
textMap.put("buttonName", getButtonName());
|
//
|
||||||
|
// // 用于存储步骤数
|
||||||
// 清空步骤与预期中存储的信息
|
// int step = 1;
|
||||||
// st.delete(0, st.length());
|
//
|
||||||
ex.delete(0, ex.length());
|
// // 添加步骤
|
||||||
|
// l.add(1);
|
||||||
// 用于存储读取测试用例的id号
|
//// st.append(step + ".正确填写所有的信息,点击“" + getButtonName() + "”按钮\r\n");
|
||||||
ArrayList<Integer> l = new ArrayList<>();
|
// ex.append(step++ + "." + successExpectation.toString() + "\r\n");
|
||||||
|
//
|
||||||
// 用于存储步骤数
|
// // 将ArrayList转换成数组
|
||||||
int step = 1;
|
// int[] id = new int[l.size()];
|
||||||
|
// for (int i = 0; i < l.size(); i++) {
|
||||||
// 添加为空的步骤及预期
|
// id[i] = l.get(i);
|
||||||
// st.append(step + ".不填写或只输入空格,点击“" + getButtonName() + "”按钮\r\n");
|
// }
|
||||||
l.add(1);
|
// return after(("添加信息完整的" + getInformationName()), new StringBuilder(getStep(methodName, id)[0]), ex, ("信息完整," + getInformationName()), 1,
|
||||||
// 判断文本框是否必填,并添加相应的信息(必填时,信息为空,则添加错误的预期)
|
// getPrecondition());
|
||||||
if (isMust) {
|
// }
|
||||||
ex.append(step++ + "." + failExpectation.toString() + "\r\n");
|
|
||||||
} else {
|
|
||||||
ex.append(step++ + "." + successExpectation.toString() + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加填写特殊字符
|
|
||||||
// st.append(step + ".填写特殊字符或HTML代码,点击“" + getButtonName() + "”按钮\r\n");
|
|
||||||
l.add(2);
|
|
||||||
// 判断文本框是否包含特殊字符的限制,若能输入特殊字符,则添加正确的用例,反之则添加失败的用例
|
|
||||||
if (inputConfine != null) {
|
|
||||||
// 用于判断inputConfine是否存在特殊字符的限制
|
|
||||||
boolean isSPE = false;
|
|
||||||
// 循环,遍历inputConfine
|
|
||||||
for (char c : inputConfine) {
|
|
||||||
// 判断当前元素是否为Input.SPE,若是,则说明文本框允许输入输入特殊字符,则将isSPE设为true,并结束循环
|
|
||||||
if (Character.compare(c, InputType.SPE) == 0) {
|
|
||||||
isSPE = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 判断isSPE,为true则添加正确的用例,为false则添加失败的用例
|
|
||||||
if (isSPE) {
|
|
||||||
ex.append(step++ + "." + successExpectation.toString() + "\r\n");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
ex.append(step++ + "." + failExpectation.toString() + "\r\n");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 如果没有输入限制,则默认可以输入特殊字符
|
|
||||||
ex.append(step++ + "." + successExpectation.toString() + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加输入限制的步骤,没有限制则不添加
|
|
||||||
// 判断输入限制是否为null,若不为null,则添加输入限制步骤
|
|
||||||
if (inputConfine != null) {
|
|
||||||
String[] s = inputConfineStep(inputConfine);
|
|
||||||
textMap.put("charGroup", s[0]);
|
|
||||||
l.add(3);
|
|
||||||
// st.append(step + ".输入非" + s[0] + "字符,点击“" + getButtonName() + "”按钮\r\n");
|
|
||||||
ex.append(step++ + "." + s[1] + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加长度限制的步骤,没有则不添加
|
|
||||||
if (lengthConfine != null) {
|
|
||||||
String[] s = lengthConfineStep(lengthConfine, step, l);
|
|
||||||
// st.append(s[0]);
|
|
||||||
ex.append(s[0]);
|
|
||||||
step = Integer.valueOf(s[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加数字大小限制的步骤,没有则不添加
|
|
||||||
if (numConfine != null) {
|
|
||||||
String[] s = numConfine(numConfine, step, l);
|
|
||||||
// st.append(s[0]);
|
|
||||||
ex.append(s[0]);
|
|
||||||
step = Integer.valueOf(s[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isRepeat) {
|
|
||||||
l.add(12);
|
|
||||||
// st.append(step + ".填写一个已存在的" + name + "信息,点击“" + getButtonName() +
|
|
||||||
// "”按钮\r\n");
|
|
||||||
ex.append(step++ + "." + failExpectation.toString() + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加优先级信息
|
|
||||||
int rank = 2;
|
|
||||||
// 判断控件是否必填,若必填,则用例为1级
|
|
||||||
if (isMust) {
|
|
||||||
rank = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int[] id = new int[l.size()];
|
|
||||||
for (int i = 0; i < l.size(); i++) {
|
|
||||||
id[i] = l.get(i);
|
|
||||||
}
|
|
||||||
return after(name, new StringBuilder(getStep(methodName, id)[0]), ex, rank);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package pres.auxiliary.work.n.tcase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p><b>文件名:</b>LabelType.java</p>
|
||||||
|
* <p><b>用途:</b>定义Label标签的名称枚举,用于定位获取的标签内容</p>
|
||||||
|
* <p><b>编码时间:</b>2020年3月12日下午6:49:22</p>
|
||||||
|
* <p><b>修改时间:</b>2020年3月12日下午6:49:22</p>
|
||||||
|
* @author 彭宇琦
|
||||||
|
* @version Ver1.0
|
||||||
|
* @since JDK 12
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
enum LabelType {
|
||||||
|
/**
|
||||||
|
* 用例标签
|
||||||
|
*/
|
||||||
|
CASE("case"),
|
||||||
|
/**
|
||||||
|
* 步骤
|
||||||
|
*/
|
||||||
|
STEP("step"),
|
||||||
|
/**
|
||||||
|
* 预期
|
||||||
|
*/
|
||||||
|
EXCEPT("except"),
|
||||||
|
/**
|
||||||
|
* 标题
|
||||||
|
*/
|
||||||
|
TITLE("title"),
|
||||||
|
/**
|
||||||
|
* 前置条件
|
||||||
|
*/
|
||||||
|
PRECONDITION("precondition"),
|
||||||
|
/**
|
||||||
|
* 优先级
|
||||||
|
*/
|
||||||
|
RANK("rank"),
|
||||||
|
/**
|
||||||
|
* 关键词
|
||||||
|
*/
|
||||||
|
KEY("key");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 记录标签的名称
|
||||||
|
*/
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化每个枚举的名称
|
||||||
|
* @param name 枚举名称
|
||||||
|
*/
|
||||||
|
private LabelType(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于返回枚举对应的标签名
|
||||||
|
* @return 标签名
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package pres.auxiliary.work.testcase;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import pres.auxiliary.work.n.tcase.InformationCase;
|
||||||
|
|
||||||
|
public class InformationCaseTest {
|
||||||
|
InformationCase ic = new InformationCase(new File("ConfigurationFiles/CaseConfigurationFile/CaseTemplet/AddInformation.xml"));
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void start() {
|
||||||
|
ic.setReplaceWord(InformationCase.BUTTON_NAME, "普通");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getTextTest() {
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Loading…
Reference in New Issue