Forum Discussion

obaid_shirwani's avatar
obaid_shirwani
Contributor
10 years ago

TestComplete support for automating REST API

Hi folks,

 

I have to automate REST API's using TestComplete. According to a previous thread REST-web-services, there was no support for automating REST API's at that time. I could not find it the current documentations.

 

Is it still not available in the latest versions of TestComplete?

 

Regards,

 

Obaid Shirwani

  • chicks's avatar
    chicks
    Regular Contributor

    Hi Obaid,

     

    I ended up automating these programmatically using javascript without any explicit support from TestComplete, though my tests still run from TC.

     

    Here's some sample code:

     

    function v2_getVirtualCardNumber (token , memberID, progUDK)

    {

     

    var message = baseURL + "v2/" + progUDK + "/members/" + memberID + "/cards" ;

     

    return http_request_member( token, message, "GET", "" );

    }

     

     

    function http_request_member (memberAccessToken, URL, method, formData, optionalAPIaccessToken) {

    Log.Message( "token " + memberAccessToken );

    Log.Message(" message in additional info", URL);

    Log.Message(" formData in additional info", formData);

     

    var XmlHttpRequest;

     

    XmlHttpRequest = null;

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

     

    if ( ( method == undefined) || (method == "") ) {

    method = "GET";

    }

     

    var curl;

    if ( method == "GET") {

    curl = "curl --insecure -H \"Content-type: application/json\" -H \"memberAccessToken:"+memberAccessToken+"\" -X " + method + " " + formData + " " + URL;

    }

    else {

    curl = "curl --insecure -H \"Content-type: application/json\" -H \"memberAccessToken:"+memberAccessToken+"\" -X " + method + " -d " + formData + " " + URL;

    }

    Log.Message(" curl in additional info=", curl+"=");

     

    // XmlHttpRequest.open( Method, URL, Asynchronous, UserName, Password)

    // method e.g. GET, POST, HEAD, PUT, DELETE, OPTIONS...

    // Asynchronous = true, do not wait on a server response, false, pause current execution utnil the request is complete

    XmlHttpRequest.open(method, URL, false);

    // Name, Value

    XmlHttpRequest.setRequestHeader("Content-Type", "application/json");

    XmlHttpRequest.setRequestHeader("memberAccessToken", memberAccessToken);

    if ( (optionalAPIaccessToken != undefined) && ( optionalAPIaccessToken != EMPTY) ) {

    Log.Message("header access_token " + optionalAPIaccessToken + " added to request");

    XmlHttpRequest.setRequestHeader("access_token", optionalAPIaccessToken);

    }

    XmlHttpRequest.send(formData);

    try {

    Log.Message("http_request: reply in additional info", XmlHttpRequest.responseText);

    return XmlHttpRequest.responseText;

    }

    catch (e) {

    return "Error" + e ;

    }

    }

     

    • obaid_shirwani's avatar
      obaid_shirwani
      Contributor

      Hey Chicks,

       

      I am extremely thankful for this. Let me investigate on this. Will update my findings after implementing your work.

       

      Regards,

       

      Obaid Shirwani

    • fazlook's avatar
      fazlook
      Frequent Contributor

      chicks Interesting, could you please explain your variables ?

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        > could you please explain your variables ?

        Which ones? In what code (snippet)?