完成无文件形式读取元素定位方式类对象
This commit is contained in:
parent
0e5e111a91
commit
d8818efdc1
|
@ -6,6 +6,7 @@ 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;
|
||||
|
||||
/**
|
||||
* <p><b>文件名:</b>NoFileLocation.java</p>
|
||||
|
@ -40,10 +41,9 @@ public class NoFileLocation extends AbstractLocation implements WriteLocation, W
|
|||
nowLocationJson = new JSONObject();
|
||||
nowLocationJson.put(JsonLocation.KEY_TEMPLETE, new JSONObject());
|
||||
nowLocationJson.put(JsonLocation.KEY_ELEMENT, new JSONObject());
|
||||
//克隆当前元素json信息
|
||||
newLocationJson = (JSONObject) nowLocationJson.clone();
|
||||
//复制当前元素json信息
|
||||
newLocationJson = JSONObject.parseObject(nowLocationJson.toJSONString());
|
||||
|
||||
//初始化json定位类对象
|
||||
jsonLocation = new JsonLocation(nowLocationJson.toJSONString());
|
||||
}
|
||||
|
||||
|
@ -108,45 +108,61 @@ public class NoFileLocation extends AbstractLocation implements WriteLocation, W
|
|||
|
||||
@Override
|
||||
public void putTempletReplaceKey(String name, String templetId, String key, String value) {
|
||||
// TODO Auto-generated method stub
|
||||
//获取元素定位方式列表
|
||||
JSONObject elementJson = getElementJson(name);
|
||||
//获取元素定位方式集合
|
||||
JSONArray locationList = elementJson.getJSONArray(JsonLocation.KEY_LOCATION);
|
||||
//若当前元素不存在定位方式集合,则抛出异常
|
||||
if (locationList == null) {
|
||||
throw new UndefinedElementException(templetId, ExceptionElementType.ELEMENT);
|
||||
}
|
||||
|
||||
//遍历集合,获取与当前指定的模板id一致的模板,在其下记录相应的key与value
|
||||
for (int i = 0; i < locationList.size(); i++) {
|
||||
JSONObject locationJson = locationList.getJSONObject(i);
|
||||
//判断当前json是否包含模板Id的key
|
||||
if (locationJson.containsKey(JsonLocation.KEY_TEMP)) {
|
||||
String tempId = locationJson.getString(JsonLocation.KEY_TEMP);
|
||||
//判断当前id是否与所传的id一致,若一致,则在json下记录相应的属性值,并结束当前方法
|
||||
if (templetId.equals(tempId)) {
|
||||
locationJson.put(key, value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//若循环完毕后仍未找到模板,则抛出异常
|
||||
throw new UndefinedElementException(templetId, ExceptionElementType.ELEMENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<ByType> findElementByTypeList(String name) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
//对json定位方式读取类进行重构判断,并调用相应的方法
|
||||
return getJsonLocation().findElementByTypeList(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<String> findValueList(String name) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
//对json定位方式读取类进行重构判断,并调用相应的方法
|
||||
return getJsonLocation().findValueList(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ElementType findElementType(String name) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
//对json定位方式读取类进行重构判断,并调用相应的方法
|
||||
return getJsonLocation().findElementType(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<String> findIframeNameList(String name) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
//对json定位方式读取类进行重构判断,并调用相应的方法
|
||||
return getJsonLocation().findIframeNameList(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long findWaitTime(String name) {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO 测试使用,返回元素信息
|
||||
*/
|
||||
public String getJson() {
|
||||
return newLocationJson.toJSONString();
|
||||
//对json定位方式读取类进行重构判断,并调用相应的方法
|
||||
return getJsonLocation().findWaitTime(name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,4 +178,25 @@ public class NoFileLocation extends AbstractLocation implements WriteLocation, W
|
|||
|
||||
return newLocationJson.getJSONObject(JsonLocation.KEY_ELEMENT).getJSONObject(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于构建JsonLocation类,若当前json未改变,则不重新构造;反之,则重新构造元素定位方式
|
||||
* @return 返回JsonLocation类
|
||||
*/
|
||||
private JsonLocation getJsonLocation() {
|
||||
if (!isJsonChange()) {
|
||||
jsonLocation.analysisJson(newLocationJson.toJSONString());
|
||||
nowLocationJson = JSONObject.parseObject(newLocationJson.toJSONString());
|
||||
}
|
||||
|
||||
return jsonLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于判断当前json是否有变化,即是否对当前的json进行过变更
|
||||
* @return json是否存在变化
|
||||
*/
|
||||
private boolean isJsonChange() {
|
||||
return nowLocationJson.equals(newLocationJson);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
package pres.auxiliary.work.selenium.location;
|
||||
|
||||
/**
|
||||
* 若传入的模型参数有误时抛出的异常
|
||||
* @author 彭宇琦
|
||||
* @version V1.0
|
||||
*
|
||||
*/
|
||||
public class UndefinedModeException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public UndefinedModeException() {
|
||||
super();
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public UndefinedModeException(String arg0, Throwable arg1, boolean arg2,
|
||||
boolean arg3) {
|
||||
super(arg0, arg1, arg2, arg3);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public UndefinedModeException(String arg0, Throwable arg1) {
|
||||
super(arg0, arg1);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public UndefinedModeException(String arg0) {
|
||||
super(arg0);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public UndefinedModeException(Throwable arg0) {
|
||||
super(arg0);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -20,16 +20,17 @@ public interface WriteTempletLocation {
|
|||
public abstract void putTemplet(String templetId, String templetValue);
|
||||
|
||||
/**
|
||||
* 用于设置元素调用模板时,该内容允许多次设置,将存储多个替换模板的词语
|
||||
* 用于设置元素调用模板时,该内容允许多次设置,将存储多个替换模板的词语。若元素不存在相应的模板,则抛出异常
|
||||
* @param name 元素名称
|
||||
* @param templetId 模板id
|
||||
* @param key 被替换的关键词
|
||||
* @param value 替换的内容
|
||||
* @throws UndefinedElementException 模板id不存在时抛出的异常
|
||||
*/
|
||||
public abstract void putTempletReplaceKey(String name, String templetId, String key, String value);
|
||||
|
||||
/**
|
||||
* 用于设置元素调用模板的id,该内容允许多次设置,将存储多个元素定位方式模板
|
||||
* 用于设置元素调用模板的id,该内容允许多次设置,将存储多个元素定位方式模板。若元素名称不存在,则创建元素
|
||||
* @param name 元素名称
|
||||
* @param byType 元素定位类型{@link ByType}枚举
|
||||
* @param templetId 模板id
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package pres.auxiliary.work.selenium.location;
|
||||
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
|
@ -20,11 +19,6 @@ public class NoFileLocationTest {
|
|||
test = new NoFileLocation();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public void showData() {
|
||||
System.out.println(test.getJson());
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于测试{@link NoFileLocation#putElementLocation(String, ByType, String)}方法<br>
|
||||
*/
|
||||
|
@ -89,4 +83,87 @@ public class NoFileLocationTest {
|
|||
public void putTempletTest() {
|
||||
test.putTemplet("模板1", "//*[text()='${name}']");
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于测试{@link NoFileLocation#putTempletReplaceKey(String, String, String, String)}方法<br>
|
||||
* 预期:<br>
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void putTempletReplaceKeyTest_NoException() {
|
||||
test.putElementLocation("测试控件8", ByType.LINKTEXT, "测试控件");
|
||||
test.putElementTempletLocation("测试控件8", ByType.XPATH, "模板1");
|
||||
test.putElementTempletLocation("测试控件8", ByType.CLASSNAME, "模板2");
|
||||
|
||||
test.putTempletReplaceKey("测试控件8", "模板1", "id", "test");
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于测试{@link NoFileLocation#putTempletReplaceKey(String, String, String, String)}方法<br>
|
||||
* 预期:<br>
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void putTempletReplaceKeyTest_Exception() {
|
||||
try {
|
||||
test.putTempletReplaceKey("测试控件8", "模板1", "id", "test");
|
||||
} catch (Exception e) {
|
||||
System.out.println("抛出异常,无元素定位方式");
|
||||
}
|
||||
|
||||
test.putElementLocation("测试控件8", ByType.LINKTEXT, "测试控件");
|
||||
test.putElementTempletLocation("测试控件8", ByType.XPATH, "模板1");
|
||||
test.putElementTempletLocation("测试控件8", ByType.CLASSNAME, "模板2");
|
||||
|
||||
try {
|
||||
test.putTempletReplaceKey("测试控件8", "模板3", "id", "test");
|
||||
} catch (Exception e) {
|
||||
System.out.println("抛出异常,不存在相应的元素定位模板");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 由于返回方法调用{@link JsonLocation}类的返回方法,故统一进行测试
|
||||
*/
|
||||
@Test
|
||||
public void getElementLocationTest() {
|
||||
String name = "测试控件8";
|
||||
|
||||
//添加定位方式
|
||||
test.putElementLocation(name, ByType.LINKTEXT, "测试控件");
|
||||
test.putElementTempletLocation(name, ByType.XPATH, "模板1");
|
||||
//添加模板
|
||||
test.putTemplet("模板1", "//*[text()='${name}']/span[@id=${id}]/span[text()='${key}']");
|
||||
//添加定位属性
|
||||
test.putTempletReplaceKey(name, "模板1", "id", "test");
|
||||
test.putTempletReplaceKey(name, "模板1", "name", "控件8");
|
||||
//添加元素
|
||||
test.putElementType(name, ElementType.COMMON_ELEMENT);
|
||||
//添加元素所在窗体
|
||||
test.putIframeNameList(name, "窗体1");
|
||||
//添加元素等待时间
|
||||
test.putWaitTime(name, 10);
|
||||
|
||||
//添加窗体
|
||||
test.putElementLocation("窗体1", ByType.LINKTEXT, "窗体");
|
||||
|
||||
//读取元素信息
|
||||
System.out.println("元素定位内容:" + test.findValueList(name));
|
||||
System.out.println("元素定位方式:" + test.findElementByTypeList(name));
|
||||
System.out.println("元素类型:" + test.findElementType(name));
|
||||
System.out.println("元素所在窗体:" + test.findIframeNameList(name));
|
||||
System.out.println("元素等待时间:" + test.findWaitTime(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试json改变与不改变时,是否有重新构建读取类对象(需要断点)
|
||||
*/
|
||||
@Test
|
||||
public void changeJsonGetElementTest() {
|
||||
test.putElementLocation("测试控件1", ByType.LINKTEXT, "测试控件1");
|
||||
test.putElementLocation("测试控件2", ByType.LINKTEXT, "测试控件2");
|
||||
|
||||
System.out.println("元素定位内容:" + test.findValueList("测试控件1"));
|
||||
System.out.println("元素定位内容:" + test.findValueList("测试控件2"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue