Forum Discussion

opaciga's avatar
opaciga
New Contributor
3 years ago

Get a list of all properties

I have a scenario where a test case loads a DataSource and uses testRunner.testCase.testSuite.setPropertyValue() in a Groovy script to pass values from the data to another test case. The code to do this is highly repetitive and depends on listing out each properly to be passed from the DataSource.

 

Essentially, I have these lines duplicated almost identically dozens of times:

 

 

def some_input_value = context.expand( '${DataSource#some_input_value}' )
testRunner.testCase.testSuite.setPropertyValue( "some_input_value", some_input_value)

 

 

Is it possible to list all properties available on the DataSource, so that I could replace all these repeated blocks with a for loop? e.g. something like:

 

 

DataSource.getProperties().each {
    testRunner.testCase.testSuite.setPropertyValue( it, context.expand( '${DataSource#'+it+'}' ))
}

 

 

 

  • Not a problem, let me see what I can do to help! Give this a shot and let me know if it works. Will need to run the Data Source so it has data, of course.
     

    // This is highly dependent on where your DataSource lives. Assuming within the same Test Case.
    
    // Assuming Data Source name of DataSource.
    def DataSource = context.testCase.testSteps["DataSource"];
    
    DataSource.getProperties().each {
     // testRunner should work, but it might need to be context.testCase.testSuite
     testRunner.testCase.testSuite.setPropertyValue( it.key, context.expand( '${DataSource#'+it.key+'}' ))
    }

     

  • Thanks, that worked like a charm! Exactly what I needed.

  • groovyguy's avatar
    groovyguy
    Community Hero

    opaciga you are very close with the script that you have. But in this instance "it" is a reference to a hash map of the key (property name) and the value (property value from the datasource.)

    If you change "it" to "it.key" the script should work as you expect. I tried it out with one of my projects that has a datasource and it worked for me. Let me know if it doesn't!

  • opaciga's avatar
    opaciga
    New Contributor

    I maybe have accidentally hit on something when what I intended as pseudocode... how do I get the DataSource object in the first place? I only know how to access its properties with `context.expand()`, which only returns a string.

     

    • groovyguy's avatar
      groovyguy
      Community Hero

      Not a problem, let me see what I can do to help! Give this a shot and let me know if it works. Will need to run the Data Source so it has data, of course.
       

      // This is highly dependent on where your DataSource lives. Assuming within the same Test Case.
      
      // Assuming Data Source name of DataSource.
      def DataSource = context.testCase.testSteps["DataSource"];
      
      DataSource.getProperties().each {
       // testRunner should work, but it might need to be context.testCase.testSuite
       testRunner.testCase.testSuite.setPropertyValue( it.key, context.expand( '${DataSource#'+it.key+'}' ))
      }

       

  • opaciga's avatar
    opaciga
    New Contributor

    Thanks, that worked like a charm! Exactly what I needed.

    • groovyguy's avatar
      groovyguy
      Community Hero

      Sorry for accidentally accepting your own reply as a solution. My mouse skipped!

      You are more than welcome, glad I was able to help!