Java接口自动化测试框架系列(四)TestNG测试数据参数化与加密方法
一、测试数据参数化
TestNG参数化大致有两种方式
- 使用@Paramenters读取xml中的参数
- 使用@DataProvider注解传参
由于数据是保存到excel中,所以使用@DataProvider注解来进行测试参数的取值
通过Java接口自动化测试框架系列(二)表格设计与数据读取来进行excel中数据的读取
//配置文件取值
@DataProvider(name = "host")
public Object[][] parameter() throws IOException, BiffException {
//citydata文件名
//city sheet页名称
ReadCSV e=new ReadCSV("citydata", "city");
return e.getExcelData();
}
读取到excel中的数据后,在case中使用dataProvider进行引用,读取到的数据放入HashMap中
@Test(dataProvider = "host")
public void case1(HashMap<String, String> citydata) {
//需要哪个列的值,直接get获取即可
System.out.println(citydata.get("city_code"));
}
二、加密方法
由于接口验签与加密的需要,加密方法是SM3、SM4、MD5、Beas64等。
以MD5加密进行举例,需要什么加密方法就新增对应的加密方法,需要使用的时候进行调用即可。
package com.Auto_Test.utils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;
public class Md5Util {
private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
/**
* 生成 MD5
*
* @param data 待处理数据
* @return MD5结果
*/
public static String MD5(String data) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] array = md.digest(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
}
/**
* MD5加密方法
*
* @param content 加密内容
* @param type 大小写
* @return
*/
public static String encrypt(String content, String type) {
char encryptChar[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
byte[] contentBytes = content.getBytes();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(contentBytes);
byte[] mdBytes = md.digest();
int mdBytesLength = mdBytes.length;
char mdlengthChar[] = new char[mdBytesLength * 2];
int k = 0;
for (int i = 0; i < mdBytesLength; i++) {
byte byte0 = mdBytes[i];
mdlengthChar[k++] = encryptChar[byte0 >>> 4 & 0xf];
mdlengthChar[k++] = encryptChar[byte0 & 0xf];
}
if ("UCase".equals(type)) {
return new String(mdlengthChar).toUpperCase();
} else if ("LCase".equals(type)) {
return new String(mdlengthChar);
} else {
return new String(mdlengthChar);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String encode(String origin, String charsetname) {
String resultString = null;
resultString = new String(origin);
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
if (charsetname == null || "".equals(charsetname)) {
resultString = Hex.encodeHexString(md.digest(resultString.getBytes()));
} else {
try {
resultString = Hex.encodeHexString(md.digest(resultString.getBytes(charsetname)));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
return resultString;
}
public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
/**
* J 转换byte到16进制
*
* @param b
* @return
*/
private static String byteToHexString(byte b) {
int n = b;
if (n < 0) {
n = 256 + n;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static void main(String[] args){
String s = encrypt("123456","LCase");
String old = "e10adc3949ba59abbe56e057f20f883e";
System.out.println(s.equals(old));
}
}
需要此项目的可以加我微信15224918643获取,也可以一起交流技术。
欢迎打扰
浙公网安备 33010602011771号