Forum Discussion

amitst's avatar
amitst
Occasional Contributor
6 years ago
Solved

context.expand with dynamic string not working in dynamic property expansion

I have a property named "username0" saved at project level.

 

In my groovy script, the following code works and logs the value of property "username0" (actually I am using context.ThreadIndex here),

log.info(context.expand('${#Project#username' + '0}'));

 

However, in dynamic property expansion under my HTTP request, the property value is not getting substituted.

 

For e.g., the following works (returns the value of username0) in side the property value string under HTTP request,

<mytag>{=context.expand('${#Project#username0}')}</mytag>

 

However, the following doesn't work (returns blank string, i.e. <mytag></mytag>)

<mytag>{=context.expand('${#Project#username' + '0}')}</mytag>

 

Eventually I need to do something like below to choose correct variable in case of multi-threaded scenario,

<mytag>{=context.expand('${#Project#username' + context.ThreadIndex + '}')}</mytag>

 

Am I doing anything wrong above?

  • This is because Inline scripts don't work when you have braces in them. So you can't put in expansion that includes braces inside an expansion.

    This also limits those of us who want to write Groovy stuff with closures inline, like....

    ${="abc".with { it.toUppercase }}

    But if you can figure out how to do what SoapUI does when it processes expansions, you might be able to get away with it.

    This should work for you:

    <mytag>${=context.testCase.project.getPropertyValue('username' + context.ThreadIndex)}</mytag>

1 Reply

  • JHunt's avatar
    JHunt
    Community Hero

    This is because Inline scripts don't work when you have braces in them. So you can't put in expansion that includes braces inside an expansion.

    This also limits those of us who want to write Groovy stuff with closures inline, like....

    ${="abc".with { it.toUppercase }}

    But if you can figure out how to do what SoapUI does when it processes expansions, you might be able to get away with it.

    This should work for you:

    <mytag>${=context.testCase.project.getPropertyValue('username' + context.ThreadIndex)}</mytag>