soapUI系列之—-02 Groovy脚本常用方法
1. 定位到某个 TestSuite\TestCase
a. 定位到某个 TestSuite
def testSuite = testRunner.testCase.testSuite.project.testSuites['testSuites Name'];
b. 层级访问**
testRunner.testCase.testSuite.project.testSuites[testSuiteName].testCases[testCaseName].testSteps[testStepName]
/*Case1:练手*/
import com.eviware.soapui.support.GroovyUtils
def testSuite = testRunner.testCase.testSuite.project.testSuites['业务场景']
log.info(testSuite.getName())
log.info(testSuite.getTestCaseCount())
for(int i=0; i<testSuite.getTestCaseCount(); i++) {
if (!testSuite.getTestCaseAt(i).isDisabled()) {
log.info( testSuite.getTestCaseAt(i).getLabel() )
if (!(testSuite.getTestCaseAt(i).getTestStepByName("issueconfirmJQ")).equals()){
def appnoJQ =testSuite.getTestCaseAt(i).getPropertyValue("proposalNoJQ")
log.info( "JQ:"+ appnoJQ)
}
if (!(testSuite.getTestCaseAt(i).getTestStepByName("issueconfirmSY")).equals()){
def appnoSY =testSuite.getTestCaseAt(i).getPropertyValue("proposalNoSY")
log.info( "SY:"+ appnoSY)
}
}
}
层级关系如下图所示
log 如下图:
2. 获取 TestSuite\TestCase 个数
a. 取 TestSuite 个数
testRunner.testCase.testSuite.project.getTestSuiteCount()
b. 取 TestCase 个数
testRunner.testCase.testSuite.project.testSuites['testSuites Name'].getTestCaseCount()**
for(int i=0; i<testSuite.getTestCaseCount(); i++) {
if (!testSuite.getTestCaseAt(i).isDisabled()) {
if (!(testSuite.getTestCaseAt(i).getTestStepByName("stepName")).equals()){
.....
}
}
}
3. 获取 project\TestSuite\TestCase 名称
a. 取project 名称
def tp = testRunner.testCase.testSuite.project;
log.info (tp.getName());
b. 取test suite的名称:getLabel()
def ts = testRunner.testCase.testSuite;
log.info (ts.getLabel());
c. 取test case的名称:getLabel()
def tc = testRunner.testCase;
log.info (tc.getLabel());
4. 设置参数值:setPropertyValue
a. 设置 project level property
//set to project level property 下面两种写法都可
testRunner.testCase.testSuite.project.setPropertyValue("Name", "propValue");
testRunner.testCase.getTestSuite().getProject().setPropertyValue("Name", "propValue");; //项目对象
b. 设置 testSuite level property
//set to testSuite level property
testRunner.testCase.testSuite.setPropertyValue("Name","testSuiteValue");
testRunner.testCase.getTestSuite().setPropertyValue("Name","testSuiteValue");
c. 设置 testCase level property
//set to testCase level property
testRunner.testCase.setPropertyValue("Name","testCaseValue");
d. 设置 testStep level property
//set to testStep level property
testRunner.testCase.testSteps['Groovy'].setPropertyValue("Name","testSuiteValue");
testRunner.testCase.getTestStepByName("Groovy").setPropertyValue("Name","testSuiteValue");
5. 定义变量
SoapUI允许在项目的各个层次中定义变量,常用的层次包括: Project,TestSuite,TestCase,Global等
在 Groovy Script中使用这些全局变量的话,可以用以下方法:
def time_num= context.expand ('${#Project#hospitalId}') //##号内为定义哪个级别的属性变量,后面为属性名
6. 获取reponse
//Get response
def groovyUtils = new GroovyUtils( context )
def holder = groovyUtils.getXmlHolder('inquire#Response')
//Parse return value
def data = holder.getNodeValue("//return")
log.info(data)
7. 执行某个接口请求
/*Case2:练手*/
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
CURRENT_TESTCASE = testRunner.testCase
TEST_SUITE = CURRENT_TESTCASE.parent
def testStep = TEST_SUITE.getTestCaseByName('TestSuite').getTestStepByName('login')
def testStepContext = new WsdlTestRunContext(testStep)
def result = testStep.run(testRunner, testStepContext)
log.info result.responseContent
调用另一个TestSuite中的teststep脚本如下:
def testproject = testRunner.testCase.testSuite.project
def testcase = testproject.testSuites['PublicCase'].testCases['TestCase1']
testcase.testSteps["CreateService1"].run(testRunner, context)
调用本TestSuite中的TestStep脚本如下:
testRunner.runTestStepByName("Login")
testRunner.runTestStepByName("CreateAPI1-Scenario2")
8. 步骤跳转脚本
testRunner.gotoStepByName("Step2") 跳转到指定步骤
testRunner.runTestStepByName("Step2") 运行指定步骤或脚本
9. 调用断言
assert sessioninfo.equals("OK")