SoapUI Knowledge sharing

 How to use ramdom parameters?

Use the string below as one parameter value.

${=(int)(Math.random()*1000)}

Refer to SoupUI user guide for detail info: http://www.soapui.org/userguide/scripting.html

Use database to test in SoapUI:

 

import groovy.sql.Sql

import groovy.xml.MarkupBuilder

import org.custommonkey.xmlunit.*

 

Sql sql=context.dbConnection;

 

//read data from database

def expectedusername = sql.firstRow("select * from es_user where id='YH000001'").NAME

 

//read data from response

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def holder = groovyUtils.getXmlHolder( "Test Request#Response" )

def actualusername = holder.getNodeValue('//return/userDetailVo/userName')

 

//assertion

assert actualusername == expectedusername

 

Use data returned from above step response to the next step request

 

${addEquipmentRepair#Response#//return/stringValue}

 

Script assertions(xml generated from database, sorted response xml, compare)

 

import groovy.sql.Sql

import groovy.xml.MarkupBuilder

import org.custommonkey.xmlunit.*

 

//Conect to DB and get the nessecery data to generate the expected Xml.

//Sql sql_SIM=Sql.newInstance("jdbc:microsoft:sqlserver://10.18.5.74:1433;DatabaseName = SIM","SIMAppUser", "SIMAppUser","com.microsoft.jdbc.sqlserver.SQLServerDriver");

Sql sql_NMC = Sql.newInstance("jdbc:oracle:thin:@//oraqa3.prn.com:1521/ora10q1.prn.com","nmc3_qa", "nmc3_qa", "oracle.jdbc.driver.OracleDriver")

//Sql sql_CM = Sql.newInstance("jdbc:oracle:thin:@//oraqa3.prn.com:1521/ora10q1.prn.com","cmuser", "cmuser", "oracle.jdbc.driver.OracleDriver")

 

def writer = new StringWriter();

def xml = new groovy.xml.MarkupBuilder(writer)

xml.storeResult(){

messages(){

message(code:"CM20000", type:"INFO","Success")

}

sql_NMC.eachRow("select * from (select count(store_ID) as A from STORES where STORES.retailer_ID = 'WMT') N"){N ->

totalStores("$N.A")

}

 

stores(){

sql_NMC.eachRow("select * from (select STORES.store_ID, STORES.description as Sdescription, STORES.retailer_ID, Retailers.description as Rdescription, STORES.address_city, STORES.address_state, STORES.address_country, STORES.hostName, STORES.active from STORES, RETAILERS where STORES.retailer_ID = 'WMT' and STORES.Retailer_ID = RETAILERS.Retailer_ID) T order by store_id"){T ->

store(){

storeID("$T.store_ID")

def des

if("$T.Sdescription" == "null"){des = " "}

else des = "$T.Sdescription"

storeName(des)

retailerID("$T.retailer_ID")

retailerName("$T.Rdescription")

city("$T.address_city")

state("$T.address_state")

hostName("$T.hostname")

def tpActive = "$T.active"

def activeValue = null

if(tpActive == "1"){activeValue = "true"}

else activeValue = "false"

active(activeValue)

 

}

}

 

}

}

def expectedResult = writer.toString()

XMLUnit.setIgnoreWhitespace(true)

 

//Save the expected result to local machine

def fileName = "stores_retailer_retailerId_DB"

def filePath = "D:/PRN_CM/TestData/"

def xmlFile = filePath + fileName + ".xml"

def File = new PrintWriter (xmlFile)

File.print(expectedResult)

File.flush()

File.close()

 

//Get the actual result

def step = testRunner.testCase.testSteps["GET: stores/retailer/{retailerId}"]

def tempresult = step.testRequest.response.contentAsString

 

//define a GroovyBean

class Store {

// properties

Integer storeID

String storeName

String retailerID

String retailerName

String city

String state

String hostName

String active

}

 

def storeList = [];

//start reading xml rows by using XMLParser

def storeResult = new XmlParser().parseText(tempresult)

storeResult.stores.store.each{

//storeList.push(new Store(storeID:it.storeID.text(), storeName:it.storeName.text(), retailerID:it.retailerID.text())) // Add Store bean into the list

def TPstoreID = it.storeID.text().toInteger()

storeList << new Store(storeID:TPstoreID, storeName:it.storeName.text(), retailerID:it.retailerID.text(), retailerName:it.retailerName.text(), city:it.city.text(), state:it.state.text(), hostName:it.hostName.text(), active:it.active.text(), )

storeList.sort{it.storeID}

}

 

//generate new xml by loop the list.

def responsewriter = new StringWriter();

def newxml = new groovy.xml.MarkupBuilder(responsewriter)

newxml.storeResult(){

messages(){

message(code:"CM20000", type:"INFO","Success")

}

totalStores(storeResult.totalStores.text())

stores(){

storeList.each{SR ->

store(){

storeID("${SR.storeID}");

storeName("${SR.storeName}");

retailerID("${SR.retailerID}");

retailerName("${SR.retailerName}");

city("${SR.city}");

state("${SR.state}");

hostName("${SR.hostName}");

active("${SR.active}");

}

} //

}//stores end

}//storeResult end

def result = responsewriter.toString()

XMLUnit.setIgnoreWhitespace(true)

def fileName2 = "stores_retailer_retailerId_response"

def filePath2 = "D:/PRN_CM/TestData/"

def xmlFile2 = filePath2 + fileName2 + ".xml"

def File2 = new PrintWriter (xmlFile2)

File2.print(result)

File2.flush()

File2.close()

 

//Check the actual result by expected result, and output the test result.

def xmlDiff = new Diff(result, expectedResult)

//def xmlDiff = new DetailedDiff(new Diff(result, expectedResult))

def s = xmlDiff.toString();

File f = new File("D://PRN_CM//TestResult","API_DV_ResultCollection.txt");

if( !f.exists()){

f.createNewFile();

}

try {

FileWriter fileWriter = new FileWriter(f, true);

if (s == "org.custommonkey.xmlunit.Diff[identical]"){

fileWriter.write("API: '"+ step.name + "' DV Result is "+ "Passed/r/n");

fileWriter.write("-------------------------------------------------------------------------------/r/n/r/n");

}

else{

fileWriter.write("API: '"+ step.name + "' DV Result is "+ "Failed/r/n/r/n" +s + "/r/n");

fileWriter.write("-------------------------------------------------------------------------------/r/n/r/n");

}

fileWriter.close();

} catch (IOException e) {

e.printStackTrace();

}

 

Convert duration(eg, 30000s) to specific format( HH:mm:ss.SSS)

 

def tempdurationinms = "$placement.durationinms";

def lengthvalue = Long.parseLong(tempdurationinms.toString());
Calendar c1 = Calendar.getInstance();

c1.setTimeInMillis(lengthvalue);

def sf = new SimpleDateFormat("HH:mm:ss.SSS");

sf.setTimeZone(TimeZone.getTimeZone("GMT"));

length(sf.format(c1.getTime()));

發佈了82 篇原創文章 · 獲贊 4 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章