添加xml其他内容获取方法,并添加单元测试,剩余获取定位值方法需要调整

This commit is contained in:
彭宇琦 2020-10-08 23:56:48 +08:00
parent e3b5562001
commit ed403b6e53
6 changed files with 304 additions and 52 deletions

View File

@ -8,9 +8,9 @@
</classpathentry> </classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes> <attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
@ -23,17 +23,10 @@
<classpathentry kind="lib" path="lib/beautyeye_lnf.jar"/> <classpathentry kind="lib" path="lib/beautyeye_lnf.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes> <attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path=".apt_generated"> <classpathentry kind="src" path=".apt_generated">
<attributes> <attributes>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
@ -41,17 +34,19 @@
</classpathentry> </classpathentry>
<classpathentry kind="src" output="target/test-classes" path=".apt_generated_tests"> <classpathentry kind="src" output="target/test-classes" path=".apt_generated_tests">
<attributes> <attributes>
<attribute name="optional" value="true"/>
<attribute name="test" value="true"/> <attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations"> <classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes> <attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/> <attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>

View File

@ -46,4 +46,27 @@ public abstract class AbstractRead {
* @return 元素的等待时间 * @return 元素的等待时间
*/ */
public abstract long getWaitTime(String name); public abstract long getWaitTime(String name);
/**
* 用于将读取到的元素类型的文本值转换为元素类型枚举类对象
* @param value 元素类型文本值
* @return 元素类型枚举类对象
*/
protected ElementType toElementType(String value) {
//转换元素类型枚举并返回
switch (value) {
case "0":
return ElementType.COMMON_ELEMENT;
case "1":
return ElementType.DATA_LIST_ELEMENT;
case "2":
return ElementType.SELECT_DATAS_ELEMENT;
case "3":
return ElementType.SELECT_OPTION_ELEMENT;
case "4":
return ElementType.IFRAME_ELEMENT;
default:
return null;
}
}
} }

View File

@ -1,36 +1,36 @@
package pres.auxiliary.work.selenium.location; package pres.auxiliary.work.selenium.location;
/** /**
* 该异常提示用户需要检查xml文件是否为空 * 路径文件有误时抛出的异常
* @author 彭宇琦 * @author 彭宇琦
* @version Ver1.0 * @version Ver1.0
*/ */
public class XmlFileNameIsNullException extends Exception { public class IncorrectFileException extends RuntimeException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public XmlFileNameIsNullException() { public IncorrectFileException() {
super(); super();
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
} }
public XmlFileNameIsNullException(String arg0, Throwable arg1, public IncorrectFileException(String arg0, Throwable arg1,
boolean arg2, boolean arg3) { boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3); super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
} }
public XmlFileNameIsNullException(String arg0, Throwable arg1) { public IncorrectFileException(String arg0, Throwable arg1) {
super(arg0, arg1); super(arg0, arg1);
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
} }
public XmlFileNameIsNullException(String arg0) { public IncorrectFileException(String arg0) {
super(arg0); super(arg0);
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
} }
public XmlFileNameIsNullException(Throwable arg0) { public IncorrectFileException(Throwable arg0) {
super(arg0); super(arg0);
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
} }

View File

@ -2,6 +2,8 @@ package pres.auxiliary.work.selenium.location;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Optional;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
@ -46,8 +48,19 @@ public class ReadXml extends AbstractRead {
*/ */
private Document dom; private Document dom;
public ReadXml(File xmlFile) throws DocumentException { /**
dom = new SAXReader().read(xmlFile); * 构造对象
* @param xmlFile xml文件对象
* @throws IncorrectFileException xml文件有误时抛出的异常
*/
public ReadXml(File xmlFile) {
//将编译时异常转换为运行时异常
try {
dom = new SAXReader().read(xmlFile);
} catch (DocumentException e) {
throw new IncorrectFileException("xml文件异常文件位置" + xmlFile.getAbsolutePath());
}
} }
@Override @Override
@ -66,48 +79,71 @@ public class ReadXml extends AbstractRead {
@Override @Override
public ArrayList<String> getValueList(String name) { public ArrayList<String> getValueList(String name) {
// TODO Auto-generated method stub ArrayList<String> valueList = new ArrayList<>();
return null; //查询元素
Element element = getElementLabelElement(name);
//遍历元素下所有的定位标签并将其转换为相应的ByType枚举存储至byTypeList中
for (Object byElement : element.elements()) {
valueList.add(((Element)byElement).getText());
//TODO 获取到元素内容后需要判断元素是否需要读取模板
}
return valueList;
} }
@Override @Override
public ElementType getElementType(String name) { public ElementType getElementType(String name) {
//查询元素 //查询元素
Element element = getElementLabelElement(name); Element element = getElementLabelElement(name);
//获取元素的元素类型属性 //若元素标签为iframe则无法获取属性直接赋予窗体类型
String elementTypeText = element.attributeValue("element_type"); if (element.getName().equals("iframe")) {
//若属性不存在则使其指向普通元素 return toElementType("4");
elementTypeText = elementTypeText == null ? "0" : elementTypeText; } else {
//非窗体元素则获取元素的元素类型属性
//转换元素类型枚举并返回 String elementTypeText = element.attributeValue("element_type");
switch (elementTypeText) { //若属性不存在则使其指向普通元素
case "0": return toElementType(elementTypeText == null ? "0" : elementTypeText);
return ElementType.COMMON_ELEMENT;
case "1":
return ElementType.DATA_LIST_ELEMENT;
case "2":
return ElementType.SELECT_DATAS_ELEMENT;
case "3":
return ElementType.SELECT_OPTION_ELEMENT;
case "4":
return ElementType.IFRAME_ELEMENT;
default:
break;
} }
return null;
} }
@Override @Override
public ArrayList<String> getIframeNameList(String name) { public ArrayList<String> getIframeNameList(String name) {
// TODO Auto-generated method stub //查询元素
return null; Element element = getElementLabelElement(name);
//存储父层iframe元素名称
ArrayList<String> iframeName = new ArrayList<>();
//循环获取元素的父层级直到获取到顶层为止
while (!element.isRootElement()) {
//定位到元素的父层
element = element.getParent();
//判断当前元素的标签名称若为iframe标签则获取其名称并存储至集合中
if (element.getName().equals("iframe")) {
iframeName.add(element.attributeValue("name"));
}
}
//反序集合使最上层窗体排在最前面
Collections.reverse(iframeName);
return iframeName;
} }
@Override @Override
public long getWaitTime(String name) { public long getWaitTime(String name) {
// TODO Auto-generated method stub //查询元素
return 0; Element element = getElementLabelElement(name);
//获取元素存储等待时间属性值并转换为long类型
try {
String text = element.attributeValue("wait");
//将属性值进行转换若属性值不存在则赋为-1
long time = Long.valueOf(text == null ? "-1" : text);
//若转换的时间小于0则返回-1
return time < 0 ? -1L : time;
} catch (NumberFormatException e) {
return -1L;
}
} }
/** /**
@ -143,7 +179,8 @@ public class ReadXml extends AbstractRead {
*/ */
private Element getElementLabelElement(String name) { private Element getElementLabelElement(String name) {
//定义获取元素的xpath //定义获取元素的xpath
String selectElementXpath = "//element[@name='" + name +"']"; String selectElementXpath = "//*[@name='" + name +"']";
return (Element) dom.selectSingleNode(selectElementXpath); //根据xpath获取元素若无法获取到元素则抛出空指针异常
return Optional.of((Element) dom.selectSingleNode(selectElementXpath)).get();
} }
} }

View File

@ -0,0 +1,95 @@
package pres.auxiliary.work.selenium.location;
import java.io.File;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* <p><b>文件名</b>ReadXmlTest.java</p>
* <p><b>用途</b>用于对{@link ReadXml}类进行测试</p>
* <p><b>编码时间</b>2020年10月8日 下午11:19:40</p>
* <p><b>修改时间</b>2020年10月8日 下午11:19:40</p>
* @author 彭宇琦
* @version Ver1.0
* @since JDK 12
*/
public class ReadXmlTest {
private final File XML_FILE = new File("src/test/java/pres/auxiliary/work/selenium/location/测试用xml文件.xml");
/**
* 定义类
*/
ReadXml rx;
/**
* 初始化数据
*/
@BeforeClass
public void init() {
rx = new ReadXml(XML_FILE);
}
/**
* 测试{@link ReadXml#getElementByTypeList(String)}方法
* 预期
* XPATH
* CSS
* XPATH
*/
@Test
public void getElementByTypeListTest() {
rx.getElementByTypeList("XX控件11").forEach(System.out :: println);
}
/**
* 测试{@link ReadXml#getElementType(String)}方法
* 预期
* COMMON_ELEMENT
* COMMON_ELEMENT
* DATA_LIST_ELEMENT
* SELECT_DATAS_ELEMENT
* SELECT_OPTION_ELEMENT
* IFRAME_ELEMENT
*/
@Test
public void getElementTypeTest() {
System.out.println(rx.getElementType("XX控件11"));
System.out.println(rx.getElementType("XX控件16"));
System.out.println(rx.getElementType("XX控件17"));
System.out.println(rx.getElementType("XX控件18"));
System.out.println(rx.getElementType("XX控件19"));
System.out.println(rx.getElementType("窗体3"));
}
/**
* 测试{@link ReadXml#getIframeNameList(String)}方法
* 预期
* 窗体1
* 窗体1.1
* 窗体1.1.1
*/
@Test
public void getIframeNameListTest() {
rx.getIframeNameList("XX控件6").forEach(System.out :: println);
}
/**
* 测试{@link ReadXml#getWaitTime(String)}方法
* 预期
* -1
* 100
* 1
* 0
* -1
* -1
*/
@Test
public void getWaitTimeTest() {
System.out.println(rx.getWaitTime("XX控件14"));
System.out.println(rx.getWaitTime("XX控件15"));
System.out.println(rx.getWaitTime("XX控件16"));
System.out.println(rx.getWaitTime("XX控件17"));
System.out.println(rx.getWaitTime("XX控件18"));
System.out.println(rx.getWaitTime("XX控件19"));
}
}

View File

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="">
<templet>
<xpath id='1'>//XXX模板控件1[@X='${name}']/div/div[@${att}='${id}']/input</xpath>
<css id='2'>http body ${tagName}</css>
<xpath id='3'>//XXX模板控件1[@X='${src}']/div[@name='${name}']</xpath>
<xpath id='4'>//XXX模板控件1[@X='${src}']/div[@name='${name}']/div[@is='${str1}' and text()='${str1}']</xpath>
<xpath id='5'>//XXX模板控件1[@X='${src}']/div[@name='${name}']/div[@is='${str1}' and text()='${src}']/span[text()='${str2}']/span[id='${aaaa}']</xpath>
</templet>
<element name='XX控件1'>
<xpath is_use='true'>//XX控件1[@X='XXXX']</xpath>
</element>
<iframe name='窗体1' >
<xpath is_use='true'>//窗体1[@X='XXXX']</xpath>
<css temp_id='2' tagName='iframe'/>
<element name='XX控件2'>
<xpath is_use='true'>//XX控件2[@X='XXXX']</xpath>
</element>
<element name='XX控件3'>
<xpath is_use='true'>//XXX控件3[@X='XXXX']</xpath>
</element>
<iframe name='窗体1.1' >
<xpath is_use='true'>//窗体1.1[@X='XXXX']</xpath>
<element name='XX控件4'>
<xpath is_use='true'>//XXX控件4[@X='XXXX']</xpath>
</element>
<element name='XX控件5'>
<xpath is_use='true'>//XXX控件5[@X='XXXX']</xpath>
</element>
<iframe name='窗体1.1.1' >
<xpath is_use='true'>//窗体1.1.1[@X='XXXX']</xpath>
<element name='XX控件6'>
<xpath is_use='true'>//XXX控件6[@X='XXXX']</xpath>
</element>
<element name='XX控件7'>
<xpath is_use='true'>//XXX控件7[@X='XXXX']</xpath>
</element>
</iframe>
</iframe>
<iframe name='窗体1.2' >
<xpath is_use='true'>//窗体1.2[@X='XXXX']</xpath>
<element name='XX控件8'>
<xpath is_use='true'>//XXX控件8[@X='XXXX']</xpath>
</element>
<element name='XX控件9'>
<xpath is_use='true'>//XXX控件9[@X='XXXX']</xpath>
</element>
</iframe>
</iframe>
<iframe name='窗体2' >
<xpath is_use='true'>//窗体2[@X='XXXX']</xpath>
<element name='XX控件10'>
<xpath is_use='true'>//XXX控件10[@X='XXXX']</xpath>
</element>
<element name='XX控件11'>
<xpath is_use='true' temp_id='1' id='Test' att='src'/>
<css is_use='true' temp_id='2' tagName='div'/>
<xpath is_use='true' temp_id='3' />
</element>
<element name='XX控件12'>
<xpath is_use='true' temp_id='3' />
</element>
</iframe>
<iframe name='窗体3' >
<xpath is_use='true'>//窗体3[@X='${ccc}']</xpath>
<element name='XX控件13'>
<xpath is_use='true' temp_id='3' />
</element>
<element name='XX控件14'>
<xpath is_use='true' temp_id='4' />
</element>
<element name='XX控件15' wait='100'>
<xpath is_use='true' temp_id='5' />
</element>
<element name='XX控件16' element_type='0' wait='1'>
<xpath is_use='true' temp_id='1' id='Test' att='src'/>
</element>
<element name='XX控件17' element_type='1' wait='0'>
<xpath is_use='true' temp_id='1' id='Test' att='src'/>
</element>
<element name='XX控件18' element_type='2' wait='-11'>
<xpath is_use='true' temp_id='1' id='Test' att='src'/>
</element>
<element name='XX控件19' element_type='3' wait='ddsf'>
<xpath is_use='true' temp_id='1' id='Test' att='src'/>
</element>
</iframe>
</project>