Forum Discussion

yu's avatar
yu
Occasional Contributor
7 years ago

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=...
  • richie's avatar
    richie
    7 years ago

    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 '&nbsp;' 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 '&nbsp;' char encode with '&amp;' once the string had been transferred (after the first transfer).  There is a way around this (by using an event handler to replace this '&nbsp;' 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 '&nbsp;', the testcase will fail on the property transfer step.  If you replace the '&nbsp;' with something that doesn't need declaring (like '&amp;') 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( "&nbsp;", "&amp;" ) // 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

  • yu's avatar
    yu
    7 years ago

    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 '&amp; 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( "&nbsp;", "&amp;")
    testRunner.testCase.testSuite.setPropertyValue("Extracted", result);
    log.info(result);

     

    3) Use Property Transfer  and take  valie //div//@data-id from saved XML

     

    Best regards!