[工具使用] SOAP UI 测试

在日常开发中使用SOAP UI 测试web service 报文。

SOAP UI 基本术语

[工具使用] SOAP UI 测试_第1张图片

 

1 连接数据库

思路:通过SOAP UI的TestSuite 的"Custom Properties" 定义连接数据库的属性,如用户名、密码等。 通过groovy 调用jdbc访问数据库。注意,需要把oracle的jdbc驱动放在SAOP UI的安装目录下(SoapUI-Pro-5.1.2\lib 目录下)。

groovy 代码

import groovy.sql.Sql;
def proposalPN = testRunner.testCase.testSuite.getPropertyValue( "ProposalPrtNo" )

def IP = testRunner.testCase.testSuite.getPropertyValue( "DB_IP" )
def DBName = testRunner.testCase.testSuite.getPropertyValue( "DB_Name" )
def Port = testRunner.testCase.testSuite.getPropertyValue( "DB_Port" )
def UserName = testRunner.testCase.testSuite.getPropertyValue( "DB_UserName" )
def PassWord = testRunner.testCase.testSuite.getPropertyValue( "DB_PassWord" )

def sql = Sql.newInstance("jdbc:oracle:thin:@$IP:$Port:$DBName","$UserName","$PassWord","oracle.jdbc.driver.OracleDriver")

def isExist = 0 ;
while(isExist == 0){
	def isExistSql = " SELECT 1 FROM dual WHERE NOT EXISTS (SELECT 1 FROM lccont lcc WHERE lcc.proposalcontno = '$proposalPN')  AND NOT EXISTS (SELECT 1 FROM lbcont lbc WHERE lbc.proposalcontno = '$proposalPN')" + 
	   " AND NOT EXISTS (SELECT 1 FROM lwmission lwm WHERE lwm.processid = '0000000003' AND lwm.missionprop1 = '$proposalPN') "
	     log.info(isExistSql);
	 log.info("Try get proposalPN = "  +  proposalPN)
	sql.eachRow(isExistSql){
		isExist = 1
      // testRunner.testCase.setPropertyValue( "ContNo","${it.contno}" )
       log.info("数据库不存在proposalPN = " +proposalPN + "" )
     }
     if(isExist == 0){
     	 log.info("数据库存在proposalPN = " +proposalPN + "将+1 重新尝试" )
     	 proposalPN = proposalPN.toBigInteger() + 1;
     }
}
log.info("使用proposalPN = " +proposalPN)
 testRunner.testCase.testSuite.setPropertyValue( "ProposalPrtNo", proposalPN+"")


2 把测试中的请求和响应报文输出到系统指定目录

思路:SAOP UI 在运行Test Case 时, 会存储请求报文和响应报文的, SOAP UI 在项目级别(Project)支持事件的监听和处理。没有个Test Step 运行完毕后, 都输出自己的报文即可。

定义位置参见下图

[工具使用] SOAP UI 测试_第2张图片

groovy 脚本

import com.eviware.soapui.support.xml.XmlUtils;
def writeXMLToFile(xmlStr, filePathName){
     def fos = new FileOutputStream( filePathName , true )
     def pw = new PrintWriter( fos )
     def dom =  XmlUtils.parseXml(xmlStr) 
     XmlUtils.serializePretty(dom,  pw)  
     pw.close()
     fos.close()
}
def logOutPath = 'E:/soapuiTestResult/wy/'+testStepResult.testStep.testCase.testSuite.name+"/"+ testStepResult.testStep.testCase.name+"/"    //定义log 路径
def logFileName = testStepResult.testStep.label+".txt";
def reqFileName = testStepResult.testStep.label+"_请求报文.xml";
def rspFileName = testStepResult.testStep.label+"_响应报文.xml";
log.info("日志将被输出到:"+logOutPath)
def file = new File(logOutPath);
if (!file.exists()) {
			file.mkdirs();
}
def fos = new FileOutputStream( logOutPath + logFileName , true )
def pw = new PrintWriter( fos )
testStepResult.writeTo( pw )
pw.close()
fos.close()

log.info(testStepResult.testStep.label + "准备输出测试报文:");
def reqContent = context.getProperty("requestContent")
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
if(reqContent != null &&  !"".equals(reqContent)){
	def xmlObj =  XmlUtils.createXmlObject(reqContent)  
	def reqStr =  XmlUtils.selectFirstNodeValue(xmlObj, "//pInXmlDoc") 
	
	 	def isHasResponse = true;
	 	 def rspContent = "";
	 	try{
	 		def holder = groovyUtils.getXmlHolder(testStepResult.testStep.label + "#Response" )
	 		 rspContent = holder.getNodeValue("//serviceReturn")
	 	}catch(e){
	 		isHasResponse = false;
	 	}
	 	if(isHasResponse){
	 		     writeXMLToFile(reqStr, logOutPath + reqFileName);
                    writeXMLToFile(rspContent, logOutPath + rspFileName);
	 	}
} else {
	log.info(testStepResult.testStep.label + "无报文输出");
}


 

 

参考资料

        

               API 文档地址:http://www.soapui.org/apidocs/overview-summary.html
               官方文档:http://www.soapui.org/Scripting-Properties/scripting-and-the-script-library.html
               如果想处理XML, 可以使用com.eviware.soapui.support.xml.XMLUtils 类。
               如果想取界面上的值,参考com.eviware.soapui.model.testsuite 包

               入门文档:http://www.docin.com/p-248591553.html?bsh_bid=82185135
               soapUI入门(来自taobaoQA)不要问为什么系列4   http://www.cnblogs.com/killbug/archive/2012/04/20/2458698.html

 

你可能感兴趣的:(Java,测试)