9

Is it possible to run specific test steps in other Test Cases from a Groovy Script test step?

Can't figure out how to do this

Thank You

Sosian
  • 622
  • 11
  • 28
Mihail
  • 183
  • 1
  • 1
  • 5

5 Answers5

21

Yes it is possible. From the groovy step you have access to the testRunner which you can use to access everything else in soapUI and yes run test steps in another test case.

So, this code is from the top my my head...

def tCase = testRunner.testCase.testSuite.project.testSuites["Name of the other test suite"].testCases["name of test case you want to access"]

or

def tCase = testRunner.testCase.testSuite.testCases["Name of test cases"]

def tStep = tCase.testSteps["test step you want to run"]

tStep.run(testRunner, context)

Check out this link it might be of some help...

Abhishek Asthana
  • 1,857
  • 1
  • 31
  • 51
5

For those of us who, like me, were looking for code for the current version of Ready!API

    def testStep = testRunner.testCase.testSuite.project.getTestSuiteByName("[OTHER_TEST_SUITE_NAME]").getTestCaseByName("[OTHER_TEST_CASE_NAME]").getTestStepByName("[OTHER_TEST_STEP_NAME]")

    testStep.run(testRunner, context)
Christian Gibbs
  • 135
  • 2
  • 7
3

I realise I'm a little late to the party but i thought I'll expand on this topic by posting my solution to a similar problem. Hope this helps someone in the future. Solution can be scaled to cover more than two test steps, test cases and/or projects. This is also my first post on here so please excuse me in advance for any noob errors. Not the most pretty solution either. It may have some redundant variables. All code blocks comprise the whole solution.

Problem: I want to retrieve responses from two different test steps, each in different test cases, in two diff projects BUT in the same workspace. Got it? great!

SOLUTION:

Variables for first project

String firstProjName = "Generic Project One" 
String firstProjTestSuiteName= "Generic Test Suite Name One"
String firstProjTestCaseName = "Generic Test Case Name One"
String firstProjTestStepName= "Generic Test Step Name One"

Variables for second project

String secondProjName= "Generic Project Two"
String secondProjTestSuiteName = "Generic Test Suite Name Two"
String secondProjTestCaseName= "Generic Test Case Name Two"
String secondProjTestStepName= "Generic Test Step Name Two"

Access Generic Test Step Name One

def firstProj= null
def workspace = testRunner.testCase.testSuite.project.getWorkspace();

firstProj= workspace.getProjectByName(firstProjName)

def firstTestCase = firstProj.testSuites[firstProjTestSuiteName].testCases[firstProjTestCaseName ]

def firstTestStep = firstTestCase.getTestStepByName(firstProjTestStepName)

Run Generic Test Step Name One

def runner = null
runner = firstTestStep.run(testRunner, context)

def firstTestStepResp = runner.getResponseContent()
runner = null

Print response to log

log.info(firstTestStepResp)

Same thing with the second test step

def secondProj= null

secondProj= workspace.getProjectByName(secondProjName)

def secondTestCase = secondProj.testSuites[secondProjTestSuiteName ].testCases[secondProjTestCaseName]

def secondTestStep = secondTestCase.getTestStepByName(secondProjTestStepName)

runner = secondTestStep.run(testRunner, context)

def secondTestStepResp = runner.getResponseContent()
log.info(secondTestStepResp)

We now have access to both responses as strings which we can play around with however we want. Compare, tokenize etc. There is also

getResponseContentAsXml()

if the response is wanted as xml instead of a string.

Castinho
  • 31
  • 4
0

// BETTER WAY


  • // project is a test suite
  • // if you have groovy scripts for generating data, the better way is to save them on properties in a test case
  • // please comment this and give me some clues on how to make it more easy, simple or complex
  • // don't know how much capacity have an array for the runner - when I tried lots of test cases with at least 100 test steps, groovy step inform me by error sign about the small array if it is not in some setups of groovy script log
// run Activation_TollContract Test Case
def testSuite = testRunner.testCase.testSuite.project.getTestSuiteByName("Write_name_of_test_suite_here");                  //get test suite by name
//log.info "testSuite " + context.getTestCase().getTestStepAt(n).name
def testCase = testSuite.getTestCaseByName("Write_name_of_test_case_here");                                                                       //get test case by name
//log.info "testCase: " + testCase  
def totalTestSteps = testCase.getTestSteps().size();                                                                                                    //get int total test steps for the given test case
//log.info "totalTestSteps " + totalTestSteps
def PROJECT = testRunner.testCase.testSuite.project.getTestSuiteByName("Write_name_of_test_suite_here")
for(n in (0..totalTestSteps - 1))
{    
    def testStepName = testCase.getTestStepAt(n).name;
    if(n>=5){
            if (PROJECT.getTestCaseByName("Write_name_of_test_case_here").getTestStepByName(testStepName).disabled != true){
           log.info "testStepName: " + n + ": " + testStepName;
           def testStep = testSuite.getTestCaseByName("Write_name_of_test_case_here").getTestStepByName(testStepName);              //get nameMap of step
        runner = testStep.run(testRunner, context);
        log.info ("testStep   >>> 1   " + runner.getStatus().toString());
            }
    }
}
0

You can use the following code:

def testStep = testRunner.testCase.testSuite.project.getTestSuiteByName("[OTHER_TEST_SUITE_NAME]").getTestCaseByName("[OTHER_TEST_CASE_NAME]").getTestStepByName("[OTHER_TEST_STEP_NAME]")

testStep.run(testRunner, context)

For a detailed demo, see: https://coderscamp.wixsite.com/codeit/post/how-to-run-a-test-step-from-groovy-script

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 2
    When linking to your own site or content (or content that you are affiliated with), you [must disclose your affiliation _in the answer_](/help/promotion) in order for it not to be considered spam. Having the same text in your username as the URL or mentioning it in your profile is not considered sufficient disclosure under Stack Exchange policy. – cigien Dec 31 '20 at 06:54