Forum Discussion

HaroldR's avatar
HaroldR
Contributor
8 years ago

How to use $ to populate variable inside another $expression

Hello, 

I am trying to populate a variable value inside a $ Expression inside my groovyscript:

def currentStepInd = context.currentStepIndex;
def previousStep = testRunner.testCase.getTestStepAt(currentStepInd - 2)
def previousStepName = previousStep.name;
def response = context.expand( '${$previousStepName#Response}' );
context.testCase.setPropertyValue("Response", response);

I would like to retrieve previousStepName string value and insert it inside the expand method before #Response but I'm not able to populate it inside the expression to get the context and property. I have tried a lot of things but did not succeed to pass my string teststep as parameter inside the $expression to retrieve context.

If you have any idea, it would be nice!

Regards

 

  • Try this:

     

     

    def currentStepInd = context.currentStepIndex;
    def previousStep = testRunner.testCase.getTestStepAt(currentStepInd - 2)
    def previousStepName = previousStep.name;
    def response = context.expand( '${' + previousStepName + '#Response}' );
    context.testCase.setPropertyValue("Response", response);

     

  • groovyguy's avatar
    groovyguy
    Community Hero

    Try this:

     

     

    def currentStepInd = context.currentStepIndex;
    def previousStep = testRunner.testCase.getTestStepAt(currentStepInd - 2)
    def previousStepName = previousStep.name;
    def response = context.expand( '${' + previousStepName + '#Response}' );
    context.testCase.setPropertyValue("Response", response);

     

    • HaroldR's avatar
      HaroldR
      Contributor

      It works fine!

      Thaks you, it does the trick!

      Best Regards,

      Harold