Forum Discussion

awl's avatar
awl
Occasional Contributor
3 years ago
Solved

Example needed: How to use a ValueProvider plugin to provide a property value?

Hello SoapUI experts,

 

can you please provide an example of how to access a custom @PluginValueProvider a.k.a. DynamicPropertyResolver.ValueProvider implementation class registered for a valueName (such as "randomNumber") from a property value?

 

Given the standard example for https://www.soapui.org/apidocs/soapui/DefaultPackage/PluginValueProvider.html

 

@PluginValueProvider(valueName = "randomNumber")
public class MyValueProvider implements DynamicPropertyResolver.ValueProvider {
    @Override
    public String getValue(PropertyExpansionContext propertyExpansionContext) {
        return String.valueOf(1000 * Math.random());
    }
}

 

How can I access the random value provided by the ValueProvider from a property defined in my TestCase?

 

I tried several variants:

 

${randomNumber}
${#randomNumber#}
${#Global#randomNumber#}
(...)

 

(and many others) as property values for a property named "testProp" - but without success...

 

So how can I make use of MyValueProvider and get hold of the random number returned by it from a TestCase?

 

Thanks a million in advance for your kind help,

awl

 

  • awl 

    How about

     

    context.randomNumber

    If you are testing in groovy script, the above should work.

     

14 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    awl 

    How about

     

    context.randomNumber

    If you are testing in groovy script, the above should work.

     

    • awl's avatar
      awl
      Occasional Contributor

      Hello again nmrao ,

       


      nmrao wrote:

      awl 

      How about

       

       

      context.randomNumber

       

      If you are testing in groovy script, the above should work.


      great news. This definitely works:

       

      log.info(context.randomNumber)
      log.info(context.randomNumber)
      log.info(context.randomNumber)
      log.info(context.randomNumber)
      log.info(context.randomNumber)

       

      and the output is:

       

      Thu Jul 15 11:49:07 CEST 2021:INFO:637.6823442157611
      Thu Jul 15 11:49:07 CEST 2021:INFO:516.7372824953289
      Thu Jul 15 11:49:07 CEST 2021:INFO:628.5712519826241
      Thu Jul 15 11:49:07 CEST 2021:INFO:353.6060359250122
      Thu Jul 15 11:49:07 CEST 2021:INFO:656.1003869084848

       

       

      But this is not yet what I'd like to see: I'd like to assign the "randomNumber" value provided as the value of a "left-hand-side" property, e.g. at test case or test suite or even global level.

       

      Of course, I can always achieve this by writing custom Groovy code that does this assignment, such as

       

      testRunner.testCase.setPropertyValue("randomNumberProvided", context.randomNumber)

       

      but this would obviously require an additional test step in any test case that would like to use the value from the value provider.

       

      So I am assuming that there MUST be an easier way to do this by typical SoapUI expansible property notation?

       

      Many thanks one more time!

       

      Best regards

      Andreas

      • awl's avatar
        awl
        Occasional Contributor

        Heureka - it seems to work now!  ๐Ÿ˜€