cancel
Showing results for 
Search instead for 
Did you mean: 

TestComplete support for automating REST API

Contributor

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

Think big. You are most likely to end big.
12 REPLIES 12
Regular Contributor

Re: TestComplete support for automating REST API

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 ;

}

}

 

Contributor

Re: TestComplete support for automating REST API

Hey Chicks,

 

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

 

Regards,

 

Obaid Shirwani

Think big. You are most likely to end big.
Community Hero

Re: TestComplete support for automating REST API

Hi Obaid,

 

SoapUI (http://smartbear.com/product/ready-api/soapui-ng/overview/ or http://www.soapui.org/ depending on your needs) fits better for REST API testing. SoapUI is integrated with TestComplete which means that you can run SoapUI tests from within TestComplete and get consolidated log.

Or, as Chicks already mentioned, you can use coded approach and keep everything within TestComplete.

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Community Hero

Re: TestComplete support for automating REST API

Just to back up, you can use - ActiveXObject("MSXML2.XMLHTTP.3.0") - with RESTful API's.

 

I have a full set of script extensions now built to allow my tests to write their results to TFS using it's new RESTful API.

Contributor

Re: TestComplete support for automating REST API

I have been able to achieve this using pHp and C# but still no luck with TestComplete / JS.

 

This is how my PHP code looks like:

<?php
$_url = "http://x.y.z.a/xyzqa/index.php/api/BringUsers";
$ch = curl_init($_url);
curl_setopt_array($ch, array(CURLOPT_HTTPHEADER => array('X-REST-USERNAME: admin', 'X-REST-PASSWORD: admin')));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//echo "Calling 'curl_exec':";
$_response = curl_exec($ch);

echo "<br>Closing CURL<br>";
curl_close($ch);


echo "<br><b>Decoding JSON:</b><br>";
$_jsonDecoded = json_decode($_response, true);
print_r($_jsonDecoded);
echo "</br>";

 

 

This is how my C# code looks like:

 

//01. Begin: myApp REST API Method 01
static void myAppRESTAPIMethod01()
{
string responseString = "";
string url = @"http://x.y.z.a/xyzqa/index.php/api/BringUsers";
using (WebClient client = new WebClient())
{
client.Headers.Add("X_REST_USERNAME", "admin");
client.Headers.Add("X_REST_PASSWORD", "admin");


//send web request; get web response as XML
try
{
responseString = client.DownloadString(url).ToString();
Console.WriteLine(responseString);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message);
}
}
}
//01. End: myAppREST API Method 01

 

I am unable to figure out how to fit this in TestComplete/JS

 

 

Think big. You are most likely to end big.
Frequent Contributor

Re: TestComplete support for automating REST API

@chicks Interesting, could you please explain your variables ?

Community Hero

Re: TestComplete support for automating REST API

Hi,

 

> could you please explain your variables ?

Which ones? In what code (snippet)?

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Regular Contributor

Re: TestComplete support for automating REST API


@fazlook wrote:

@chicks Interesting, could you please explain your variables ?


@fazlook  Which variables?

Frequent Contributor

Re: TestComplete support for automating REST API

Sorry for being late on this one Smiley Happy I meant the following:

 

memberAccessToken, URL, method, formData, optionalAPIaccessToken

 

New Here?
Join us and watch the welcome video:
Join the September Hub-bub to show off, learn and win