完成无文件形式读取元素定位方式类对象

This commit is contained in:
彭宇琦 2020-11-02 18:59:27 +08:00
parent 0e5e111a91
commit d8818efdc1
4 changed files with 144 additions and 69 deletions

View File

@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import pres.auxiliary.work.selenium.element.ElementType; import pres.auxiliary.work.selenium.element.ElementType;
import pres.auxiliary.work.selenium.location.UndefinedElementException.ExceptionElementType;
/** /**
* <p><b>文件名</b>NoFileLocation.java</p> * <p><b>文件名</b>NoFileLocation.java</p>
@ -40,10 +41,9 @@ public class NoFileLocation extends AbstractLocation implements WriteLocation, W
nowLocationJson = new JSONObject(); nowLocationJson = new JSONObject();
nowLocationJson.put(JsonLocation.KEY_TEMPLETE, new JSONObject()); nowLocationJson.put(JsonLocation.KEY_TEMPLETE, new JSONObject());
nowLocationJson.put(JsonLocation.KEY_ELEMENT, new JSONObject()); nowLocationJson.put(JsonLocation.KEY_ELEMENT, new JSONObject());
//克隆当前元素json信息 //复制当前元素json信息
newLocationJson = (JSONObject) nowLocationJson.clone(); newLocationJson = JSONObject.parseObject(nowLocationJson.toJSONString());
//初始化json定位类对象
jsonLocation = new JsonLocation(nowLocationJson.toJSONString()); jsonLocation = new JsonLocation(nowLocationJson.toJSONString());
} }
@ -108,45 +108,61 @@ public class NoFileLocation extends AbstractLocation implements WriteLocation, W
@Override @Override
public void putTempletReplaceKey(String name, String templetId, String key, String value) { 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 @Override
public ArrayList<ByType> findElementByTypeList(String name) { public ArrayList<ByType> findElementByTypeList(String name) {
// TODO Auto-generated method stub //对json定位方式读取类进行重构判断并调用相应的方法
return null; return getJsonLocation().findElementByTypeList(name);
} }
@Override @Override
public ArrayList<String> findValueList(String name) { public ArrayList<String> findValueList(String name) {
// TODO Auto-generated method stub //对json定位方式读取类进行重构判断并调用相应的方法
return null; return getJsonLocation().findValueList(name);
} }
@Override @Override
public ElementType findElementType(String name) { public ElementType findElementType(String name) {
// TODO Auto-generated method stub //对json定位方式读取类进行重构判断并调用相应的方法
return null; return getJsonLocation().findElementType(name);
} }
@Override @Override
public ArrayList<String> findIframeNameList(String name) { public ArrayList<String> findIframeNameList(String name) {
// TODO Auto-generated method stub //对json定位方式读取类进行重构判断并调用相应的方法
return null; return getJsonLocation().findIframeNameList(name);
} }
@Override @Override
public long findWaitTime(String name) { public long findWaitTime(String name) {
// TODO Auto-generated method stub //对json定位方式读取类进行重构判断并调用相应的方法
return 0; return getJsonLocation().findWaitTime(name);
}
/**
* TODO 测试使用返回元素信息
*/
public String getJson() {
return newLocationJson.toJSONString();
} }
/** /**
@ -162,4 +178,25 @@ public class NoFileLocation extends AbstractLocation implements WriteLocation, W
return newLocationJson.getJSONObject(JsonLocation.KEY_ELEMENT).getJSONObject(name); 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);
}
} }

View File

@ -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
}
}

View File

@ -20,16 +20,17 @@ public interface WriteTempletLocation {
public abstract void putTemplet(String templetId, String templetValue); public abstract void putTemplet(String templetId, String templetValue);
/** /**
* 用于设置元素调用模板时该内容允许多次设置将存储多个替换模板的词语 * 用于设置元素调用模板时该内容允许多次设置将存储多个替换模板的词语若元素不存在相应的模板则抛出异常
* @param name 元素名称 * @param name 元素名称
* @param templetId 模板id * @param templetId 模板id
* @param key 被替换的关键词 * @param key 被替换的关键词
* @param value 替换的内容 * @param value 替换的内容
* @throws UndefinedElementException 模板id不存在时抛出的异常
*/ */
public abstract void putTempletReplaceKey(String name, String templetId, String key, String value); public abstract void putTempletReplaceKey(String name, String templetId, String key, String value);
/** /**
* 用于设置元素调用模板的id该内容允许多次设置将存储多个元素定位方式模板 * 用于设置元素调用模板的id该内容允许多次设置将存储多个元素定位方式模板若元素名称不存在则创建元素
* @param name 元素名称 * @param name 元素名称
* @param byType 元素定位类型{@link ByType}枚举 * @param byType 元素定位类型{@link ByType}枚举
* @param templetId 模板id * @param templetId 模板id

View File

@ -1,6 +1,5 @@
package pres.auxiliary.work.selenium.location; package pres.auxiliary.work.selenium.location;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -20,11 +19,6 @@ public class NoFileLocationTest {
test = new NoFileLocation(); test = new NoFileLocation();
} }
@AfterClass
public void showData() {
System.out.println(test.getJson());
}
/** /**
* 用于测试{@link NoFileLocation#putElementLocation(String, ByType, String)}方法<br> * 用于测试{@link NoFileLocation#putElementLocation(String, ByType, String)}方法<br>
*/ */
@ -89,4 +83,87 @@ public class NoFileLocationTest {
public void putTempletTest() { public void putTempletTest() {
test.putTemplet("模板1", "//*[text()='${name}']"); 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"));
}
} }