创建读取无文件存储元素信息方法

This commit is contained in:
彭宇琦 2020-10-30 19:08:26 +08:00
parent 5c92dab0ed
commit bfb53a51a5
2 changed files with 257 additions and 0 deletions

View File

@ -0,0 +1,165 @@
package pres.auxiliary.work.selenium.location;
import java.util.ArrayList;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import pres.auxiliary.work.selenium.element.ElementType;
/**
* <p><b>文件名</b>NoFileLocation.java</p>
* <p><b>用途</b>
* 提供在不编写元素定位文件的情况下读取元素定位方法通过该类可存储一些临时添加的元素定位
* 方式并在编写脚本过程中进行调用
* </p>
* <p><b>编码时间</b>2020年10月29日上午8:38:24</p>
* <p><b>修改时间</b>2020年10月29日上午8:38:24</p>
* @author 彭宇琦
* @version Ver1.0
*/
public class NoFileLocation extends AbstractLocation implements WriteLocation, WriteTempletLocation {
/**
* 存储json的读取方式
*/
private JsonLocation jsonLocation;
/**
* 用于存储当前通过方法添加的元素信息
*/
private JSONObject nowLocationJson;
/**
* 用于存储通过新增方法添加的元素信息
*/
private JSONObject newLocationJson;
/**
* 构造类对象
*/
public NoFileLocation() {
//初始化json参数
nowLocationJson = new JSONObject();
nowLocationJson.put(JsonLocation.KEY_TEMPLETE, new JSONObject());
nowLocationJson.put(JsonLocation.KEY_ELEMENT, new JSONObject());
//克隆当前元素json信息
newLocationJson = (JSONObject) nowLocationJson.clone();
//初始化json定位类对象
jsonLocation = new JsonLocation(nowLocationJson.toJSONString());
}
@Override
public void putElementLocation(String name, ByType byType, String value) {
//获取元素对象
JSONObject elementJson = getElementJson(name);
//获取对象的location键值若键值不存在则添加键值
if (!elementJson.containsKey(JsonLocation.KEY_LOCATION)) {
elementJson.put(JsonLocation.KEY_LOCATION, new JSONArray());
}
//封装定位方式
JSONObject locationJson = new JSONObject();
locationJson.put(JsonLocation.KEY_TYPE, byType.getValue());
locationJson.put(JsonLocation.KEY_VALUE, value);
//向定位方式组中添加定位方式
elementJson.getJSONArray(JsonLocation.KEY_LOCATION).add(locationJson);
}
@Override
public void putElementTempletLocation(String name, ByType byType, String templetId) {
//获取元素对象
JSONObject elementJson = getElementJson(name);
//获取对象的location键值若键值不存在则添加键值
if (!elementJson.containsKey(JsonLocation.KEY_LOCATION)) {
elementJson.put(JsonLocation.KEY_LOCATION, new JSONArray());
}
//封装定位方式
JSONObject locationJson = new JSONObject();
locationJson.put(JsonLocation.KEY_TEMP, templetId);
locationJson.put(JsonLocation.KEY_TYPE, byType.getValue());
//向定位方式组中添加定位方式
elementJson.getJSONArray(JsonLocation.KEY_LOCATION).add(locationJson);
}
@Override
public void putElementType(String name, ElementType elementType) {
//获取元素对象并存储键值
getElementJson(name).put(JsonLocation.KEY_TYPE, elementType.getValue());
}
@Override
public void putIframeNameList(String name, String iframeName) {
//获取元素对象并存储键值
getElementJson(name).put(JsonLocation.KEY_IFRAME, iframeName);
}
@Override
public void putWaitTime(String name, long waitTime) {
//获取元素对象并存储键值
getElementJson(name).put(JsonLocation.KEY_WAIT, waitTime);
}
@Override
public void putTemplet(String templetId, String templetValue) {
newLocationJson.getJSONObject((JsonLocation.KEY_TEMPLETE)).put(templetId, templetValue);
}
@Override
public void putTempletReplaceKey(String name, String templetId, String key, String value) {
// TODO Auto-generated method stub
}
@Override
public ArrayList<ByType> findElementByTypeList(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public ArrayList<String> findValueList(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public ElementType findElementType(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public ArrayList<String> findIframeNameList(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public long findWaitTime(String name) {
// TODO Auto-generated method stub
return 0;
}
/**
* TODO 测试使用返回元素信息
*/
public String getJson() {
return newLocationJson.toJSONString();
}
/**
* 用于返回元素名称对应的元素json对象
* @param name 元素名称
* @return 元素名称对应的json对象
*/
private JSONObject getElementJson(String name) {
//获取元素对象若元素对象不存在则创建相应的元素json并返回
if (!newLocationJson.getJSONObject(JsonLocation.KEY_ELEMENT).containsKey(name)) {
newLocationJson.getJSONObject(JsonLocation.KEY_ELEMENT).put(name, new JSONObject());
}
return newLocationJson.getJSONObject(JsonLocation.KEY_ELEMENT).getJSONObject(name);
}
}

View File

@ -0,0 +1,92 @@
package pres.auxiliary.work.selenium.location;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import pres.auxiliary.work.selenium.element.ElementType;
public class NoFileLocationTest {
/**
* 定义类
*/
NoFileLocation test;
/**
* 初始化数据
*/
@BeforeClass
public void init() {
test = new NoFileLocation();
}
@AfterClass
public void showData() {
System.out.println(test.getJson());
}
/**
* 用于测试{@link NoFileLocation#putElementLocation(String, ByType, String)}方法<br>
*/
@Test
public void putElementLocationTest() {
test.putElementLocation("测试控件1", ByType.NAME, "测试控件11111");
test.putElementLocation("测试控件2", ByType.CLASSNAME, "test_class");
test.putElementLocation("测试控件3", ByType.CSS, "html body div");
test.putElementLocation("测试控件4", ByType.ID, "test");
test.putElementLocation("测试控件5", ByType.LINKTEXT, "测试控件");
test.putElementLocation("测试控件6", ByType.TAGNAME, "span");
test.putElementLocation("测试控件7", ByType.XPATH, "//*[text()='测试控件7']");
test.putElementLocation("测试控件7", ByType.ID, "test7");
test.putElementLocation("测试控件8", ByType.LINKTEXT, "测试控件");
}
/**
* 用于测试{@link NoFileLocation#putElementTempletLocation(String, ByType, String)}方法<br>
*/
@Test
public void putElementTempletLocationTest() {
test.putElementTempletLocation("测试控件8", ByType.XPATH, "模板1");
test.putElementTempletLocation("测试控件8", ByType.CLASSNAME, "模板2");
}
/**
* 用于测试{@link NoFileLocation#putElementType(String, pres.auxiliary.work.selenium.element.ElementType)}方法<br>
* 预期<br>
*
*/
@Test
public void putElementTypeTest() {
test.putElementType("测试控件8", ElementType.COMMON_ELEMENT);
}
/**
* 用于测试{@link NoFileLocation#putIframeNameList(String, String)}方法<br>
* 预期<br>
*
*/
@Test
public void putIframeNameListTest() {
test.putIframeNameList("测试控件8", "窗体1");
}
/**
* 用于测试{@link NoFileLocation#putWaitTime(String, long)}方法<br>
* 预期<br>
*
*/
@Test
public void putWaitTimeTest() {
test.putWaitTime("测试控件8", 10);
}
/**
* 用于测试{@link NoFileLocation#putTemplet(String, String)}方法<br>
* 预期<br>
*
*/
@Test
public void putTempletTest() {
test.putTemplet("模板1", "//*[text()='${name}']");
}
}