1

In SOAPUI i have a JDBC Request step with the next result:

<Results>
    <ResultSet fetchSize="10">
        <Row rowNumber="1">
            <CUSTOMER_ID>1963</CUSTOMER_ID>
            <ID>444</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>vitae consectetuer eget rutrum</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="2">
            <CUSTOMER_ID>4389</CUSTOMER_ID>
            <ID>6008</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>neque</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="3">
            <CUSTOMER_ID>8836</CUSTOMER_ID>
            <ID>5304</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>lectus suspendisse potenti</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="4">
            <CUSTOMER_ID>7360</CUSTOMER_ID>
            <ID>8279</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>at turpis a</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="5">
            <CUSTOMER_ID>823</CUSTOMER_ID>
            <ID>235</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>non interdum</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="6">
            <CUSTOMER_ID>5430</CUSTOMER_ID>
            <ID>4278</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>leo maecenas pulvinar</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="7">
            <CUSTOMER_ID>9209</CUSTOMER_ID>
            <ID>9974</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>ut suscipit</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="8">
            <CUSTOMER_ID>8135</CUSTOMER_ID>
            <ID>8296</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>lectus pellentesque eget</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="9">
            <CUSTOMER_ID>8247</CUSTOMER_ID>
            <ID>4569</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>vulputate nonummy</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="10">
            <CUSTOMER_ID>7156</CUSTOMER_ID>
            <ID>836</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>adipiscing molestie</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
    </ResultSet>
</Results>

Custom Property: "ResponseAsXml"

And i have a Porperty Transfer with the next Source:

Source: JDBC Request

Property: ResponseAsXml

Path Language: XPath

Text box: Results/ResultSet/Row[1]/CUSTOMER_ID[1]

I need to get Row[Random node]/CUSTOMER_ID[1], (this is to avoid use the same customer in multiple test executions), but i don't know how is the sentence to get a random node.

Thanks in advance!

Julian.

2 Answers2

1

Welcome to Stack Overflow. (Or just SO as some say)

I don’t think you can do that with xpath. But its fairly straightforward to do in a Groovy Script Teststep.

You may want to look into XmlSlurper at the same time. Very nifty to know, when you want to do something extra.

Add the Groovy Script Teststep after your JDBC Teststep. Use it to look up how many customers your database search has returned. Use that number to generate a random integer anywhere in the interval 0 to number of customers minus one.

Then use that random number, to fetch the customer id from that particular customer. Then just return the id from your script. This value can now easily be used elsewhere.

Try for yourself. If you have trouble getting it to work, submit your code here, and I’ll help you.

EDIT

I just looked into it, and you can almost do it solely with xpath. But you will need some random number generator too. And it will not be pretty to have that embedded in your xpath. So my second suggestion will be:

  1. Create a Groovy Script Teststep that calculates the number of rows. Name it "RandomInteger". If you always get 10 rows, just hardcode that. Based on that number, return a random integer in the interval 0 to number of rows minus one.

  2. Place this script between your JDBC request teststep, and your property transfer teststep.

  3. Modify your xpath to something like

    Results/ResultSet/Row[${RandomInteger#result]/CUSTOMER_ID[1]

Steen
  • 853
  • 5
  • 12
0

Thanks for the answer,

instead of using property transfer i've replaced it with a groovy script:

Random rnd = new Random() def rowCount = context.expand( '${COUNT#ResponseAsXml#Results/ResultSet/Row/COUNTDISTINCTAD.CUSTOMER_ID}' ).toInteger()

int randomNumber = rnd.nextInt(rowCount + 1 - 0)

testRunner.testCase.testSteps["Properties"].setPropertyValue( "x", randomNumber.toString() )

def customer = context.expand( '${CustomerQuery#ResponseAsXml#Results/ResultSet/Row[${Properties#x}]/CUSTOMER_ID}' )

testRunner.testCase.testSteps["Properties"].setPropertyValue( "CustomerId", customer )