Forum Discussion

plakar's avatar
plakar
New Contributor
6 years ago

Property array

Hi, 

 

I wanted to know if it was possible to store multiple value in a property and call the elements in a request. Something like

array = 1|2|3  (property in TestSuite for example)

and ${#TestSuite#array#0} would return 1 and so on. Is there something like that ? I read that you could use delimiter with | but i didn't really find much more on the doc and on the forum. (also, i don't use the pro version)

 

Thanks

  • Yes, it's possible to store multiple values in a single property using a delimiter and then retrieve those values individually in SoapUI (even in the free version). You can achieve this by using Groovy scripting to split the values stored in the property and access them as needed.

    Here's how you can do it:

    ### **1. Store the Values in a Property:**
    First, store your values as a single string separated by a delimiter (like `|`) in a property, for example, in the `TestSuite` properties:

    - **Property Name:** `array`
    - **Property Value:** `1|2|3`

    ### **2. Access the Values Using Groovy Script:**
    Since you want to retrieve the values individually, you can use a Groovy script to split the values and access them by index.

    Here’s a Groovy script that you can use in a **Groovy Script TestStep**:

    ```groovy
    // Get the property value
    def arrayString = context.testCase.testSuite.getPropertyValue("array")

    // Split the string into an array
    def array = arrayString.split("\\|")

    // Access individual elements
    def firstElement = array[0]
    def secondElement = array[1]
    def thirdElement = array[2]

    // Log the values (for testing purposes)
    log.info("First element: " + firstElement)
    log.info("Second element: " + secondElement)
    log.info("Third element: " + thirdElement)

    // You can also store them in context or variables for later use in your TestCase
    context.setProperty("firstElement", firstElement)
    context.setProperty("secondElement", secondElement)
    context.setProperty("thirdElement", thirdElement)
    ```

    ### **3. Using the Values in Other Test Steps:**
    You can then access these individual values using `${#TestCase#firstElement}`, `${#TestCase#secondElement}`, and `${#TestCase#thirdElement}` in your requests or other test steps.

    ### **Explanation:**
    - The script splits the `array` string into an array using the `split("\\|")` method, where `\\|` is the regular expression to match the pipe `|` delimiter.
    - You can access each element by index (e.g., `array[0]` for the first element).
    - The `context.setProperty` method stores the elements in the TestCase context, allowing you to reference them later. You can explore more on fort worth menu.

    ### **Alternatives:**
    If you prefer not to use Groovy scripts, you could store each value in separate properties and access them directly. However, the Groovy script method is more flexible and scales better for larger arrays or more complex logic.

    This approach gives you the flexibility to store and retrieve multiple values from a single property, even without the pro version of SoapUI.

  • nmrao's avatar
    nmrao
    Champion Level 3
    Use case and little bit of context would help? Please add the details.
    • plakar's avatar
      plakar
      New Contributor

      Sorry for the slow response, kinda lost track this weekend ^^

       

      I'm doing a JDBC request and retrieving multiple value of the same data type. Using a property transfer i can transfer one result into one property, which is not enough for what i want. I would need to put several value of the same type into one property.

       

      The only solution i could think of was to somehow retrieve the xml results of the jdbc request in a groovy script (i guess it must be possible) and then create as much variables as there are results. But i thought it would be way simpler to just be able to put several values into one property.

      • nmrao's avatar
        nmrao
        Champion Level 3
        plakar, may be it would be helpful if you can specify what do you want to after receiving jdbc result.