Forum Discussion

nishay_patel's avatar
nishay_patel
Occasional Contributor
12 years ago

Web Service returns an array of objects

I am using a web service in which a method returns an array of custom objects.



Example:

Method Name - GetProducts

Input - String

Output - ProductType[]



where ProductType is an object type that is defined in the web service (just a collection of strings).



My problem is after i parse the response returned from the web service, I have no idea how to access this object.  I am working in Jscript, so I know I need to convert the array from a VB safe array to a Jscript array, but what is the syntax for doing this?





var ResponseXml = XmlHttpRequest.responseXML;

var response = webservice.ParseResponse("GetProducts", ResponseXml);



The "response" object should contain a reference to the array of ProductTypes. Now, how do I go about accessing the array from the response?



var array = (new VBArray(________?______).toArray();







Thanks in advance

2 Replies

  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 1 rankChampion Level 1
    Hi Nishay,



    To provide you with more precise answer, I would like to ask you either for the web address of the tested web service if it is accessible from the internet or for the files from the WebServices subfolder of TestComplete project.

    Without this informationI can say the following generic things:

    -- I did not use JScript for web services test but DelphiScript and VBScript, so the code might need convertion of arrays for JScript;

    -- I also did not use .ParseResponse because according to my understanding it is required only when the whole request to web service is created by test code but not using the means provided by TestComplete (http://smartbear.com/forums/f73/t48530/how-to-run-the-webservices-test/, http://smartbear.com/forums/f75/t59793/adding-header-to-the-web-services-in-test-compl/);

    -- When response returned an array of objects, usually the code like this worked:

    Deals := WebService.DealSearch(criterions);

    WebService.GetDealInfo(StrToInt(Deals.item[0].text), dsResult);

    i.e. TestComplete was able to extract from the WSDL file that the call to some method returns an array and provide access to the array elements.

  • nishay_patel's avatar
    nishay_patel
    Occasional Contributor
    Thanks for the reply.  My goal is to not have the webservice stored in TestComplete, rather just post the soap envelope directly through my script.  parseResponse works perfectly when no portion of the returned object is an array, but it appears the array returned is a generic object has cannot be distinguished from a child object.



    Anyway, I used a brute force method to use to regexp on the returned XML from the XmlHttpRequest post.




        var XmlHttpRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0");


        XmlHttpRequest.open("POST", wsdl, false);


        XmlHttpRequest.send(test);   //test is the soap envelope in xml format (actually just a string with its content)


        


        //parse the response


        var ResponseXml = XmlHttpRequest.responseXML;


        var re = new RegExp("Blah(.*)Blah", "g");


        var match;


        var results = new Array();


        while (match = re.exec(ResponseXml.xml)) {


          results.push(match[1]);


        }


        return results;



    This will take the reponse given in XML format and read all of the matches of that regexp type and push it onto a single array.



    Thanks again for the response!