Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 0654beb

Browse files
committed
增加测试套件中对动态参数的支持
1 parent d2c7b17 commit 0654beb

File tree

1 file changed

+111
-16
lines changed

1 file changed

+111
-16
lines changed

src/main/java/org/suren/autotest/web/framework/core/suite/SuiteRunner.java

Lines changed: 111 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,11 @@
2828
import java.net.URLDecoder;
2929
import java.util.ArrayList;
3030
import java.util.Arrays;
31+
import java.util.Collection;
3132
import java.util.Enumeration;
33+
import java.util.HashMap;
3234
import java.util.List;
3335
import java.util.Map;
34-
import java.util.concurrent.ExecutionException;
35-
import java.util.concurrent.ExecutorService;
36-
import java.util.concurrent.Executors;
37-
import java.util.concurrent.Future;
38-
import java.util.concurrent.TimeUnit;
39-
import java.util.concurrent.TimeoutException;
4036

4137
import org.dom4j.DocumentException;
4238
import org.slf4j.Logger;
@@ -47,6 +43,7 @@
4743
import org.suren.autotest.web.framework.core.ui.FileUpload;
4844
import org.suren.autotest.web.framework.core.ui.Selector;
4945
import org.suren.autotest.web.framework.core.ui.Text;
46+
import org.suren.autotest.web.framework.data.DynamicDataSource;
5047
import org.suren.autotest.web.framework.page.Page;
5148
import org.suren.autotest.web.framework.settings.SettingUtil;
5249
import org.suren.autotest.web.framework.settings.SuiteParser;
@@ -86,7 +83,11 @@ public static void main(String[] args) throws NoSuchFieldException, SecurityExce
8683
}
8784

8885
private ProgressInfo<String> progressInfo;
89-
private ExecutorService executor = Executors.newSingleThreadExecutor();
86+
// private ExecutorService executor = Executors.newSingleThreadExecutor();
87+
88+
/** 全局的参数配置 */
89+
private Map<String, Object> globalData = new HashMap<String, Object>();
90+
private static final String DATA_SOURCE_PARAM_KEY = "DATA_SOURCE_PARAM_KEY";
9091

9192
public SuiteRunner()
9293
{
@@ -106,7 +107,8 @@ public SuiteRunner(ProgressInfo<String> progressInfo)
106107
}
107108

108109
/**
109-
* 添加空白的进度保存实现
110+
* 添加空白的进度保存实现<br/>
111+
* 标识符为:emptyIdentify
110112
*/
111113
private void setEmptyProgress()
112114
{
@@ -122,7 +124,7 @@ public void setIdentify(String id){}
122124
@Override
123125
public String getIdentify()
124126
{
125-
return null;
127+
return "emptyIdentify";
126128
}
127129

128130
@Override
@@ -142,19 +144,45 @@ public void setStatus(int status){}
142144
* @throws InterruptedException
143145
* @throws SAXException
144146
*/
145-
public void runFromClasspathFile(String filePath)
147+
public void runFromClasspathFile(final String filePath)
146148
throws IOException, DocumentException, NoSuchFieldException,
147149
SecurityException, IllegalArgumentException, IllegalAccessException,
148150
InterruptedException, SAXException
149151
{
152+
if(StringUtils.isBlank(filePath))
153+
{
154+
throw new IllegalArgumentException("File path can not be empty.");
155+
}
156+
150157
SuiteParser suiteParser = new SuiteParser();
151158
ClassLoader classLoader = SuiteRunner.class.getClassLoader();
152159

160+
String targetFilePath = filePath;
161+
boolean suffixMatch = false;
162+
for(String suffix : suiteParser.getSupport())
163+
{
164+
if(filePath.endsWith(suffix))
165+
{
166+
suffixMatch = true;
167+
break;
168+
}
169+
}
170+
171+
if(!suffixMatch)
172+
{
173+
// 没有从已经支持的文件后缀中找到对应的,指定为默认的类型
174+
targetFilePath = (targetFilePath + suiteParser.getSupport().get(0));
175+
}
176+
177+
int resCount = 0;
153178
Enumeration<URL> resources = classLoader.getResources(filePath);
154179
while(resources.hasMoreElements())
155180
{
156181
URL url = resources.nextElement();
157182

183+
resCount++;
184+
progressInfo.setInfo(String.format("Prepare to run from file : [%s].", url));
185+
158186
try(InputStream input4Valid = url.openStream())
159187
{
160188
Validation.validationSuite(input4Valid);
@@ -164,9 +192,13 @@ public void runFromClasspathFile(String filePath)
164192
{
165193
Suite suite = suiteParser.parse(input);
166194

195+
progressInfo.setInfo(String.format("Parse file [%s] complete.", url));
196+
167197
runSuite(suite);
168198
}
169199
}
200+
201+
progressInfo.setInfo(String.format("All runner is done, total [%s].", resCount));
170202
}
171203

172204
/**
@@ -301,7 +333,7 @@ private void runSuite(Suite suite)
301333
{
302334
this.progressInfo.setInfo(String.format("解析元素定位配置文件[%s]!", xmlConf));
303335

304-
if(suite.getPathUrl() != null)
336+
if(suitePathUrl != null)
305337
{
306338
File patentFile = new File(URLDecoder.decode(suitePathUrl.getFile(), "utf-8"));
307339
patentFile = patentFile.getParentFile();
@@ -328,7 +360,10 @@ private void runSuite(Suite suite)
328360

329361
runSuiteWithData(settingUtil, row, pageList);
330362

331-
Thread.sleep(afterSleep);
363+
if(afterSleep > 0)
364+
{
365+
Thread.sleep(afterSleep);
366+
}
332367
}
333368
}
334369
}
@@ -346,6 +381,20 @@ private void runSuite(Suite suite)
346381
private void runSuiteWithData(SettingUtil settingUtil, int row, List<SuitePage> pageList)
347382
throws SecurityException, IllegalArgumentException, IllegalAccessException, InterruptedException
348383
{
384+
385+
Collection<DynamicDataSource> dynamicDataSourceList = settingUtil.getDynamicDataSources();
386+
for(DynamicDataSource dynamicDataSource : dynamicDataSourceList)
387+
{
388+
Object dynamicParam = globalData.get(DATA_SOURCE_PARAM_KEY);
389+
if(dynamicParam instanceof Map)
390+
{
391+
Map<String, Object> dataGlobalMap = dynamicDataSource.getGlobalMap();
392+
if(dataGlobalMap != null)
393+
{
394+
dataGlobalMap.putAll((Map<? extends String, ? extends Object>) dynamicParam);
395+
}
396+
}
397+
}
349398
settingUtil.initData(row);
350399

351400
this.progressInfo.setInfo(String.format("数据初始化完毕!共有[%s]个测试页面!", pageList.size()));
@@ -356,8 +405,15 @@ private void runSuiteWithData(SettingUtil settingUtil, int row, List<SuitePage>
356405
Page page = (Page) settingUtil.getPage(pageCls);
357406
if(page == null)
358407
{
359-
this.progressInfo.setInfo(String.format("the page[%s] is null.", pageCls));
360-
continue;
408+
new RuntimeException(String.format("Can not found page [%s].", pageCls));
409+
}
410+
411+
Object pageData = globalData.get(pageCls);
412+
if(pageData instanceof Map)
413+
{
414+
@SuppressWarnings("unchecked")
415+
Map<String, Object> pageDataMap = (Map<String, Object>) pageData;
416+
page.putAllData(pageDataMap);
361417
}
362418

363419
String url = page.getUrl();
@@ -393,7 +449,7 @@ private void runSuiteWithData(SettingUtil settingUtil, int row, List<SuitePage>
393449
* @throws IllegalAccessException
394450
* @throws IllegalArgumentException
395451
*/
396-
private void performActionList(Page page, List<SuiteAction> actionList, SettingUtil settingUtil)
452+
private void performActionList(final Page page, List<SuiteAction> actionList, SettingUtil settingUtil)
397453
throws SecurityException, InterruptedException,
398454
IllegalArgumentException, IllegalAccessException
399455
{
@@ -435,7 +491,7 @@ else if(!(pageObj instanceof Page))
435491
{
436492
e.printStackTrace();
437493

438-
throw new RuntimeException(String.format("Can not found field [%s] from class [%].", field, targetPage));
494+
throw new RuntimeException(String.format("Can not found field [%s] from class [%s].", field, targetPage));
439495
}
440496

441497
//防止一个任务长期执行
@@ -768,4 +824,43 @@ private <T> T getFieldObj(Class<T> type, Field pageField, Object instance)
768824
return null;
769825
}
770826
}
827+
828+
/**
829+
* 添加参数配置
830+
* @param key
831+
* @param value
832+
*/
833+
public void putData(String key, Object value)
834+
{
835+
globalData.put(key, value);
836+
}
837+
838+
/**
839+
* @see #putData(String, Object)
840+
* @param key
841+
* @param mapKey
842+
* @param mapValue
843+
*/
844+
@SuppressWarnings("unchecked")
845+
public void putMapData(String key, String mapKey, Object mapValue)
846+
{
847+
Object mapObj = globalData.get(key);
848+
if(!(mapObj instanceof Map))
849+
{
850+
mapObj = new HashMap<String, Object>();
851+
globalData.put(key, mapObj);
852+
}
853+
854+
((Map<String, Object>) mapObj).put(mapKey, mapValue);
855+
}
856+
857+
/**
858+
* 全局的数据源参数
859+
* @param key
860+
* @param value
861+
*/
862+
public void putSourceData(String key, Object value)
863+
{
864+
putMapData(DATA_SOURCE_PARAM_KEY, key, value);
865+
}
771866
}

0 commit comments

Comments
 (0)