Knowledge Base Article

Checking API Status in TestComplete

Our software environment has become more API rich in the last few years. Checking the state of APIs has become a requirement in many cases to prevent wasted time in automation.

Introduction

I first saw the need to verify the state of an API several years ago with an application that used an address validation API in several of it's operations.  If this API was down or did not respond in a timely manor, many of the automated test cases would run and fail. In this case, and in others, I have found that doing a simple call to the API to check the returned status code allowed me to skip, fail or log a warning with a logical message instead of allowing the application to fail with another less direct error message due to the API issue.

The aqHttp Object

The TestComplete aqHttp object and it's methods are very useful for performing simple checks like this and are also useful for other more complex tasks like leveraging an API to return a test data set or even verifying a certain data is returned prior to running tests against the UI that depend on the data.  

More Complete API Testing 

Most proper API testing should be done using a tools like ReadyAPI or SoapUI.  Both of these tools will integrate with TestComplete  or can be used alone and will provide much more capabilities and automation options.

Code Example

Here I have provided a working example of how to code a Get request using 'aqHttp.CreateRequest' to confirm an API returns a status code of 200 and it will log the returned records.

function sendGetRequest() 
{
  let resourcePath ="https://restcountries.com/v3.1/all"
  let resourceQuery = "?fields=name,capital";
  let url = resourcePath + resourceQuery;
   
  try {
    // Send GET request
    let response = aqHttp.CreateRequest("GET", url, false).Send();
    
    // Check for successful response
    if (response.StatusCode === 200) {
      // Parse JSON response
      let allData = JSON.parse(response.Text);
      Log.Message("Total records received: " + allData.length);

      // Process each record
      allData.forEach((record, index) => {
        Log.Message("Record " + (index + 1) + ": " + JSON.stringify(record));
      });
      return true;  // Send a bool back to the calling function.
    } 
    else {
      throw new Error("Failed to fetch data. Status code: " + response.StatusCode);
    }
  } 
  catch (error) {
      Log.Error("Error during request or data processing: " + error.message);
  }
}  

Enhancements

  • You could accept parameters for the resourcePath and resourceQuery.
  • Parameterize the logging loop run or remove it.
  • Return the JSON to the calling function for use.
  • Perform other tasks based on the return code.

Conclusion

With the growing use of API calls in desktop applications and the fact that APIs are almost the foundation of any web site checking an API before a test case run is almost a requirement for consistent test runs and good error logging.  This small script can bring big rewards to your test runs and reports.

Cheers!

I hope you find it as useful as I have!

If you find my posts helpful drop me a like! šŸ‘ Leave a comment if you want to contribute  or have a better solution or an improvement. šŸ˜Ž

 Have a great day

 

 

Updated 5 months ago
Version 1.0
No CommentsBe the first to comment