一波小團戰!java
1.操做excel的groovy腳本linux
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腳本app
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值返回eclipse
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文件;ui
打開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放入,調用方法以下:this
在testCase中新建一個Groovy Script,而後輸入以下代碼:excel
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過程當中遇到的問題。