目标:通过读取多个excel用例,自动创建Test
方法:根据官网提供的注解@Factory的用法,来创建多个测试类
工程结构:
image.png1.首先在工程目录下创建一个caseexcel文件夹来存放用例
image.png2.遍历文件夹获取用例文件
static public ArrayList<String> getCasePathList() {
ArrayList<String> files = new ArrayList<String>();
File file = new File("caseexcel/");
File[] tempList = file.listFiles();
if (tempList.length > 0) {
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println("文 件:" + tempList[i]);
files.add(tempList[i].toString());
}
if (tempList[i].isDirectory()) {
// System.out.println("文件夹:" + tempList[i]);
}
}
} else {
throw new NullPointerException("没有找到case");
}
return files;
}
3.测试类
将上一次测试类中的各个方法copy过来
新增属性publicCaseList、caseList用来接收测试用例
package com.testng.testng;
import java.util.List;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import Util.CaseModel;
import Util.CaseType;
import Util.Driver;
import Util.StepModel;
import Util.WebElementZ;
public class WebTest {
private List<CaseModel> publicCaseList;//公共用例
private List<CaseModel> caseList;//普通用例
//构成方法;
@SuppressWarnings("static-access")
public WebTest(List<CaseModel> publicCaseList,List<CaseModel> caseList) {
this.publicCaseList = publicCaseList;
this.caseList = caseList;
}
@Test(dataProvider = "dp")
public void f(CaseModel caseModel) {
System.out.println("执行了用例" + caseModel);
doCase(caseModel);
}
@AfterClass
public void AfterClass() {
Driver.getChromeDriver().quit();
Driver.setChromeDriver(null);
}
@BeforeClass
public void BeforeClass() {
}
@DataProvider
public Object[] dp() {
//获取用例
int size = caseList.size();
Object[] objects = new Object[size];
for (int i = 0; i < objects.length; i++) {
objects[i] = caseList.get(i);
}
return objects;
}
//根据casel模型执行用例
public void doCase(CaseModel caseModel) {
for (int i = 0; i < caseModel.getStepModels().size(); i++) {
StepModel stepModel = caseModel.getStepModels().get(i);
System.out.println(stepModel);
String object = stepModel.getObject();
if (stepModel.getPrecondition() != null) {
//执行前置条件
for (CaseModel caseModel2 : publicCaseList) {
if (stepModel.getPrecondition().equals(caseModel2.getCaseName())) {
//根据casename查找到说要执行的公共用例
doCase(caseModel2);
break;
}
}
}
if (object.equals("driver")) {
Driver.action(stepModel);
} else {
//懒得写elseif。这里假装判断了是element
WebElementZ.ElementAction(stepModel);
}
}
}
}
4.根据用例个数生成多个测试类对象
@Factory
public Object[] createInstances() {
List<String> pathList = getCasePathList();//获取测试用例文件
Object[] result = new Object[pathList.size()];
for (int i = 0; i < pathList.size(); i++) {
//生成测试类对象,将公共用例与普通用例解析后传入对象
CaseModel.getCaseList(pathList.get(i), CaseType.publicCase);
result[i] = new WebTest(CaseModel.getCaseList(pathList.get(i), CaseType.publicCase),
CaseModel.getCaseList(pathList.get(i), CaseType.ordinaryCase));
}
return result;
}
5.在testng.xml中添加class
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="com.testng.testng.WebTestFactory"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
完事
6.以下官网内容
5.8 - 工厂
工厂创建了10个测试类@Factory
public class WebTestFactory {
@Factory
public Object[] createInstances() {
Object[] result = new Object[10];
for (int i = 0; i < 10; i++) {
result[i] = new WebTest(i * 10);
}
return result;
}
}
测试类
public class WebTest {
private int m_numberOfTimes;
public WebTest(int numberOfTimes) {
m_numberOfTimes = numberOfTimes;
}
@Test
public void testServer() {
for (int i = 0; i < m_numberOfTimes; i++) {
// access the web page
}
}
}
通过testng.xml调用,添加工厂WebTestFactory
<class name="WebTestFactory" />
通过代码执行
TestNG testNG = new TestNG();
testNG.setTestClasses(WebTestFactory.class);
testNG.run();
网友评论