Java自动化测试(登陆接口测试 14)

Java自动化测试(登陆接口测试 14)

测试用例

用例

用例2

环境

pom.xml中添加testng

1
2
3
4
5
6
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>

使用DataProvider遍历参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.zhongxin.cases;

import com.zhongxin.utils.HttpUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/*
* 注册接口测试
* */
public class RegisterCase {

@Test(dataProvider = "datas")
public void test(String url, String params) {
try {
HttpUtils.post(url, params);
} catch (Exception e) {
e.printStackTrace();
}
}

@DataProvider
public Object[] datas() {
Object[][] datas = {
{"http://api.lemonban.com/futureloan/member/register", "{\"mobile_phone\":\"13877788811\",\"pwd\":\"\"}"},
{"http://api.lemonban.com/futureloan/member/register", "{\"mobile_phone\":\"\",\"pwd\":\"12345678\"}"},
{"http://api.lemonban.com/futureloan/member/register", "{\"mobile_phone\":\"123\",\"pwd\":\"12345678\"}"},
};
return datas;
}
}

使用Excel中的数据遍历测试

回忆之前操作Excel的操作

引入POI

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>

编写一个读取excel的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.zhongxin.utils;

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExcelUtils {
public static void read() {
FileInputStream fis = null;
try {
fis = new FileInputStream("src/test/resources/cases_v1.xls");
Workbook sheets = WorkbookFactory.create(fis);
Sheet sheet = sheets.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
for (int i = 1; i <= lastRowNum; i++) {
Row row = sheet.getRow(i);
int lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellType(CellType.STRING);
String cellValue = cell.getStringCellValue();
System.out.println(cellValue + ',');
}
System.out.println();
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

结果

问题

编码复杂,按索引不方便适配后续变更

使用EasyPoi

使用文档:

http://easypoi.mydoc.io/

引入

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.0.0</version>
</dependency>

<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.0.0</version>
</dependency>

编写excel 表格映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.zhongxin.pojo;

import cn.afterturn.easypoi.excel.annotation.Excel;

/**
* excel 表格映射
*/
public class CaseInfo {

@Excel(name = "CaseId(用例编号)")
private int id;
@Excel(name = "Name(接口名)")
private String name;
@Excel(name = "Type(接口提交类型)")
private String method;
@Excel(name = "Url(接口地址)")
private String url;
@Excel(name = "Desc(用例描述)")
private String desc;
@Excel(name = "Params(参数)")
private String params;
@Excel(name = "Content-Type")
private String contentType;


public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public String getParams() {
return params;
}

public void setParams(String params) {
this.params = params;
}

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}


@Override
public String toString() {
return "CaseInfo{" +
"id=" + id +
", name='" + name + '\'' +
", method='" + method + '\'' +
", url='" + url + '\'' +
", desc='" + desc + '\'' +
", params='" + params + '\'' +
", contentType='" + contentType + '\'' +
'}';
}
}

读取excel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.zhongxin.utils;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.zhongxin.pojo.CaseInfo;

import java.io.FileInputStream;
import java.util.List;

public class ExcelUtils {
public static void main(String[] args) {
read();
}
public static void read() {
try {
// 1. excel文件流
FileInputStream fis = new FileInputStream("src/test/resources/cases_v1.xls");
// 2. easypoi 导入参数
ImportParams params = new ImportParams();
// 3. 导入
List<CaseInfo> caseInfoList = ExcelImportUtil.importExcel(fis, CaseInfo.class, params);
// 4. 关流
fis.close();
for (CaseInfo caseInfo : caseInfoList) {
System.out.println(caseInfo);
}

} catch (Exception e) {
e.printStackTrace();
}
}
}

结果

提取封装

为了可以让该方法可以读取更多的sheet对其进行提取

1
2
params.setStartSheetIndex(sheetIndex);//从第x个sheet开始读取
params.setSheetNum(sheetNum);//读取x个sheet

新写一个sheet2的映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.zhongxin.pojo;

import cn.afterturn.easypoi.excel.annotation.Excel;

public class API {
@Excel(name = "CaseId")
private int id;
@Excel(name = "Name")
private String name;
@Excel(name = "Url")
private String url;
@Excel(name = "Type")
private String method;

@Override
public String toString() {
return "API{" +
"id=" + id +
", name='" + name + '\'' +
", url='" + url + '\'' +
", method='" + method + '\'' +
'}';
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}
}

分别读取sheet1sheet2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.zhongxin.utils;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.zhongxin.pojo.API;
import com.zhongxin.pojo.CaseInfo;

import java.io.FileInputStream;
import java.util.List;

public class ExcelUtils {
public static void main(String[] args) {
List<CaseInfo> list = read(0, 1, CaseInfo.class);
List<API> list2 = read(1, 1, API.class);
for (CaseInfo caseInfo : list) {
System.out.println(caseInfo);
}
System.out.println("=======");
for (API api : list2) {
System.out.println(api);
}
}

public static List read(int sheetIndex, int sheetNum, Class clazz) {
try {
// 1. excel文件流
FileInputStream fis = new FileInputStream("src/test/resources/cases_v1.xls");
// 2. easypoi 导入参数
ImportParams params = new ImportParams();
params.setStartSheetIndex(sheetIndex);//从第x个sheet开始读取
params.setSheetNum(sheetNum);//读取x个sheet
// 3. 导入
List caseInfoList = ExcelImportUtil.importExcel(fis, clazz, params);
// 4. 关流
fis.close();
return caseInfoList;

} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

结果

重写测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.zhongxin.cases;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.zhongxin.pojo.CaseInfo;
import com.zhongxin.utils.ExcelUtils;
import com.zhongxin.utils.HttpUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.List;

/**
* 注册接口测试类型
*/
public class RegisterCase {

@Test(dataProvider = "datas")
public void test(CaseInfo caseInfo) {
try {
HttpUtils.post(caseInfo.getUrl(), caseInfo.getParams());
} catch (Exception e) {
e.printStackTrace();
}
}

@DataProvider
public Object[] datas() {
List list = ExcelUtils.read(0, 1, CaseInfo.class);
return list.toArray();
}
}

反射

反射:java代码在 「运行时」 「动态」 获取一个类的属性和方法,或者调用一个对象的属性和方法

实现反射:必须要有字节码对象

Class 字节码对象 约等于 .class 文件

拿到字节码对象就相当于拿到了整个类所有信息

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.zhongxin.reflect;

public class Student {
public String name;
private int age;

public void eat() {
System.out.println("Student.eat");
}

private void study() {
System.out.println("Student.study");
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

三种方法拿到字节码对象

1
2
3
4
5
6
7
// 1
Class clazz1 = Student.class;
// 2
Class clazz2 = s.getClass();
// 3
String className = "com.zhongxin.reflect.Student";
Class clazz3 = Class.forName(className);

创建对象

1
2
3
4
Object o = clazz3.newInstance();
System.out.println(o);
// Student{name='null', age=0}
// com.zhongxin.reflect.Student@8efb846

调用属性

1
2
3
4
Field field = clazz3.getField("name");
field.set(o, "张三");
System.out.println(o);
// Student{name='张三', age=0}

调用方法

1
2
3
Method method = clazz3.getMethod("eat");
method.invoke(o);
// Student.eat

暴力反射,获取属性和方法

1
2
3
4
Field field2 = clazz3.getDeclaredField("age");
field2.setAccessible(true);
field2.set(o, 22);
System.out.println(o);
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!