soapUI:如何从断言脚本访问Test Step属性?

时间:2011-02-18 07:15:18

标签: testing groovy properties soapui

我是SoapUI和Groovy的新手,但他是一位经验丰富的Java程序员。

我创建了一个包含两个测试步骤的TestCase:

  1. 属性步骤名为CID,其中包含单个属性correlationID和值${=java.util.UUID.randomUUID()}
  2. Test Request我将<CorrelationID>${correlationID}</CorrelationID>放入请求中。
  3. 它完美运行,每次运行测试时都会提交唯一的CorrelationID值。

    现在我想添加新的脚本断言到测试步骤2(Test Request),将测试步骤1(correlationID)的计算CID属性值与测试步骤2中的一些数据进行比较响应。问题是我似乎无法从那里访问correlationID的生成值。

    如果我试试这个:log.info "${correlationId}"

    我得到:No such property: correlationId for class: Script19

    如果我试试这个:log.info "${CID#correlationId}"

    我明白了:

    startup failed:
    Script43.groovy: 1: unexpected char: '#' @ line 1, column 16.
       log.info "${CID#correlationId}"
                      ^
    org.codehaus.groovy.syntax.SyntaxException: unexpected char: '#' @ line 1, column 16.
       at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:97)
       at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:71)
       at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
       at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:158)
       at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:814)
       at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:511)
       at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:487)
       at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:464)
       at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
       at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287)
       at groovy.lang.GroovyShell.parseClass(GroovyShell.java:727)
       at groovy.lang.GroovyShell.parse(GroovyShell.java:739)
       at groovy.lang.GroovyShell.parse(GroovyShell.java:766)
       at groovy.lang.GroovyShell.parse(GroovyShell.java:757)
       at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:148)
       at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:93)
       at com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion.assertScript(GroovyScriptAssertion.java:116)
       at com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion.internalAssertResponse(GroovyScriptAssertion.java:133)
       at com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion.assertResponse(WsdlMessageAssertion.java:156)
       at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest.assertResponse(WsdlTestRequest.java:189)
       at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest.setResponse(WsdlTestRequest.java:159)
       at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.run(WsdlTestRequestStep.java:346)
       at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.runTestStep(WsdlTestCaseRunner.java:207)
       at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.internalRun(WsdlTestCaseRunner.java:138)
       at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.internalRun(WsdlTestCaseRunner.java:39)
       at com.eviware.soapui.impl.wsdl.support.AbstractTestRunner.run(AbstractTestRunner.java:135)
       at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
       at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
       at java.util.concurrent.FutureTask.run(Unknown Source)
       at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
       at java.lang.Thread.run(Unknown Source)
    Caused by: Script43.groovy:1:16: unexpected char: '#'
       at org.codehaus.groovy.antlr.parser.GroovyLexer.nextToken(GroovyLexer.java:695)
       at org.codehaus.groovy.antlr.parser.GroovyLexer$1.nextToken(GroovyLexer.java:248)
       at groovyjarjarantlr.TokenBuffer.fill(TokenBuffer.java:69)
       at groovyjarjarantlr.TokenBuffer.LA(TokenBuffer.java:80)
       at groovyjarjarantlr.LLkParser.LA(LLkParser.java:52)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.nls(GroovyRecognizer.java:780)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.openOrClosableBlock(GroovyRecognizer.java:8848)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.stringConstructorValuePart(GroovyRecognizer.java:13449)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.stringConstructorExpression(GroovyRecognizer.java:11932)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.primaryExpression(GroovyRecognizer.java:11091)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.argumentLabel(GroovyRecognizer.java:10863)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.commandArgument(GroovyRecognizer.java:10756)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.commandArguments(GroovyRecognizer.java:10173)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expressionStatement(GroovyRecognizer.java:8948)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.statement(GroovyRecognizer.java:1258)
       at org.codehaus.groovy.antlr.parser.GroovyRecognizer.compilationUnit(GroovyRecognizer.java:650)
       at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:93)
       ... 31 more
    
    1 error
    

    如何从我的断言Groovy脚本中访问correlationId计算值?

    谢谢

3 个答案:

答案 0 :(得分:11)

我的方法的问题是,即使我访问该属性,它也是动态的,每次我读取属性的值时,我的UUID都不同。

相反,我用测试用例的安装脚本替换了我的Properties测试步骤:

uuid = context.expand('${=java.util.UUID.randomUUID()}')
testRunner.testCase.setPropertyValue("correlationID", uuid)

每次我的测试用例执行时,都会生成一个新的uuid。然后,未来的测试步骤可以访问此静态测试用例级别属性。

在我的测试请求中,我使用:

<CorrelationID>${#TestCase#correlationID}</CorrelationID>

在脚本断言中我使用:

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

感谢SoapUI论坛的Unhandled

答案 1 :(得分:1)

想要添加context.getPropertyNames()或context.getProperties()不会列出“属性”测试步骤中定义的属性。 只有context.expand('')可以工作。

答案 2 :(得分:0)

要从脚本声明中获取当前测试步骤的属性名称,我们可以使用以下代码-

def testStepProp = context.getCurrentStep().getPropertyNames() for(i=0; i<testStepProp.size(); i++){ log.info testStepProp[i] }

要从脚本声明中获取当前测试用例的属性名称,我们可以使用以下代码:

def testCaseProp = context.testCase.getPropertyNames() for(i=0; i<testCaseProp .size(); i++){ log.info testCaseProp[i] }

要从脚本断言中获取属性值:

例如,如果我的属性名称为“ correlationID”-

def myValue = context.testCase.getPropertyValue("correlationID") //If we are in same testCase def myValue = context.testCase.testSuite.testCases["testCaseName"].getPropertyValue("correlationID") //If we are in different testCase

请注意,“ testRunner”在脚本断言中不起作用。相反,我们需要使用上下文。