SoapUI Script Library

Environment

Get active environment via groovy script

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

Set active environment via groovy script

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

Get request

Get request header via messageExchange

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

Get test step name via messageExchange

def testStepName = messageExchange.modelItem.name

Get response

Get response by testRunner

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

Get response by Context

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

Get response by messageExchange

def response = messageExchange.getResponseContent()

Get response header

def headers = messageExchange.getResponseHeaders()

Parse XML

XPath Parse XML : Get node value

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

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

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

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

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 

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

//  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

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

Get property value by context

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

Set property

Set property value by testRunner

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

Control flow

Goto test step by name

testRunner.gotoStepByName("Copy File")

posted on 2017-06-30 11:18  张缤分  阅读(556)  评论(0编辑  收藏  举报

导航