How extract html string from json response.
Hi all! Would you be so kind and help with this -
response from server is this in Json format:
{
"Success": true,
"Message": "OK",
"Result": "\r\n<div class=\"row contact table-row\" data-id=\"551215\" data-disabled=\"False\" data-keydisabled=\"False\">\r\n <input id=\"returnCompanyId\" name=\"returnCompanyId\" type=\"hidden\" ...
"Title": ""
}
I used Transfer and have string in TestCase property
<div class="row contact table-row" data-id="551215" data-disabled="False" data-keydisabled="False"> <input id="returnCompanyId" name="returnCompanyId" type="hidden" value="374982" /> <div class="icon-cell col-md-1-half table-cell"> ....
Is it possble to parse dinamic value of data-id with groovy script?
Can anybody help how to parse this value? Thank you!
Ok - it's took me a while but I've got it working using XPATH (like the CDATA handling passing the content from one property to another) - but there is a problem with the content.
you have a value of &nbps; in your content that the parser doesn't like - once I'd removed this - I got it working with the embedded functionality and I used an event handler to replace the '&nbps;' char anyway.
Just so you can follow what I did - I created a VIRT that would return your response
I ran the request and did a property transfer of the Result object, passing the contents of the Result object (html string) to a property (see next image)
I then did a transfer of the html string (in the Properties step) to a second Property, but specifying the data-id tag attribute (see next image)
as you can see - the data-id attribute value was successfully picked up in the transfer (THERE IS A PROBLEM HERE - and I'll come back to it) - for completeness the next image is the Properties step - here you can see the 2 properties that were extracted successfully
So - all looks ok - right? Well there is a problem using XPATH to extract the html attribute value you need. There is an undeclared xml entity (not one of the standard 5) within your content - the value is ' ' within the html string that will cause a problem for the parser.
If you search your response you can find it - the above approach will fail BECAUSE the content includes this. I cheated inasmuch that I initially manually replaced the ' ' char encode with '&' once the string had been transferred (after the first transfer). There is a way around this (by using an event handler to replace this ' ' with ANYTHING that is standard xml) - however - it depends how important the content is in your tests before you start thinking about altering the response using an event handler.
SO - if you don't replace the ' ', the testcase will fail on the property transfer step. If you replace the ' ' with something that doesn't need declaring (like '&') the test case will run through executing successfully (extracting the values into the Properties step for later use)
As I state above - to get it to run successfully without any manual intervention - I used the 'RequestFilter.afterRequest event handler. The code for the event handler was as follows:
def content = context.httpResponse.responseContent content = content.replaceAll( " ", "&" ) // this replaces the nonbreakspace char for the ampersand code which doesnt need to be declared in xml log.info( content ) context.httpResponse.responseContent = content
So - this works completely automatically once I'd added in that event handler. its not pretty and some of the other lads may have some whizzy way of using a groovy step in the test case to do all the work for you (but I can't help with that) - but if there's no problem replacing the nonbreakspace entity - the above will get you where you need to (i.e. extracting attributes from the html string)
Hope this helps!
Cheers,
richie
\zx
asas
Hi Richie,
thank you a lot for you help!
I used your solution and it works! it's wonderful!
I don't know what is 'RequestFilter.afterRequest event handler.
And did this solution in longer way but with the same logic as you discribed .
Might it can be helpful for somebody:
1) Save Json request in Property1 using PropertyTransfer.
2) Then in Groovy script invoked this property and replace &nbps with '& as you discrabed.
and set this XML in an another Property with Groovy
Code is :
import com.eviware.soapui.support.types.StringToStringMap
def result = testRunner.testCase.testSuite.getPropertyValue("TempTarget")
result = result.replaceAll( " ", "&")
testRunner.testCase.testSuite.setPropertyValue("Extracted", result);
log.info(result);3) Use Property Transfer and take valie //div//@data-id from saved XML
Best regards!