- 按照自动化测试分层实现的原理,每一层的脚本实现都要进行参数化,自动化的目标就是要实现脚本代码与测试数据分离。
- 当测试数据进行调整的时候不会对脚本的实现带来震荡,从而提高脚本的稳定性与灵活度,降低脚本的维护成本。
- Soapui最为强大的参数化就是支持groovy脚本的参数化或者java代码,任何无法实现的地方都可以考虑脚本的扩展。
1.Soapui的常用参数化类型
image.png2.Soapui参数化操作方式
菜单路径:Test Steps->Add Step(右键菜单)->Data Source
image.png
3.Txt 文件参数化
-
新建txt文件
-
文件内容
数据之间要用逗号做分隔符,否则soapui没办法区分数据
image.png
-
选择数据源类型与文件
1.增加参数
2.选择数据类型
3.选择数据文件
4.数据的分隔符
image.png
4.Excel文件参数化
-
新建excel文件
-
编辑文件内容
-
选择数据源类型与文件
1.增加参数
2.选择数据类型
3.选择数据文件
4.起始单元格
image.png
5.Groovy脚本参数化
groovy是基于java语言的脚本语 言,语法结构与java雷同,或者直接写java代码也是可以的
-
假设我们要通过接口创建客户
要求:姓名,手机号码,email不能重复
1.随机生成姓名的代码
public List<String> RandomUserName(int RandomUserName) {
String[] FirstName = ["赵","钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
"何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎",
"鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷"];
List<String> randomName = new ArrayList<>();
for (int i = 1; i <= RandomUserName; i++) {
Random randomFirstName = new Random();
int index = randomFirstName.nextInt(FirstName.length - 1);
String name = FirstName[index];
if(randomFirstName.nextBoolean()){
name += getChinese()+getChinese();
randomName.add(name);
}
else{
name += getChinese();
randomName.add(name);
}
}
return randomName;
}
public static String getChinese() {
String str = null;
int highPos, lowPos;
Random randomChinese = new Random();
highPos = (176 + Math.abs(randomChinese.nextInt(55)))
randomChinese = new Random();
lowPos = 161 + Math.abs(randomChinese.nextInt(94))
byte[] bArr = new byte[2];
bArr[0] = (new Integer(highPos)).byteValue();
bArr[1] = (new Integer(lowPos)).byteValue();
try {
str = new String(bArr, "GB2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
}
2.随机生成手机号的代码
public List<String> RandomTelNum(int RandomTel) {
List<String> RandomTelNum = new ArrayList<>();
for (int i = 0; i < RandomTel; i++) {
String TelNumHead = "170";
int min = 10000000;
int max = 99999999;
Random randomTelLast = new Random();
int s = randomTelLast.nextInt(max) % (max - min + 1) + min;
String telNum = TelNumHead + Integer.toString(s);
RandomTelNum.add(telNum);
}
return RandomTelNum;
}
3.随机生成Email的代码
public List<String> RandomEmail(int RandomEmail) {
List<String> randomEmail = new ArrayList<>();
for (int i = 0; i < RandomEmail; i++) {
String[] randomName = "ab,bc,cd,de,ef,fg,gh,hi,ij,jk,kl,lm,mn,no,op,pq,qr,rs,st,tu,uv,vw,wx,xy,yz,za".split(",");
Random randomletter = new Random();
int index = randomletter.nextInt(26);
String EmailName = randomName[index] +randomName[index/3]+ System.currentTimeMillis();
EmailName += getemail_suffix();
randomEmail.add(EmailName);
}
return randomEmail;
}
public static String getemail_suffix() {
String[] email_suffix = "@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.net,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn".split(",");
String str;
Random randomEmail_suffix = new Random();
int index = randomEmail_suffix.nextInt(email_suffix.length - 1);
str = email_suffix[index];
return str;
}
-
运行的效果
-
代码产生的数据与变量关联
//随机生成姓名
println(RandomUserName(1)[0]);
//代码产生的数据与变量关联
result["name"]=RandomUserName(1)[0]
//随机生成邮箱
println(RandomEmail(1)[0]);
//代码产生的数据与变量关联
result["email"]=RandomEmail(1)[0]
//随机生成手机号码
println(RandomTelNum(1)[0]);
//代码产生的数据与变量关联
result["mobile"]=RandomTelNum(1)[0]
6.JDBC参数化
当接口产生的数据需要从数据库读取的时候我们可以连接数据对数据查询进行数据关联,soapui是可以链接多种数据库,我们使用mysql做演示
-
mysql驱动配置
1.下载mysql驱动的jar包
http://pan.baidu.com/s/1ge5BLhL
2.拷贝驱动jar到如下路径(soapui的安装路径)
C:\Program Files\SmartBear\SoapUI-Pro-5.1.2\jre\lib\ext
-
配置数据源
数据库连接成功,说明数据源已经配置好
image.png
-
编辑SQL
select userName,passWord from user
7.参数关联到Request
这一步虽然比较简单,但是至关重要,否则参数化是体现不出来的
image.png
网友评论