Hi All,

I have posted the SOAPUI and Groovy useful commands that may help you in your testing. Below are the commands collected from various websites and blogs. I have used few of them in my testing and if any commands are wrongly given please comment and I will update the same.

// How  to get the TestStep Count in SOAPUI using groovy Script

def testStepCount= testRunner.testCase.getTestStepCount()

// How  to get the TestCase in SOAPUI using groovy Script

def tc = testRunner.testCase.testSuite.project.testSuites["Sample Simple TestSuite"].testCases["Simple Login and Logout w. Properties Steps"]

// How  to set  the properties in SOAPUI using groovy Script

tc.setPropertyValue("Username", "ole" )

// How to run the test synchrouously in SOAPUI using groovy Script

def runner = tc.run( null, false )
testRunner.testCase
testRunner.fail
context.myProperty = "Hello"

// How to Create JDBC Connection in SOAPUI using groovy Script

def utils =new  com.eviware.soapui.support.GroovyUtilsPro(context);
context.dbConnection= utils.getJdbcConnection(“StoreageDB”)
context.statement= context.dbConnection.createStatement()
def query = “select * from table_name”
def rs = context.statement.executeQuery(query)
while(rs.next()){
// do your program here
}
rs.close()
context.statement.close()
context.statement.close() 

// How to do Property expansion in SOAPUI using groovy Script

${#Project#Password}

// Write the response from the "Test Request: login" TestStep to a file

// How to  Get the Current User in SOAPUI using groovy Script

def currentUser = context.expand( '${#TestCase#currentUser}' )

// How to Get the Response in SOAPUI using groovy Script

def response = context.expand( '${Test Request: login#Response}' )

// How to  Create a New File and write the response in SOAPUI using groovy Script

new File( "C:/Users/eviware/" + currentUser + "_response.txt" ).write( response )

// How to Create a New File and write the response with a different Charset in SOAPUI using groovy Script

new File( "C:/Users/eviware/" + currentUser + "_response.txt" ).write( response, "UTF-8" )

// How to get username property from TestSuite in SOAPUI using groovy Script

def username = testRunner.testCase.testSuite.getPropertyValue( "Username" )

// How to get properties from testSuite and project in SOAPUI using groovy Script

def testCaseProperty = testRunner.testCase.getPropertyValue( "MyProp" )
def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue( "MyProp" )

// How to get Global  property  in SOAPUI using groovy Script

def globalProperty = com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "MyProp" )

// How to set testCase, testSuite , project and Global  property in SOAPUI using groovy Script

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 )

// How to Get testCase property from Script Assertion in SOAPUI using groovy Script

def testCaseProperty = messageExchange.modelItem.testStep.testCase.getPropertyValue( "MyProp" )

// How to Get testCase name from testCaseResult in SOAPUI using groovy Script

for ( testCaseResult in runner.results )
{
testCaseName = testCaseResult.getTestCase().name
log.info testCaseName
}

// How to Get and Set Settings in SOAPUI using groovy Script

import com.eviware.soapui.settings.SSLSettings
import com.eviware.soapui.SoapUI
// set
SoapUI.settings.setString( SSLSettings.KEYSTORE, pathToKeystore )
SoapUI.settings.setString( SSLSettings.KEYSTORE_PASSWORD, keystorePassword )
// get
SoapUI.settings.getString( SSLSettings.KEYSTORE, "value to return if there is no such setting set" )

// How to get Project name in SOAPUI using groovy Script

def projectName = testRunner.testCase.testSuite.project.name

// How to Conditional inline property expansion in SOAPUI using groovy Script

${= testCase.getPropertyValue( "selection" ) == "first" ? testCase.getPropertyValue( "myFirstXMLSnippet" ) : testCase.getPropertyValue( "mySecondXMLSnippet" )}

// How to Iterate nodes in SOAPUI using groovy Script

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "Request 1#Response" )
for( item in holder.getNodeValues( "//item" ))
log.info "Item : [$item]"

// How to Register the Jdbc driver in SOAPUI using groovy Script

com.eviware.soapui.support.GroovyUtils.registerJdbcDriver( "com.mysql.jdbc.Driver" )

// How to Access Interface, Operation and Request in SOAPUI using groovy Script

import com.eviware.soapui.impl.wsdl.WsdlInterface
myInterface = (WsdlInterface) testRunner.testCase.testSuite.project.getInterfaceByName("SampleServiceSoapBinding")
myOperation = myInterface.getOperationByName("login")
myRequest = myOperation.getRequestByName("Request 1")

// How to Log the result messages of all TestSteps in all failing TestCases in SOAPUI using groovy Script

for ( testCaseResult in runner.results )
{
testCaseName = testCaseResult.getTestCase().name
log.info testCaseName
if ( testCaseResult.getStatus().toString() == 'FAILED' )
{
log.info "$testCaseName has failed"
for ( testStepResult in testCaseResult.getResults() )
{
testStepResult.messages.each() { msg -> log.info msg }
}
}
}

// How to Create Project Event Handler to Save all TestStep's results into files in SOAPUI using groovy Script

Create a Project EventHandler (Project Window > Events tab > Add new EventHandler) of type TestRunListener.afterStep with the following content:
filePath = 'c:/users/henrik/soapUI-results/'
fos = new FileOutputStream( filePath + testStepResult.testStep.label + '.txt', true )
pw = new PrintWriter( fos )
testStepResult.writeTo( pw )
pw.close()
fos.close()

// How to Loop a sequence of TestSteps many times in SOAPUI using groovy Script

A simple loop is easiest achieved by placing a Groovy Script TestStep after the last TestStep in the loop with the following content:
if( context.loopIndex == null )
context.loopIndex = 0
if( ++context.loopIndex < 10 )
testRunner.gotoStepByName( "Name of first TestStep in loop" )

// How to Update a Property to update a Request in SOAPUI using groovy Script

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
// get XmlHolder for request message def
holder = groovyUtils.getXmlHolder( "login#Request" )
// change password using XPath
holder["//username"] = "test"
// write updated request back to teststep
holder.updateProperty()

// How to Get the Response Content and Change the content in SOAPUI using groovy Script

if( request.response == null )
return
// get response content
def content = context.httpResponse.responseContent
// manipulate content
content = content.replaceAll( "555", "444" )
// write it back
context.httpResponse.responseContent = content

Reference: http://www.soapui.org/Scripting-Properties/tips-a-tricks.htm

// How to Get the handle for current Testcase/TestSuite/Project using context/testRunner in SOAPUI using groovy Script

Below command will be useful to get the handle of current Test case or Test suite.

Context.testCase
testRunner.testCase
def project = context.testCase.testSuite.project
def project = testRunner.testCase.testSuite.project
def myTestSuite = project.getTestSuiteAt(IndexNumber)
def myTestSuite = project.getTestSuiteByName(“Name of the TestSuite”)
def myTestCase = myTestSuite.getTestCaseAt(IndexNumber)
def myTestCase = myTestSuite.getTestCaseByName(“Name of the TestCase”)
def myTestStep = myTestCase.getTestStepAt(IndexNumber)
def myTestStep = myTestCase.getTestStepByName(“Name of the TestStep”)

// How to Get Time taken to process the Request/Response in SOAPUI using groovy Script

messageExchange.getTimeTaken()   

// How to Get the End Point in SOAPUI using groovy Script

messageExchange.getEndpoint()

// How to Run the SOAPUI Request in SOAPUI using groovy Script

testRunner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testCase, null);
testStepContext = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext(testStep);
testStep.run(testRunner, testStepContext);

// How to  get theRawRequest & RawResponse in SOAPUI using groovy Script

messageExchange.getRequestContentAsXml.toString()
messageExchange.getResponseContentAsXml.toString()

// How to use closures in SOAPUI using groovy Script

closures are similar to iterators. We mention an array or map of values, and write a closure to iterate over it.

To access the current value in the loop ‘it’ is to be used (but not ${it})

def  sampleArray = [“dev”, “integration”, “qa”, “prod”];
def a = 1;
sampleArray.each() {
log.info(a + ‘:’ + it);
a++;
}
def sampleMap = [ "Su" : "Sunday", "Mo" : "Monday", "Tu" : "Tuesday",
"We" : "Wednesday", "Th" : "Thursday", "Fr" : "Friday",
"Sa" : "Saturday" ];
then use
stringMap.each() {
log.info(it);
log.info( it.key + ‘:’ + it.value );
} 

// How to Access Request/Response using Message Exchange in SOAPUI using groovy Script

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def requsetHolder = groovyUtils.getXmlHolder( messageExchange.requestContent )
def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )

// How to load property files from bin directory in SOAPUI using groovy Script

And this master property file could be placed in %SOAP_UI_HOME%\bin directory of the SOAP UI and can be easily accessed across the Suites

props = new java.util.Properties();
props.load( new FileInputStream(“testProps.properties”) );

  and the property values can be obtained by:

props.getProperty(“QA_PROP_LOCATION”);
props.getProperty(“DEV_PROP_LOCATION”);

// How to create sql conection in SOAPUI using groovy Script

def  sql = Sql.newInstance(dbPath, dbUserName, dbPassword, dbDriverName);
res = sql.execute( “SELECT * FROM TABLE1 WHERE COL1=’123′” );

// How to Get the Project Path in SOAPUI using groovy Script

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

// How to Loop the Response Nodes in SOAPUI using groovy Script

If the desired content is namespace qualified (very likely for SOAP responses), you need to define the namespace first.

holder.namespaces["ns"] = "http://acme.com/mynamspace"
loop item nodes in response message
for( item in holder.getNodeValues( "//ns:item" ))
log.info "Item : [$item]"

// How to Count nodes in SOAPUI using groovy Script

def numberOfLinksInParagraphs = holder["count(//html/body/p/a)"]

// How to Get Nodes in SOAPUI using groovy Script

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder("Properties#response")
log.info holder.getNodeValue("//id")
for( node in holder['//id'] )
log.info node

//How to Add Assertions to test steps in SOAPUI using groovy Script

TSName = "Test Case Name"
StepName = "Test Step Name"
project.getTestSuiteList().each {
if(it.name == TSName)
{
  TS = it.name
it.getTestCaseList().each {
  TC =it.name
  def asserting = project.getTestSuiteByName(TS).getTestCaseByName(TC).getTestStepByName(StepName).getAssertionByName("XPath Match")
  if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
  {
    project.getTestSuiteByName(TS).getTestCaseByName(TC)getTestStepByName(StepName).removeAssertion(asserting)
  }
  def assertion = project.getTestSuiteByName(TS).getTestCaseByName(TC)getTestStepByName(StepName).addAssertion("XPath Match")
  assertion.path = "declare namespace here”
  assertion.expectedContent = "200"
}
}
}  

// How  to assert a node value using holder  in SOAPUI using groovy Script

def holder = new XmlHolder( messageExchange.responseContentAsXml )
assert holder["//ns1:RequestId"] != null
def node = holder.getDomNode("//ns1:RequestId”)

// How  to assert a request and response time  value  in SOAPUI using groovy Script

assert messageExchange.timeTaken < 400

[SoapUI] SOAP UI-Groovy Useful Commands的更多相关文章

  1. soapUI参数中文乱码问题解决方法&soap UI工具进行web接口测试

    soapUI参数中文乱码问题解决方法 可能方案1: 字体不支持中文,将字体修改即可: file-preferences-editor settings-select font 修改字体,改成能显示中文 ...

  2. [原创] SOAP UI 创建SOAP工程进行接口测试

    下载及安装 1. 登录http://www.soapui.org/ 2. 鼠标移动到导航头的Downloads选项 3. 点击SOAP UI 4. 下载页面 新建项目 创建项目 1. 创建项目很简单. ...

  3. soap ui 进行接口测试

    [前置条件] 1. 电脑上已安装soap UI 5.0 2. 电脑上已安装eclipse. JDK1.6.tomcat 3. eclipse已经成功的配置JDK1.6.tomcat [操作步骤] 1. ...

  4. wsse:InvalidSecurity Error When Testing FND_PROFILE Web Service in Oracle Applications R 12.1.2 from SOAP UI (Doc ID 1314946.1)

    wsse:InvalidSecurity Error When Testing FND_PROFILE Web Service in Oracle Applications R 12.1.2 from ...

  5. Soap UI 数据库脚本(转)

    3:在SoapUI的Test Case中新建Groovy Script连接数据库 接口如下 def sql = Sql.newInstance( 地址, 用户名, 密码, 驱动 ) 实现样例如下: i ...

  6. soapUI系列之—-01 介绍soapUI简介,groovy 简介

    1.soapui简介 SoapUI是一个自由和开放源码的跨平台功能测试解决方案.通过一个易于使用的图形界面和企业级功能,SoapUI让您轻松,快速创建和执行自动化功能.回归.合规和负载测试.在一个测试 ...

  7. API自动化测试 Soap UI工具介绍

    一.   建立测试用例 (一)   基本概念 soapUI 中工程的层次结构 项目名称:位于最上层 (BookStoreTest),项目可以包含多个服务的定义. REST 服务定义:服务其实是对多个 ...

  8. SOAP UI

    We use SoapUI-Pro-5.1.2 1. Basic introduction - Windows 2. Use project environment tab to manage the ...

  9. SOAP UI(ReadyAPI)学习——第一步:资源帖

    SoapUI的参数说明:http://www.soapui.org/Working-with-soapUI/preferences.html 进一步了解可以阅读:http://www.51testin ...

随机推荐

  1. JAVA-Unit02: Oracle字符串操作 、 Oracle数值操作 、 Oracle日期操作 、 空值操作

    Unit02: Oracle字符串操作 . Oracle数值操作 . Oracle日期操作 . 空值操作 DQL数据查询语言 查询语句基本由SELECT子句由FROM子句构成. SELECT子句指定要 ...

  2. Xbox360游戏收藏

    xbox360游戏下载地址 http://dl.3dmgame.com/SoftList_221.html   XBLA游戏总结.http://tieba.baidu.com/p/3174478602 ...

  3. windows7下怎样安装whl文件(python)

    本文转载自:http://blog.csdn.net/fhl812432059/article/details/51745226 windows7 python2.7 1.用管理员方式打开cmd 2. ...

  4. java代码----FileInputStream 和File

    总结:程序运行后,发现新建的两个文件里的东西突然i清空了.以为是程序出错了. 然后慌了,之后我再运行时,发现可以了.是电脑的问题吧 一如既往的打扰他,只因为他优秀 package com.a.b; i ...

  5. 009:JSON

    一. MySQL JSON类型 1. JSON介绍 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级 ...

  6. Linux学习笔记 -- stdin/stdout 重定向

    输入/输出重定向 Linux系统通常从一个叫标准输入的地方读取输入并且将一个命令的结果以写入到标准输出反馈给我们:默认情况下,这也是我们使用的终端(命令行).如果我们想改变输入和输出的方式,就需要使用 ...

  7. 13_java之final|static|包|匿名对象|代码块|内部类

    01final关键字概念 * A: 概述 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承, 或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承 ...

  8. [Z]QPS、PV和需要部署机器数量计算公式

    QPS = req/sec = 请求数/秒 [QPS计算PV和机器的方式] QPS统计方式 [一般使用 http_load 进行统计]QPS = 总请求数 / ( 进程总数 *   请求时间 )QPS ...

  9. DBCA Does Not Display ASM Disk Groups In 11.2

    DBCA Does Not Display ASM Disk Groups In 11.2 https://oraclehowto.wordpress.com/2011/08/15/dbca-does ...

  10. 网卡流量监控脚本 ( Shell )

    #!/bin/bash # Traffic Monitor # author: Xiao Guaishou get_traffic_info(){ recv=`cat /proc/net/dev | ...