·测试方法:
Method:1) 手动打开seleniumserver,然后用junit运行BaseExcelSheetTestCase类。
Method:2) 打开cmd,进入pom.xml所在文件夹,运行mvnclean integration-test命令。
·工作流程:(入口程序BaseExcelSheetTestCase)
1)根据Applicationcontext.xml,spring完成对象的初始化和注入工作。(如初始化SeleniumManager,Executor,Functions,ReportProcessor,BaseDaoImpl等)
2)SeleniumManager注入完成后根据参数启动selenium并打开测试网页。
3)Executor根据传入excel每行相应的action,调用Functions或者Selenium中的方法进行对应测试工作。
4)ReportProcessor将测试的结果记录下来并生成报表
5)BaseDaoImpl将每条测试结果插入数据库保存
6)CloseSelenium.
· 文件及路径介绍:(maven项目目录结构)
1)user.dir/src/main/java à 包含所有逻辑的源代码
2)user.dir/src/test/java à 测试代码路径
3)user.dir/src/test/resourcesà Applicationcontext.xml、testcates/、jrxml/、reports/
4)user.dir/ à pom.xml
5)user.dir/bin à class 文件
关于测试工具可以用TestNG或者JUnit.
source code:
---------------------------------------Maven中的测试类------------------------
package com.citivelocity.junit;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;
import com.citivelocity.functions.SeleniumManager;
public class BaseExcelSheetTestCase extends TestCase {
// Port is not currently in use
private ApplicationContext context = newClassPathXmlApplicationContext(
new String[]{ "applicationContext.xml" });
@Override
protected void setUp() {
}
@Override
protected void tearDown() throws Exception{
super.tearDown();
}
@Test
public void testExcelSheet() {
SeleniumManager testManager =(SeleniumManager) context
.getBean("seleniumManager");
testManager.execute();
}
}
---------------------------------------------------------------------------------------
packagecom.citivelocity.functions;
importjava.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
importorg.apache.commons.lang.StringUtils;
importcom.citivelocity.springJDBC.BaseDaoImpl;
import com.citivelocity.utils.ExcelTestCasesParser;
import com.citivelocity.utils.TestCaseResultEntity;
import com.citivelocity.utils.TestCaseStepEntity;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleniumException;
public class Executor {
private final String LOCATOR_REGEX =".*\\(.*\\)";
private final String RUNWHEN_BOTH = "BOTH";
private String report;
private String jrxml;
private Functions functions;
private Map<Integer,TestCaseStepEntity> testCaseStepEntityMap;
private Map<String,String> locatorActionMap;
private SeleniumManager seleniumManager;
private ReportProcessor reportProcessor;
private BaseDaoImpl baseDaoImpl;
private Map<String,Method> seleniumMethodMap = newTreeMap<String, Method>();
private Map<String,Method> functionMethodMap = newTreeMap<String, Method>();
privateList<TestCaseResultEntity> dataList =newArrayList<TestCaseResultEntity>();
publicExecutor() {
functionMethodMap =getMethodMap(Functions.class);
seleniumMethodMap =getMethodMap(Selenium.class);
printAllMethods();
}
protected voidexecute(String strTestCaseFile) {
testCaseStepEntityMap= ExcelTestCasesParser.generate(strTestCaseFile);
System.out.println("Retreivingtestcases with size "
+testCaseStepEntityMap.size());
Iterator<Integer>it = testCaseStepEntityMap.keySet().iterator();
long beforeTestRun =System.currentTimeMillis();
while (it.hasNext()) {
runTestCase(testCaseStepEntityMap.get(it.next()));
}
long endTestRun =System.currentTimeMillis();
String duration =getDuration(beforeTestRun, endTestRun);
try {
// Generatereport
Map params =new HashMap();
StringexcelName = getReportName(seleniumManager
.getStrTestCaseFile());
params.put("Duration",duration);
report =String.format(report, excelName);
reportProcessor.generate(params,dataList, jrxml, report);
} catch (FileNotFoundExceptione) {
e.printStackTrace();
}
seleniumManager.seleniumClose();
}
private voidrunTestCase(TestCaseStepEntity testCase) {
//System.out.println(testCase.toString());
if(testCase.getRunWhen().equalsIgnoreCase(RUNWHEN_BOTH)
||seleniumManager.getIntExt().equalsIgnoreCase(
testCase.getRunWhen())){
StringelementLocator = fetchLocator(testCase.getElementLocator());
String action= standardize(testCase.getAction().toLowerCase());
System.out.println("-----------------elementLocator:"
+elementLocator);
System.out.println("-----------------action:" + action);
String value= testCase.getValue();
Method method= null;
TestCaseResultEntityresultEntity;
// *data forreport*
String status= "Pass";
String remark= "NA";
try {
if(functionMethodMap.containsKey(action)) {
method= functionMethodMap.get(action);
method.invoke(functions,
createParameters(method,elementLocator, value));
}else if (seleniumMethodMap.containsKey(action)) {
method= seleniumMethodMap.get(action);
method.invoke(seleniumManager.getSelenium(),
createParameters(method,elementLocator, value));
}else {
status= "Fail";
remark= "Invalid Action.";
System.out.println("Error:invalid action for test step"
+testCase.getStepNum() + ", " + action);
}
} catch(SeleniumException e) {
//*data for report
System.out
.println("Iget the SeleniumException.----------------->"
+testCase.getStepNum());
status= "Fail";
remark= e.getMessage();
} catch(InvocationTargetException e) {
System.out
.println("Iget theInvocationTargetException.----------------->"
+testCase.getStepNum()
+"message: --"
+e.getTargetException().getMessage());
status= "Fail";
remark= e.getTargetException().getMessage();
} catch(Exception e) {
System.out.println("Iget the Exception.----------------->"
+testCase.getStepNum());
status= "Fail";
remark= e.getMessage();
} finally{
//preparedata for report
resultEntity= new TestCaseResultEntity(testCase, status,
remark);
dataList.add(resultEntity);