soapui接口测试-常用的grovvy脚本

1.参数处理


1.1 从上个步骤取到参数,并进行处理

def paraName1= context.expand( '${DataSource#para1}' )
def paraName2=(paraName1.toInteger()/1000000).toString()

1.2 将处理好的参数设置到属性中

testRunner.testCase.setPropertyValue( "paraName2",paraName2)

1.3 将处理好的参数设置到后面的请求字段中

testRunner.testCase.testSteps["stepName"].setPropertyValue("para",paraName2)

2.xml格式取参数



def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def responseAsXml = context.expand( '${JDBC Request#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/para1[1]}' )
log.info "responseAsXml: "+responseAsXml
def holder = groovyUtils.getXmlHolder(responseAsXml)
def trade_no = holder.getNodeValue( "//para1[1]/para2[1]/para3[1]" )

3.json格式取参数



import groovy.json.JsonSlurper

def paraName1= context.expand( '${para1#Response#//para2[1]/para3[1]}' )
log.info "paraName1"+paraName1
def paraName2=getParaFmJson("paraName2",paraName1)
log.info "paraName2:"+paraName2
testRunner.testCase.setPropertyValue( "paraName2",paraName2)

def static String getParaFmJson( String para, String inputJson ) {
def slurper = new JsonSlurper()
def re = slurper.parseText(inputJson)
def result
if(para == "AAA")
{ result = re.AAA}
if(para == "BBB")
{ result = re.BBB}
return result
}

4.检验预期值和实际值是否相同



import groovy.json.JsonSlurper

def testResult = context.expand( '${stepName#Response#//para1[1]/para2[1]}' )
def hopeResult = context.expand( '${DataSource#hope_result}' )

def rslt = checkResult( testResult, hopeResult )

testRunner.testCase.testSteps["DataSink"].setPropertyValue("result",rslt)
testRunner.testCase.testSteps["DataSink"].setPropertyValue("hopeResult",hopeResult)

def static String checkResult( String testResult,String hopeResult ) {
//判断测试结果
if( testResult == hopeResult )
{
//符合预期结果,或者返回固定的错误消息
//log.info "testResult: " + testResult
return "OK"
}
else if( testResult.contains("AAA") //只抽取错误码开始的部分
|| testResult.contains("BBB")
)
{
return "AAA BBB OK"
}
else
{
return "NG"
}
}
}

5.常用的取参数格式:


5.1 从数据源中取

${DataSource#parmName}

5.2 从其他接口的返回信息中取

${stepName#Response#//para1[1]/para2[1]}

5.3 从测试用例属性中取值

${#TestCase#pro1}

5.4 从测试集属性中取值

${#TestSuite#pro1}

5.5 从公用的groovy方法中生成的值中取

${=className.methodName()}

6.取测试用例,测试集,测试项目,全局属性的字段值



def testCaseProperty = testRunner.testCase.getPropertyValue( "MyProp" )
def testSuiteProperty = testRunner.testCase.testSuite.getPropertyValue( "MyProp" )
def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue( "MyProp" )
def globalProperty = com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "MyProp" )

7.设置测试用例,测试集,测试项目,全局属性的字段值



testRunner.testCase.setPropertyValue( "MyProp", someValue )
testRunner.testCase.testSuite.setPropertyValue( "MyProp", someValue )
testRunner.testCase.testSuite.project.setPropertyValue( "MyProp", someValue )
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue( "MyProp", someValue )

8.可以将通用信息写成一个类,放在soapui安装目录的bin目录下


我的路径:C:\Program Files\SmartBear\SoapUI-Pro-5.1.2\bin\scripts


soapui接口测试-常用的grovvy脚本_第1张图片
Paste_Image.png

这样添加的脚本步骤就可以直接调用里面的方法了,
放在公用脚本路径的NewTrade.groovy 源文件如下:


package newTrade
import groovy.json.JsonSlurper

class NewTrade {

// 在json串中找参数为para的值
def static String getParaFmJson( String para, String inputJson ) {

def slurper = new JsonSlurper()
def re = slurper.parseText(inputJson)
def result
if(para == "AAA")
{ result = re.AAA}
if(para == "BBB")
{ result = re.BBB}
return result
}


// 比较测试结果和预期结果是否相等,相等返回OK,不相等查看是够包含什么信息,进行提示
def static String checkResult( String testResult,String hopeResult ) {
//判断测试结果
if( testResult == hopeResult )
{
//符合预期结果,或者返回固定的错误消息
//log.info "testResult: " + testResult
return "OK"
}
else if( testResult.contains("AAA") //header部分的检查,只抽取错误码开始的部分
|| testResult.contains("BBB") //header部分的检查,只抽取错误码开始的部分
)
{
return "AAA BBBOK"
}
else
{
return "NG"
}
}
}


改造上面第3项,json取参数:

def paraName2=getParaFmJson("paraName2",paraName1)

改为取公用bin目录下的groovy脚本的方法:

def paraName2=NewTrade.getParaFmJson("paraName2",paraName1)

改造上面第4项,检验预期值和实际值是否相同:

def rslt = checkResult( testResult, hopeResult )

改为取公用bin目录下的groovy脚本的方法:

def rslt = NewTrade.checkResult( testResult, hopeResult )

你可能感兴趣的:(soapui接口测试-常用的grovvy脚本)