Ask a Question

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

SOLVED
awl
Occasional Contributor

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
nmrao
Community Hero

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

@awl 

How about

 

context.randomNumber

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

 



Regards,
Rao.

View solution in original post

14 REPLIES 14
nmrao
Community Hero

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

Have you followed any documentation? Please check below if not already
https://www.soapui.org/docs/extension-plugins/developing-plugins/plugin-development/
Where did you copied the library that you created?


Regards,
Rao.
nmrao
Community Hero

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

@awl 

How about

 

context.randomNumber

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

 



Regards,
Rao.

View solution in original post

awl
Occasional Contributor

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

Hello Rao,

 


@nmrao wrote:
Have you followed any documentation? Please check below if not already
https://www.soapui.org/docs/extension-plugins/developing-plugins/plugin-development/
Where did you copied the library that you created?

yes, I was fully aware of the link that you provided.

The code sample with the "randomNumber" ValueProvider can be found at many places, e.g.

https://support.smartbear.com/readyapi/docs/configure/plugins/dev/annotations/misc/valueprovider.htm...

(and yes, I am aware that generally this is about commercial ReadyAPI), or also in the Maven SoapUI plugin archetype project(s) on Github:

https://github.com/olensmar/maven-soapui-plugin-archetype/blob/master/src/main/resources/java/ValueP...

https://github.com/SmartBear/ready-api-plugins/blob/master/ready-api-plugin-archetype/src/main/resou...

 

The sad thing (and I really wonder that the person who wrote this piece of code and documentation has not provided this info) is that there is absolutely no information available at any of these places how to access the value that has now successfully been provided by the ValueProvider from any test case...

 

I will just move forward and try the proposal from your next reply and report back shortly.

 

Many thanks so far! 😀

Andreas

awl
Occasional Contributor

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

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

nmrao
Community Hero

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

Would you like to use randomNumber in test request such as SOAP or REST?

say <id><randomNumber></id>

There are other options such

<id>${= System.currentTimeMillis()}</id>

 

or

<id>${= java.util.UUID.randomUUID().text()}</id>

 

Please see if the above approach works in your case.



Regards,
Rao.
awl
Occasional Contributor

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

Heureka - it seems to work now!  😀

 

grafik.png

awl
Occasional Contributor

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

Hi again,

 


@nmrao wrote:

Would you like to use randomNumber in test request such as SOAP or REST?

no - my goal for the plugin is even (much) more complex: I want to use a named ValueProvider but in addition, pass parameters to a concrete usage of this ValueProvider, such as

 

Value Provider Name ("valueName") == "vaultSecret"

 

and then use it like

${vaultSecret#some_path_within_a_Vault_instance_representing_a_secret_value}

Do you happen to know whether it's possible to pass such a property parameter value ("some_path_within_a_Vault_instance_representing_a_secret_value") into the ValueProvider class, i.e. can I access it using the PropertyExpansionContext that is passed to my

public String getValue(PropertyExpansionContext propertyExpansionContext) { (...) }

method?

 

Thanks a million one more time for your kind help! 👍
awl

nmrao
Community Hero

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

This is brilliant. Nice you have explore well.


Regards,
Rao.
nmrao
Community Hero

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

Thank you for the details.

 

Somewhere it still not hit my brain, the final use case. But your use case seems to be interesting.

If I understand it better, probably can try something.

These are some other uses found for DataProviders found in the source, Not sure if that is of any help to you.

https://github.com/SmartBear/soapui/search?q=DynamicPropertyResolver



Regards,
Rao.
cancel
Showing results for 
Search instead for 
Did you mean: