Forum Discussion

Mason's avatar
Mason
Frequent Contributor
15 years ago

[RESOLVED] Property Expansion in RequestFilter.filterRequest script

I was wondering if the Groovy script written for these events in the Events tab support Property expansion?

For example:

def NTT_id_header = context.getProperty("PostMethod")
NTT_id_header.setRequestHeader("NTT_id", "${#TestSuite#subscriber.NTT}");


Reason I ask is, the Soap log is indicating compiler error, complaining about the '#' symbol.  I've tried escaping the characters, I've tried it without quotes, still getting compiler errors.

Any advice?

Thanks,
Mason

UPDATE:  09-25-2009 - 4:22 PM (EST) - I have since figured out that I need to use the getProperty method from the CONTEXT scope but I found another issue.  (New post)

8 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    you need to use single quotes, otherwise groovy will try to expand the property reference itself.. ie

    NTT_id_header.setRequestHeader("NTT_id", '${#TestSuite#subscriber.NTT}');

    regards!

    /Ole
    eviware.com
  • Mason's avatar
    Mason
    Frequent Contributor
    This doesn't work either.  The property does not get expanded (the string ${#TestSuite#subscriber.NTT} is inserted into the header.  The reason for this, we suspect, is because a TestSuite variable will not work in the Event handler area of SoapUI since the scope is at the context level (the TestSuite script hasn't run yet, it runs after the script in the Event handler area).  We used the following code to populate the header:

    def subscriber_c = context.getProperty("subscriber.c");
    log.info ("subscriber_c = " + subscriber_c);
    def c_id_header = context.getProperty("postMethod")
    c_id_header.setRequestHeader("c_id", subscriber_c);

    We populate the property of subscriber.c in the setup script of the project so that it is available to the context scope.

    This has caused a problem however, because we need to be able to modify headers at the TestSuite and/or TestCase level as well.  Having the headers populated at the Project level does not allow us to send two requests with different values for the headers.  For instance if we wanted to send one request with a c_id header with a value of "123" and in the same project, later in the test sequence, send a request with a c_id header and a value of "321".

    Is there any way to dynamically insert headers at the TestSuite and/or TestCase level?
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    I'm sorry, I though you wanted the actual property-expansion to be in the header.. have you tried

    NTT_id_header.setRequestHeader("NTT_id", context.expand( '${#TestSuite#subscriber.NTT}') );

    ?

    regards,

    /Ole
    eviware.com
  • Mason's avatar
    Mason
    Frequent Contributor
    I'm not in a position to try the extra set of parenthesis at the moment.  But will a TestSuite property work within the scope of the Event handlers?
  • Mason's avatar
    Mason
    Frequent Contributor
    eviware support wrote:

    Hi!

    I'm sorry, I though you wanted the actual property-expansion to be in the header.. have you tried

    NTT_id_header.setRequestHeader("NTT_id", context.expand( '${#TestSuite#subscriber.NTT}') );

    ?

    regards,

    /Ole
    eviware.com


    As I suspected, this didn't work.  I get the following error:

    Mon Sep 28 11:34:32 EDT 2009:ERROR:groovy.lang.MissingPropertyException: No such property: NTT_id_header for class: Script4


    The reason, again, we suspect is the scope is wrong.  Event handlers run at the CONTEXT level, the variable/property we are trying to expand is at the TestSuite level.  Is there anyway to get a TestSuite property to expand in the CONTEXT level.  Is there a way to run the Event handlers at the TestSuite level?
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    The error you're getting points to a problem with the NTT_id_header variable, not with the property expansion. There shouldn't be a problem with the scope, since the filter is running after a request, which does belong to a TestSuite. I have verified this myself by setting a TestSuite Property var = hello and adding the following Event handler:

    RequestFilter.filterRequest

    log.info context.expand('${#TestSuite#var}')


    When running the Test (by running the Request itself, the TestCase, TestSuite or even the whole Project) "hello" is being logged once for each request that is run. You may want to try just logging the result of the Property Expansion to the log to verify that it is being expanded as it should with:


    log.info context.expand('${#TestSuite#subscriber.NTT}')


    Hope this helps!

    Regards,
    Dain
    eviware.com
  • Mason's avatar
    Mason
    Frequent Contributor
    When you say "run the test", at which level are you running it?  Project, TestSuite, TestCase?
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hello,

    I've tried running it using all of the above (Project, TestSuite, TestCase and TestStep), it shouldn't make a difference. When the TestStep gets executed, the RequestFilter kicks in and the TestSuite property should be accessible.

    Regards,
    Dain
    eviware.com