【问题标题】:SoapUI & Groovy - How to get properties from different TestCases using Run testCaseSoapUI & Groovy - 如何使用 Run testCase 从不同的 TestCase 获取属性
【发布时间】:2015-09-11 12:07:41
【问题描述】:

提前抱歉。我确信这不是一个新问题,但我发誓我已经寻找了几天并尝试了几种解决方案,但没有一个完全符合我的需要。我希望你们能帮助我...

我的问题:

  1. 我有一个向我们的服务器发送 bash 命令的主脚本:
TestSuite: Tools;
   TestCase: sendBashCommands;
      TestStep: groovycript;
  1. 这个测试脚本被几个使用“运行测试用例”的测试用例调用。每个测试用例都有不同的 bash 命令:
TestSuite: ServerInfo
   TestCase: getServerVersion
      Property: BashCommand="cat smt | grep Version"
      TestStep: Run sendBashCommands

   TestCase: getServerMessage
      Property: BashCommand="cat smt | grep Message"
      TestStep: Run sendBashCommands
   ...

在我的 sendBashCommands.groovyScript 上,我已经尝试过以下操作:

//def bashCmd= context.expand( '${#BashCommand}' );
     //it returns empty;
//def bashCmd= testRunner.testCase.getPropertyValue( "BashCommand" );
     //it returns null;
//def bashCmd= context.getTestCase().getPropertyValue( "BashCommand" );
     //it returns null;
def bashCmd = context.expand('${#TestCase#BashCommand}');
     //it also returns empty;

目前我使用的解决方案适用于项目属性,但我真正需要的是在调用 sendBashCommand 脚本的测试用例级别上使用这些属性。那可能吗?我该怎么做?

【问题讨论】:

    标签: groovy properties soapui getproperty getproperties


    【解决方案1】:

    我认为您这样做是出于维护目的... 根据您的方法,结果必须为 null 或为空。因为你的 groovyscript 不能直接获取其他测试用例的属性。如果直接调用 getProperty() 方法,那么它将引用当前 testStep 的属性。因此,以下方法可以帮助您。

    将以下代码放入您的个人测试用例中,“getServerVersion”等。

    def currentProject = testRunner.testCase.testSuite.project
    // the following is to store the names of the test suite and the test cases from which you want to call your main script
    
    currentProject.setPropertyValue("TestSuiteName",testRunner.testCase.getTestSuite().getLabel().toString())
    currentProject.setPropertyValue("TestCaseName", testRunner.getTestCase().getLabel().toString())
    // call sendBashCommands here -> run sendBashCommands
    

    将此代码放入您的主 groovy 脚本 (sendBashCommands.groovyscript)

    def currentProject = testRunner.testCase.testSuite.project
    def callingTestCase = currentProject.testSuites[currentProject.getPropertyValue("TestSuiteName")].testCases[currentProject.getPropertyValue("TestCaseName")]
    def bashCmd = callingTestCase.getPropertyValue( "BashCommand" )
    // to verify
    log.info bashCmd 
    

    该项目对所有测试套件都是通用的,因此您在任何情况下都必须使用项目属性,即满足您的要求 :) 享受:)

    【讨论】:

    • 正是我需要的!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 2019-02-21
    • 2015-01-26
    相关资源
    最近更新 更多