简体   繁体   English

SoapUI和Groovy-如何使用Run testCase从不同的TestCases获取属性

[英]SoapUI & Groovy - How to get properties from different TestCases using Run testCase

Sorry in advance. 不好意思 I am sure this is not a new question but I swear I've been looking for days and have tried several solutions but no one fits exactly what I need. 我确信这不是一个新问题,但是我发誓我一直在寻找几天,并尝试了几种解决方案,但没有人完全符合我的需求。 I hope you guys can help me... 我希望你们能帮助我...

My problem: 我的问题:

  1. I have a main script which sends bash commands to our server: 我有一个主脚本,它将bash命令发送到我们的服务器:
TestSuite: Tools;
   TestCase: sendBashCommands;
      TestStep: groovycript;
  1. This test script is called by several test cases using "Run testCase". 一些测试用例使用“ Run testCase”调用此测试脚本。 Each test case have a different bash command: 每个测试用例都有一个不同的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
   ...

On my sendBashCommands .groovyScript I have already tried the following: 在我的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;

Currently I use a solution which works with project properties but what I actually need is working with these properties on the level of the test case which is calling the sendBashCommand script. 当前,我使用的解决方案适用于项目属性,但是我实际需要的是在调用sendBashCommand脚本的测试用例级别上使用这些属性。 Is that possible? 那可能吗? How could I do it? 我该怎么办?

I think you are doing it for maintenance purpose... As per your approach, the result has to come as null or empty. 我认为您这样做是出于维护目的...按照您的方法,结果必须为null或为空。 Because your groovyscript cannot get the properties of other testcases directly. 因为您的groovyscript无法直接获取其他测试用例的属性。 If you call a getProperty() Method directly, then it will refer to the current testStep's property. 如果直接调用getProperty()方法,则它将引用当前testStep的属性。 So the following approach could help you. 因此,以下方法可以为您提供帮助。

put the following code in your individual test cases, "getServerVersion" etc. 将以下代码放入您的各个测试用例中,“ 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

put this code in your main groovy script (sendBashCommands.groovyscript) 将此代码放入您的主要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 

The project is common to all the test suites, so you have to use project property in any case, ie, for your requirement :) Enjoy :) 该项目是所有测试套件所共有的,因此无论如何您都必须使用项目属性,即,根据您的要求:)享受:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM