mo0206
mo0206

Reputation: 801

Finding html element with custom field in XML response using groovy

In SOAP UIs XML Response I have an element like below:

<option selected="selected" value="5">Premium</option>

i am tryin to find the element with a custom field value in the XML response. Get the value "selected" of 'selected' attribute of element with value="5" and text= Premium.

i am doing something like below. But it returns me a [] response.

def sortByValue= resp.depthFirst().option.findAll{it.@selected=="selected" && it.text()=='Premium'}
log.info sortByValue

while

 def sortByValue= resp.depthFirst().option.findAll{it.@value=="5" && it.text()=='Premium'}
log.info sortByValue

returns me a response

  [option[attributes={value=5}; value=[Premium]]]

I am unable to get the value of selected

Could someone please let me know how to make this work ?

Upvotes: 0

Views: 492

Answers (2)

Eiston Dsouza
Eiston Dsouza

Reputation: 404

Try this... :)

 def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)

        def holder =  groovyUtils.getXmlHolder("TestStepName#Response")
        def optionNode = holder.getDomNode("//option")
        def optionNodeAttributes = optionNode.getAttributes()

        log.info optionNodeAttributes.getNamedItem("selected").getNodeValue()
        log.info optionNodeAttributes.getNamedItem("value").getNodeValue()
        log.info holder.getNodeValue("//option")

Upvotes: 0

dmahapatro
dmahapatro

Reputation: 50245

Similar to this would suffice?

def xml = '''\
<div>
    <select>
        <option value="1">Regular</option>
        <option selected="selected" value="5">Premium</option>
        <option value="7">Gold</option>    
    </select>
</div>
'''

def parsed = new XmlParser().parseText(xml)
parsed.'**'.option.findAll { 
    it.'@selected' == 'selected' && it.text() == 'Premium' 
}

Upvotes: 1

Related Questions