SOAP UI循序渐进之三(Update)
本帖最后由 开着拖拉机上班 于 2013-12-3 16:47 编辑为了测试项目的稳定性,需要实现7*24不间断的对service发出request并获取response的值,然后存储到Excel中。
以上就是需求,从今天开始把学习成果记录在这里,供大家批评指正。
首先给大家看看一个case的step有哪些:
其中looper是用来控制循环执行这些step的;Read是从excel中读取数据并写入到properties中,供发送request使用;Write用于把response中的数据写入excel存储;delay类似于性能测试中的思考时间。
以上是项目基本背景:
说重点,直接上实现loop的脚本,超级简单
import groovy.transform.Field
testRunner.testCase.setPropertyValue("RUN", '1' )
def runCycle = (testRunner.testCase.getPropertyValue("RUN")).toInteger()
def size = 3 // sets the number of loops
for( counter in 0..size-1)
{
testRunner.runTestStepByName("Groovy Script - Read") // calls the excel file read step
testRunner.runTestStepByName("Delay 1") // calls the delay step for each loop
runCycle++
testRunner.testCase.setPropertyValue("RUN",runCycle.toString())
}
--------------------------------------------------------------------------------------------------------
之二:把excel里的数据取到SOAP UI的properties中,实现方式:
import groovy.transform.Field
import jxl.*
import jxl.write.*
Workbook workbook1 = Workbook.getWorkbook(new File("D:\\FAL\\SOAPUI_Automation\\output.xls"))
def sheetCounter = workbook1.getNumberOfSheets();
sheetNameArray = workbook1.getSheetNames();
def sNumber = 0
def accountId = new Object
def tcuId = new Object
def vin = new Object
def userId = new Object
for( c in 0..sheetCounter-1) // loops through the excel sheets
{
readingSheetName = sheetNameArray
//log.info "readingSheetName: ${readingSheetName}"
Sheet sNC = workbook1.getSheet(sheetNameArray)
Cell A2 = sNC.getCell(0,1) // getCell(column,row) count from 0,0 = cell A1
Cell B2 = sNC.getCell(1,1)
Cell C2 = sNC.getCell(2,1)
Cell D2 = sNC.getCell(3,1)
accountId = "AccountId${sNumber}" //can i delete ?
accountId = A2.getContents()
//log.info accountId
//log.info accountId
tcuId = "TCUID${sNumber}" //can i delete ?
tcuId = B2.getContents()
vin = "VIN${sNumber}" //can i delete ?
vin = C2.getContents()
userId = "UserId${sNumber}" //can i delete ?
userId = D2.getContents()
def propswrite = new java.util.Properties(); // sets the properties step for use with the soap request
propswrite = testRunner.testCase.getTestStepByName("Properties 1");
propswrite.setPropertyValue("AccountId",accountId);
propswrite.setPropertyValue("TCUID",tcuId);
propswrite.setPropertyValue("VIN",vin);
propswrite.setPropertyValue("UserId",userId);
propswrite.setPropertyValue("SheetName",readingSheetName);
testRunner.runTestStepByName("GetVehicleParkedLocation"); // calls the soap test step
testRunner.runTestStepByName("Groovy Script - Write"); // calls the excel file write step
sNumber++
}
workbook1.close() 之三:把Response中的数据写入Excel
import groovy.transform.Field
import java.io.File;
import java.util.*;
import jxl.*;
import jxl.write.*;
def propsread = new java.util.Properties();
propsread = testRunner.testCase.getTestStepByName("Properties 1");
def writingSheetName = propsread.getPropertyValue("SheetName")
//log.info "writingSheetName: ${writingSheetName}"
def flag = (testRunner.testCase.getPropertyValue("RUN")).toInteger();
//log.info "initialWritingRow: ${flag}"
Workbook workbook = Workbook.getWorkbook(new File("D:\\FAL\\SOAPUI_Automation\\GetVehicleParkedLocation.xls"))
WritableWorkbook copy = Workbook.createWorkbook(new File("D:\\FAL\\SOAPUI_Automation\\GetVehicleParkedLocation.xls"), workbook) // creates a copy to append data
WritableSheet sheet = copy.getSheet(writingSheetName)
//log.info(sheet.isHidden())
//log.info sheet.getRows()
//log.info sheet.getColumns()
flag = sheet.getRows().toInteger()
//log.info "actualWritingRow: ${flag}"
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def responseAsXml = context.expand( '${GetVehicleParkedLocation#Response}' ) // gets the xml response
def holderResponse = groovyUtils.getXmlHolder(responseAsXml)
if( holderResponse !=null )
{
// declare namespace according to xml request
holderResponse.declareNamespace('veh', 'http://ns.hughestelematics.com/Gateway/service/VehicleParkedLocationService')
holderResponse.declareNamespace('v1', 'http://xmlns.hughestelematics.com/Gateway/VehicleParkedLocation/V1')
holderResponse.declareNamespace('v11', 'http://xmlns.hughestelematics.com/Gateway/Common/Header/V1')
def E2_Timestamp
E2_Timestamp = holderResponse.getNodeValue("//veh:GetVehicleParkedLocationResponse/v1:GetVechicleLocationResponse/v1:Header/v11:Timestamp")
//log.info E2_Timestamp
def F2_Altitude
F2_Altitude = holderResponse.getNodeValue("//veh:GetVehicleParkedLocationResponse/v1:GetVechicleLocationResponse/v1:Data/v1:Altitude")
def G2_Course
G2_Course = holderResponse.getNodeValue("//veh:GetVehicleParkedLocationResponse/v1:GetVechicleLocationResponse/v1:Data/v1:Course")
def H2_Latitude
H2_Latitude = holderResponse.getNodeValue("//veh:GetVehicleParkedLocationResponse/v1:GetVechicleLocationResponse/v1:Data/v1:Latitude")
def I2_Longitude
I2_Longitude = holderResponse.getNodeValue("//veh:GetVehicleParkedLocationResponse/v1:GetVechicleLocationResponse/v1:Data/v1:Longitude")
def J2_PrecisionInMeters
J2_PrecisionInMeters = holderResponse.getNodeValue("//veh:GetVehicleParkedLocationResponse/v1:GetVechicleLocationResponse/v1:Data/v1:LocationPrecision/v1:PrecisionInMeters")
def K2_PrecisionTrueness
K2_PrecisionTrueness = holderResponse.getNodeValue("//veh:GetVehicleParkedLocationResponse/v1:GetVechicleLocationResponse/v1:Data/v1:LocationPrecision/v1:PrecisionTrueness")
def L2_statusReceivedTimeStamp
L2_statusReceivedTimeStamp = holderResponse.getNodeValue("//veh:GetVehicleParkedLocationResponse/v1:GetVechicleLocationResponse/v1:Data/v1:statusReceivedTimeStamp")
Label label_Timestamp = new Label(4, flag, E2_Timestamp); // fills excel cells with the results
sheet.addCell(label_Timestamp);
Label label_Altitude = new Label(5, flag, F2_Altitude); //column=0=A,row=0=1, cell A1
sheet.addCell(label_Altitude);
Label label_Course = new Label(6, flag, G2_Course);
sheet.addCell(label_Course);
Label label_Latitude = new Label(7, flag, H2_Latitude);
sheet.addCell(label_Latitude);
Label label_Longitude = new Label(8, flag, I2_Longitude);
sheet.addCell(label_Longitude);
Label label_PrecisionInMeters = new Label(9, flag, J2_PrecisionInMeters);
sheet.addCell(label_PrecisionInMeters);
Label label_PrecisionTrueness = new Label(10, flag, K2_PrecisionTrueness);
sheet.addCell(label_PrecisionTrueness);
Label label_statusReceivedTimeStamp = new Label(11, flag, L2_statusReceivedTimeStamp);
sheet.addCell(label_statusReceivedTimeStamp);
}
def logArea = com.eviware.soapui.SoapUI.logMonitor.getLogArea("SoapUI log"); // reads the soapui log
def M2_soapuiLog
if( logArea !=null )
{
def model = logArea.model
if( model.size > 0 )
M2_soapuiLog = (model.getElementAt(model.size-1)).toString()
//log.info M2_soapuiLog
Label label_soapuiLog = new Label(12, flag, M2_soapuiLog);
sheet.addCell(label_soapuiLog);
}
copy.write()
copy.close()
workbook.close() 看看,谢谢分享 都是代码,,看不懂,。。
页:
[1]