logo logo

Advanced API Test Automation and Validation Flows

Validation Flows - Advanced API Test Automation and Validation Flows

End-to-end API testing flows can involve multiple input sources, such as HTML, Databases, Telnet/SSH, Mobile, Shell, Data files, other RESTful APIs, validation flows and more. TestProject‘s test automation platform allows not only testing your APIs manually, but also to complete end-to-end API based test automation flows, schedule and run them periodically, get execution reports and all of that without any third party tools and without a single line of code 🚀

This hands-on tutorial consists of step-by-step examples and real automated test flows, shared here for you to use and practice while automating your APIs. In this chapter (Chapter #5), we will walk through advanced API test automation and validation flows. You can access the tests mentioned in the examples below by using the following shared test links:

  1. RESTful API GET Call and JSON Schema Validation Test Example
  2. RESTful API GET Call with Extracting Multiple Values from JSON Response Test Example
  3. Basic Authorization Within RESTful API Call Test Example
  4. RESTful API Urlencoded Format Requests Example
  5. Executing API Tests with  Dynamic Parameters and Predefined Data Set

Table of Contents – Automating End to End API Testing Flows

  1. Overview – Automating End to End API Testing Flows
  2. Chapter 1 – Basic API Test Automation
  3. Chapter 2 – API Test Automation Flows Combined with Mobile Functional Test
  4. Chapter 3 – API Test Automation Flows Combined with Database Flows
  5. Chapter 4 – API Test Automation with Combination of Shell Commands and Local Files
  6. You’re here → Chapter 5 – Advanced API Test Automation and Validation Flows
  7. Chapter 6 – Scheduling API Automation Flows and CI/CD Execution

RESTful API GET Call and JSON Schema Validation Test Example

JSON Schema is a vocabulary that allows you to validate your APIs JSON response. The RESTful API client addon has three fields relating to this functionality:

RESTful API GET Call and JSON Schema Validation Test Example

  • The SchemaPath field will be your local path to your schema file, a URL to the schema, or the schema itself.
  • The CreateFile is a boolean that can be either true/false to decide if you’d like to store your validation result in a file.
  • The SchemaValidationOutputFilePath should be used in case the previous field has been set to true, this will be the location on your local machine where the validation result file will be created.

Note: the file will be created only in case there are violations.

In the following example, we will once again test the NASA public API.

  • The URI endpoint: https://api.nasa.gov/insight_weather/
  • The query parameters are: api_key=DEMO_KEY&feedtype=json&ver=1.0

For this example, the schema has purposely been set to fail and create a file, see the ‘DOES_NOT_EXIST’ required key.

RESTful API GET Call and JSON Schema Validation Test Example

RESTful API GET Call and JSON Schema Validation Test Example

After the execution is complete, we will notice an additional message for schema validation at the end of the step’s execution results:

RESTful API GET Call and JSON Schema Validation Test Example

If we check the path specified in the output file path, we will find a file containing all the violations our response has:

RESTful API GET Call and JSON Schema Validation Test Example

RESTful API GET Call and JSON Schema Validation Test Example

RESTful API GET Call with Extracting Multiple Values from JSON Response Test Example

Each action in the RESTful API Client Addon can extract a single JSON path from its respective response.

In case you would like to extract more than one path at a time, you can do so using one of the utilities available in the JSON Operations Addon. You can extract up to 10 JSON paths from a single JSON value at once.

JSON Operations

You can pass the output parameter of your API call as an input parameter to the Extract multiple JsonPaths action along will all your JSON paths separated by newlines.

RESTful API GET Call with Extracting Multiple Values from JSON Response Test Example

You can store all their outputs in as many parameters as you might need based on your amount of input paths.

RESTful API GET Call with Extracting Multiple Values from JSON Response Test Example

Basic Authorization Within RESTful API Call Test Example

If your API endpoint requires basic authorization, you can perform it using the TestProject RESTful API Client Addon by providing the token as a header in your request. Most clients that require basic authorization require it in the following format:

Authorization = Basic username:password

Where username:password is converted to a Base64 encoded string.

You can either come pre-prepared with your Base64 encoded token or use the TestProject Base64 Encoder/Decoder Addon to pre-process your token.

Basic Authorization Within RESTful API Call Test Example

Once completed, your API call should look like this in the TestProject Test Recorder step editor:

Basic Authorization Within RESTful API Call Test Example

RESTful API Urlencoded Format Requests Example

To create requests which are in the urlencoded application format, specify the format in the appropriate field in the request and separate the body parameters using ‘&’.

Your request should look like this:

Validation Flows- RESTful API Urlencoded Format Requests Example

Executing API Tests with Dynamic Parameters and Predefined Data Set

To execute API tests using dynamic parameters with a predefined data set, create a data-driven API test.

In the following example, the public MetaWeather API will be used to get weather statistics in various locations around the world. As we have seen in previous examples, create your HTTP request in your test:

Validation Flows- Executing API Tests with Dynamic Parameters and Predefined Data Set

Now, let’s parameterize the dynamic part of your request: in the above example, the query value will be parameterized. Select ‘Use Parameter’:

Executing API Tests with Dynamic Parameters and Predefined Data Set

And create a new parameter.

Executing API Tests with Dynamic Parameters and Predefined Data Set

Afterward, replace the existing hardcoded value with a parameter.

Validation Flows - Executing API Tests with Dynamic Parameters and Predefined Data Set

Once you’re done creating all parameters, exit the test recorder and create a data source file for your test.

You can have one automatically generated for you, by selecting the ‘Data Source Template’ option from the test you need a data source of:

Data Source Template

Open the CSV file, notice that the column header must be the name of your parameter in your test:

Data Source Template

Under the header, you can fill in the parameter values, each row in the CSV file will be a separate execution of the test with all the parameters receiving the values in that row.

Once you’re done creating the CSV file, upload it to your project as a data source by going to the ‘Data Sources’ section and selecting ‘Add a data file’:

Validation Flows - Add Data Source File to TestProject

Add Data Source File to TestProject

Now when you run the test, you can select to use the data source:

Add Data Source File to TestProject

Add Data Source File to TestProject

Or you can create a job, and attach the data source to the test permanently:

Executing API Tests with Dynamic Parameters and Predefined Data Set

Executing API Tests with Dynamic Parameters and Predefined Data Set

💥 TAKE ME TO CHAPTER #6 💥

About the author

David Goichman

Automation has been a great passion of mine from day one. Software Engineer at TestProject.

Leave a Reply

FacebookLinkedInTwitterEmail