SoapUI Script Library

Environment

Get active environment via groovy script

1
log.info testRunner.testCase.testSuite.project.getActiveEnvironment().getName()

Set active environment via groovy script

1
testRunner.testCase.testSuite.project.setActiveEnvironment("Live")

Get request

Get request header via messageExchange

1
def requestID = messageExchange.requestHeaders.get("X-API-RequestId")

Get test step name via messageExchange

1
def testStepName = messageExchange.modelItem.name

Get response

Get response by testRunner

1
def response = testRunner.testCase.testSteps["InitCase"].testRequest.response.contentAsString 

Get response by Context

1
2
3
// Get response
String testStepName = "Intraday Table"
def responseLive = context.expand( '${'+testStepName+'#Response}' )

Get response by messageExchange

1
def response = messageExchange.getResponseContent()

Get response header

1
def headers = messageExchange.getResponseHeaders()

Parse XML

XPath Parse XML : Get node value

1
2
3
4
5
6
7
8
import com.eviware.soapui.support.GroovyUtils
  
 //Get xmlHolder of the xml response
def groovyUtils = new GroovyUtils( context )
def xmlHolder = groovyUtils.getXmlHolder( "testStepName#ResponseAsXml" )
  
//Parse response by XPath
def data = xmlHolder .getNodeValue("//html[1]/body[1]/text()")

XPath Parse XML : Get nodes list and attributes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import com.eviware.soapui.support.GroovyUtils
   
def testStepName = "Intraday Table"
def XPath = "//B/I/I"
   
// Get response
def groovyUtils = new GroovyUtils(context)
def xmlHolder = groovyUtils.getXmlHolder(testStepName+"#ResponseAsXml")
  
// Get nodes list
def nodesArray = xmlHolder.getDomNodes(XPath)
List nodesList = nodesArray.toList()
   
for(int i=0;i<nodesList.size();i++){
    def attributes = nodesList.get(i).getAttributes()
    def attributesNumber = attributes.getLength()

XmlParser parse XML : parse xml in json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import groovy.json.JsonSlurper
   
def testStepname = "Attribution Detail"
def responseLive = context.expand( '${'+testStepName+'#Response}' )
   
def jsonLive = new JsonSlurper().parseText(responseLive)
String xmlRecordLive = jsonLive.data.data
   
def xmlParser = new XmlParser()
def xmlLive = xmlParser.parseText(xmlRecordLive)
   
def nodesArrayLive = xmlLive.Body.B.I
List nodesListLive = nodesArrayLive.toList()
int recordsNumberLive = nodesListLive.size()

Parse JSON

JsonPath Parse JSON : Get datas list

1
2
3
4
5
6
7
import com.jayway.jsonpath.*
   
def testStepName = "Holdings Scatter Plot"
def JPath = '$.Holdings[*]'
   
def response = context.expand( '${'+testStepName+'#Response}' )
def datasList = JsonPath.read(response, JPath)

JsonSlurper Parse JSON : Get data

1
2
3
4
5
6
7
import groovy.json.JsonSlurper
// Get response
def testStepName = "Holdings Scatter Plot"
  
def response = context.expand( '${'+testStepName+'#Response}' )
def jsonSlurper = new JsonSlurper().parseText(response)
def datas = jsonSlurper.data.data

Verify JSON Node's value 

1
2
3
4
5
6
7
import groovy.json.JsonSlurper
  
def response = messageExchange.getResponseContent()
def json = new JsonSlurper().parseText(response)
def clientCount = json.pagination.count
  
assert clientCount>0,"No client"

Get test suite/case/step name

Get test step, test case and test suite's name

1
2
3
4
5
6
7
8
9
//  Get test steps' name
def currentStepIndex = context.currentStepIndex
String currentStepName = testRunner.testCase.getTestStepAt(currentStepIndex).name
String previousStepName = testRunner.testCase.getTestStepAt(currentStepIndex-1).name
String prePreStepName = testRunner.testCase.getTestStepAt(currentStepIndex-2).name
  
// Get test case and test suite's name
String testCaseName = testRunner.testCase.getTestStepAt(currentStepIndex).getParent().getName()
String testSuiteName = testRunner.testCase.getTestStepAt(currentStepIndex).getParent().getParent().getName()

Get property

Get property value by testRunner

1
String testResultPath = testRunner.testCase.testSuite.project.getPropertyValue( "testResultPath" )

Get property value by context

1
String dataDeviationFile = context.expand( '${#Project#dataDeviationFile}' )

Set property

Set property value by testRunner

1
testRunner.testCase.testSuite.project.setPropertyValue( "cookie", cookieNew )

Control flow

Goto test step by name

1
testRunner.gotoStepByName("Copy File")

posted on   张缤分  阅读(556)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示