Forum Discussion

francisbrochu's avatar
francisbrochu
Occasional Contributor
8 years ago
Solved

Is there a way to call a context property with hashtag ?

With a test case property I can do :

 

${#TestCase#idEna}

 

Is there a way to do the same with context property or I always need getProperty ?

 

The reason I ask is because I would like to use two context properties in a REST call.

 

Thank you,

  • Hi,

     

    TestCase context properties can be referenced using the default scope e.g. 

    ${idEna}

    Regards,

    Rupert

8 Replies

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    TestCase context properties can be referenced using the default scope e.g. 

    ${idEna}

    Regards,

    Rupert

  • francisbrochu's avatar
    francisbrochu
    Occasional Contributor
    context.setProperty('idEna','2334')
    
    log.info context.getProperty('idEna')
    log.info ${idEna}

    My first log.info works, but not the second one : 

     

    groovy.lang.MissingMethodException: No signature of method: Script5.$() is applicable for argument types: (Script5$_run_closure1) values: [Script5$_run_closure1@4e3c1551] Possible solutions: is(java.lang.Object), run(), run(), any(), use([Ljava.lang.Object;), any(groovy.lang.Closure) error at line: 4

    • rupert_anderson's avatar
      rupert_anderson
      Valued Contributor

      Ok, so thats slightly different to the way I took your first question.

       

      To get a context property from Groovy code do:

      log.info context["idEna"]
      
      or
      
      log.info context.idEna

      to access a property using a property expansion e.g. to insert into a request, do:

      ${idEna}

      Is that what you wanted?

       

      Regards,

      Rup

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Yes, you can also do it that way, although longer :-)

         

        Similarly, to set a context property in Groovy you can also a shorter option:

         

        context["hello"]="yes"
        
        log.info context.hello
        
        or
        
        context.hello = "no"
        
        log.info context.hello

        Regards,

        Rup

    • nmrao's avatar
      nmrao
      Champion Level 3

      Q: How to set a property to context in Groovy Script?

      A: context.myPropertyName = 'myValue'

       

      Q: How to get a context property?

      A#1: In  Groovy Script (exactly opposite to set)

      def val = context.myPropertyName

      log.info val

       

      A#2: In Request step such as SOAP / REST

      like rupert_anderson told,

      xml:

      <xmlElement>${myPropertyName}</xmlElement>

      json :

      { "property" : "${myPropertyName}"}

       

    • francisbrochu's avatar
      francisbrochu
      Occasional Contributor

      It works with 

       

      log.info context.expand('${idEna}')

      Thank you !