标签:
一波小团战!
1.操作excel的groovy脚本
package pub import jxl.* import jxl.write.Label import jxl.write.WritableWorkbook class ExcelOperation { def xlsFile def workbook def writableWorkbook def ExcelOperation(){} //设置xlsFile文件路径 def ExcelOperation(xlsFile){ this.xlsFile = xlsFile } //初始化workbook def init(){ workbook = Workbook.getWorkbook(new File(xlsFile)) writableWorkbook = Workbook.createWorkbook(new File(xlsFile ), workbook) } //关闭workbook流 def close(){ writableWorkbook.write() writableWorkbook.close() workbook.close() } //获取list环境 def envlistGet(sheetName, projectName) { def list = [] init() list = envlist(workbook, sheetName, projectName) close() return list } //获取map环境 def envmapGet(sheetName, projectName) { def map = [:] init() map = envmap(workbook, sheetName, projectName) close() return map } //写入文件 def cellWriteSet(sheetName, rowName, columnName, content){ init() cellWrite(writableWorkbook, sheetName, rowName, columnName, content) close() } //获取测试环境* def envlist(Workbook workbook, sheetName, projectName) { def list = [] Sheet sheet = workbook.getSheet(sheetName) Cell[] c1 = sheet.getColumn(0) Cell[] c2 = sheet.getColumn(1) def end c1.each {it -> if(it.getContents().contains(projectName)) { end = it.row } } for(i in 0..5){ list[i] = c2[end].getContents() end-- } return list } //获取测试环境 def envmap(Workbook workbook, sheetName, projectName) { def map = [:] Sheet sheet = workbook.getSheet(sheetName) Cell[] c1 = sheet.getColumn(0) Cell[] c2 = sheet.getColumn(1) def end c1.each {it -> if(it.getContents().contains(projectName)){ end = it.row } } for(i in 0..5) { map.put(c1[end].getContents(), c2[end].getContents()) end-- } return map } //写入测试结果 def cellWrite(WritableWorkbook writableWorkbook, sheetName, rowName, columnName, content){ Sheet sheet = writableWorkbook.getSheet(sheetName) if(sheet == null){ sheet = writableWorkbook.createSheet(sheetName,2) } Cell[] c1 = sheet.getColumn(0) Cell[] c2 = sheet.getRow(0) def row, col c1.each {it -> if(it.getContents().equals(rowName)){ row = it.row } } c2.each {it -> if(it.getContents().equals(columnName)){ col = it.column } } sheet.addCell(new Label(col, row, content)) } }
这是个封装好的调用excel的类,主要看怎么读写excel,其他方面根据个人需要来修改
2.生成MD5的groovy脚本
package pub import java.security.MessageDigest class MD5 { def MD5 //空的构造方法 def MD5(){} //构造方法实现构造返回MD5值 def MD5(map, pwd) { String s = getMapString(map, pwd) String b = getMD5(s) MD5 = b.toLowerCase() } //将map按key排序,并取出value组成字符串 def getMapString(Map map, String pwd) { def tm = new TreeMap(map) //按mapkey排序 StringBuffer sb = new StringBuffer() tm.each { key,value -> sb.append"$value" } sb.append(pwd) return sb.toString() } //返回传入字符串的MD5值 def getMD5(String source) { MessageDigest md5 = MessageDigest.getInstance("MD5") md5.update(source.getBytes()) //将字节数组传递给md5 byte[] digest = md5.digest() //产生md5序列 StringBuffer sb = new StringBuffer() digest.eachByte { sb.append(String.format("%02x", it & 0xff)) //将md5值转为16进制 } return sb.toString() } }
传入Map和pwd,然后将map按key排序,把map的value连接起来生成md5值返回
3.操作txt文本
package pub import java.io.File class Text { def writeFile(fileName, context) { def file = new File(fileName) if(!file.exists()) { file.createNewFile() } file.append(new Date().format(‘yyyy-MM-dd HH-mm-ss‘), ‘UTF-8‘) file.append(‘\n‘) file.append(context, ‘UTF-8‘) file.append(‘\n‘) } }
4.调用第三方jar包
soapui可以调用第三方jar包/.class/.groovy文件;
打开soapui中lib目录(我的SoapUI-Pro-5.1.2.exe目录为C:\Program Files (x86)\SmartBear\SoapUI-Pro-5.1.2\lib),可以看到这里有很多jar文件。将第三方jar(java.jar/groovy.jar)包放进去,将以上三个脚本打成pub.jar放入,调用方法如下:
在testCase中新建一个Groovy Script,然后输入如下代码:
import pub.Text
def text = new Text()
text.writeFile("D:\1.log", "test")
执行完成后可以看到在d盘下建立了一个1.log文件,并写入test;
5.调用第三方.class文件
将得到的java.jar文件解压得到.class文件,将解压出来的文件夹及.class文件放到${soapui}/bin/scripts(我的SoapUI-Pro-5.1.2.exe目录为C:\Program Files (x86)\SmartBear\SoapUI-Pro-5.1.2\bin\scripts)目录下,调用方法和.groovy文件一致
6.调用第三方.groovy文件
将如上第三个.groovy脚本放在pub文件夹下,拷贝到${soapui}/bin/scripts(我的SoapUI-Pro-5.1.2.exe目录为C:\Program Files (x86)\SmartBear\SoapUI-Pro-5.1.2\bin\scripts)目录下,调用方法如下:
import pub.Text //如果这么写报错则改成import scripts.pub.Text
def text = new Text()
text.writeFile("D:\1.log", "test")
执行完成后可以看到在d盘下建立了一个1.log文件,并写入test;
7.再谈调用第三方jar包!
打开${soapui}/bin/ext(我的SoapUI-Pro-5.1.2.exe目录为C:\Program Files (x86)\SmartBear\SoapUI-Pro-5.1.2\bin\ext)目录有个readme.txt写着一行鸟语,大致意思是这个文件夹是存放外部jar文件的位置;
建议所有的外部文件均放在这个文件夹底下,像.class/.groovy尽量打成jar包放在ext文件夹下;之所以如此建议,是基于如下原因:
1.有些安装的soapui bin目录下并没有scripts文件夹(linux下安装或免费版soapui),当然你要手动去创建的话和安装自动生成并无差别;
2.在Linux下运行外部文件存在于scripts文件夹下的脚本会报错;
3.官方对于ext文件夹的描述为,soapui启动时首先加载外部jar包的地方;
4.groovy脚本打包时要注意,使用eclipse打包时会让我们勾选生成.class文件,不要勾选此选项,就使用原版的.groovy文件(不然运行时会报错);简单来说这个打包就跟你用压缩工具压缩然后改后缀为.jar效果是一样的。
本章到此结束, 下章介绍使用soapui过程中遇到的问题。
标签:
原文地址:http://www.cnblogs.com/mayibanjiah/p/4555236.html