Surya
Surya

Reputation: 13

Assert multiple values in an XML response received on querying DB in SOAP UI

I am trying to retrieve some data from db and based on the response, I have to pass or fail a test case. I have to do this each time I query the db

I am able to make the connection to the db and get the response. However, not quite sure on how to assert the values from the response

Tried to use Script Assertion but not able to figure out as I am completely new at this

<Results>
<ResultSet fetchSize="10">
    <Row rowNumber="1">
        <T1>TEXT1</T1>
        <T2>TASK1</T2>
        <T3>Value1</T3>
        <T4>xyz</T4>
      </Row>
       <Row rowNumber="2">
        <T1>TEXT2</T1>
        <T2>TASK2</T2>
        <T3>Value1</T3>
        <T4>ABC</T4>
      </Row>
     </ResultSet>

From the above response, on the first step I will have to assert that "TASK1" exists and the "Value1" exists with in the same set followed by the "TASK2" and "Value1"

Please let me know if this is vague so that I can try to modify my query

Upvotes: 1

Views: 2826

Answers (3)

Ud_Undefined
Ud_Undefined

Reputation: 104

Use Xpath assertion instead
Assertion 1
/Results/ResultSet[@fetchSize="10"]/Row[1]/T1
Expected result
Task1
Assertion 2
/Results/ResultSet[@fetchSize="10"]/Row[1]/T3
Expected result
Value1

You can add as many Xpath Assertion as you want for any number of nodes.

Quick Tip: To generate XPaths use online tools or Oxygen XML Editor. :)

Upvotes: 1

Rao
Rao

Reputation: 21389

You can use Script Assertion for the JDBC Request test step in soapUI.

Script assertion

//define your expected data as map. You may add more key value pairs if more Rows
def expectedData = ['TASK1':'Value1', 'TASK2':'Value1']

//Assert if the response is not null
assert context.response, 'Response is not null or empty'

//Parse and get rows
def rows = new XmlSlurper().parseText(context.esponse).ResultSet.Row

def getRowData = { data, elementName, elementValue ->
    data.'**'.findAll { it.name() == elementName && it == elementValue }*.parent()
}

def assertionErrors = new StringBuffer()

//Loop thur expectedData and capture the errors
expectedData.each { key, value->
    def matchingRow = getRowData(rows, 'T2', key)[0]
    value == matchingRow.T3.text() ?: assertionErrors.append("Assertion failed for rowNumber ${matchingRow.@rowNumber}\n")
}

//Check and show the result
if (assertionErrors) {
    throw new Error(assertionErrors.toString())
} else {
    log.info 'Assertions passed'
}

You may quickly try the solution online Demo; it shows how failure error message look like.

Note that, column names T2, T3are used in above script. If the names are different in original result, just make the change at your end.

Also note that assertion is not used deliberately to catch all the comparison issues, do not want to stop at first compare issue.

Upvotes: 0

Anton Hlinisty
Anton Hlinisty

Reputation: 1467

Try XmlSlurper: http://groovy-lang.org/processing-xml.html

To check TASK1:

def results = new XmlSlurper().parseText(response)

def row1 = results.ResultSet.find { node->
    node.name() == 'Row' && node.@rowNumber == '1'
}

assert row1.T2 == 'TASK1'

I hope you'll be able to do the rest by your own ;)

Upvotes: 0

Related Questions