Trade Me-3-Retrieve general categories API testing | Postman API automation testing

API Function

Retrieves all or part of the Trade Me category tree.

API - Retrieve general categories script development steps

API Specification -> Business Presentation -> API Testing Script Debugging -> API Testing Script Enhancement -> API Testing Script Negative Cases Extension -> API Testing Script Publishment

API Specification

  • Key request fields

Key response fields - A category tree.

Name String or null The name of the category.
Number String or null A unique identifier for the category e.g. “0004-0369-6076-”. We plan to change this to a numeric identifier (e.g. “6076”) so you should ensure you can cope with both formats.
Path String or null The full URL path of this category e.g. “/Home-living/Beds-bedroom-furniture/Bedside-tables”.
Subcategories Collection of or null The list of subcategories belonging to this category.

More API specification refers to Retrieve general categories .

  • Example request

GET https://api.tmsandbox.co.nz/v1/Categories.JSON?depth=1

  • Example response

{
    "Name": "Root",
    "Number": "",
    "Path": "",
    "Subcategories": [
        {
            "Name": "Trade Me Motors",
            "Number": "0001-",
            "Path": "/Trade-Me-Motors",
            "HasClassifieds": true,
            "AreaOfBusiness": 3,
            "CanHaveSecondCategory": true,
            "CanBeSecondCategory": true,
            "IsLeaf": false
        },
        {
            "Name": "Trade Me Property",
            "Number": "0350-",
            "Path": "/Trade-Me-Property",
            "HasClassifieds": true,
            "AreaOfBusiness": 2,
            "IsLeaf": false
        },
        ...
    ],
    "AreaOfBusiness": 0,
    "IsLeaf": false
}        

Business Presentation

  • Front-end page - Go to all categories page

home page > View all categories

  • Front-end page - Categories page

  • Business scope selection

Get all subcategories.

  • Business verification points

Verify a specific subcategory such as ‘Antiques & Collectables’ exists in the query result.

API Testing Script Debugging

  • Choose the OAuth 1.0 in the Authorization label of Postman to authorize the request.

The OAuth 1.0 is for the transaction that needs to be authorized before sending. Template likes the below:

From the API specification, parameter - depth=1 means to return all subcategories of the category tree.

So the request should be GET https://api.tmsandbox.co.nz/v1/Categories.JSON?depth=1

  • API connectivity

The connectivity test passes and gets a response example.

{
    "Name": "Root",
    "Number": "",
    "Path": "",
    "Subcategories": [
        ...
        {
            "Name": "Antiques & collectables",
            "Number": "0187-",
            "Path": "/Antiques-collectables",
            "AreaOfBusiness": 1,
            "CanHaveSecondCategory": true,
            "CanBeSecondCategory": true,
            "IsLeaf": false
        },
        ...
    ],
    "AreaOfBusiness": 0,
    "IsLeaf": false
}        
  • API checkpoint

We can know the subcategory ‘Antiques & Collectables’ exists in the query result from the connectivity testing response.

Then we get the checking code snippet below base on the example feature.

pm.test("Checking all subcategories returned, and the given subcategory exists in", function () {

    var jsonData = pm.response.json();

    var arr =[];

    var Subcategories = jsonData.Subcategories;  

    // Traverse all Subcategories from response, and find wheter a specific subcategory is in.
    for (i in Subcategories){

        var Name = Subcategories[i].Name;

        arr.push(Name);

        if(Name == "Antiques & Collectables"){

            var SpecificSubcategories = Name;

        }
    }
    
    // checkpoint: whether subcategory - 'Antiques & Collectables' exists in the response
    if(SpecificSubcategories !== []){

        console.log("Subcategory - Antiques & Collectables existed");

    }
    
});

Run script to verify the checkpoint works from console log.

API Testing Script Enhancement

  • Enhance the script, such as to add the transactional function, exceptional verification, parameterization, etc.
pm.test("Checking all subcategories returned, and the given subcategory exists in", function () {

    console.log("Post-Transaction: CheckingtheCountsofGivenSubcategory Start");

    var jsonData = pm.response.json();

    var arr =[];

    if (typeof(pm.response.json().Subcategories) !== "undefined"){

        var Subcategories = jsonData.Subcategories;  

        // Traverse all Subcategories from response, and find wheter a specific subcategory is in.
        for (i in Subcategories){

            var Name = Subcategories[i].Name;

            arr.push(Name);

            if(Name == "Antiques & collectables"){

                var SpecificSubcategories = Name;

            }
        }
        
        console.log("Subcategories counts to: " + jsonData.Subcategories.length);

        console.log("All subcategories name listed below: ");

        console.log(arr);

        // checkpoint: whether subcategory - 'Antiques & collectables' exists in the response      
        if(typeof(SpecificSubcategories) !== "undefined" && SpecificSubcategories.length >= 1 ){

            console.log("Subcategory - Antiques & collectables existed");

            console.log("Post-Transaction: CheckingtheCountsofGivenSubcategory Pass");
            
        }
        else{

            console.log("Subcategory - Antiques & collectables is not existed");

            console.log("Post-Transaction: CheckingtheCountsofGivenSubcategory Failed");

        }
     
    }

    else{

        console.log(pm.response.json());

        console.log("Didn't catch the Subcategory field");
        
        console.log("Post-Transaction: CheckingtheCountsofGivenSubcategory Failed");

    }

});

Run the final script and to verify from front-end page or console log.

API Testing Script Negative Cases Extension

We could test API using the ‘Equivalent Class Partitioning’ and ‘Boundary Value Analysis’ as we did in manual testing cases.

These API test scripts are doing the negative direction of the business, to be an extension to the above API positive case.

From the API specification, parameter - depth=-10 can be an ‘out of boundary value’ case that expects to return an exception if we send a request with this parameter.

So the request should be https://api.tmsandbox.co.nz/v1/Categories.JSON?depth=-10

  • API checkpoint

Run the script, we get the sample response as blow.

{
    "Name": "Root",
    "Number": "",
    "Path": "",
    "AreaOfBusiness": 0,
    "IsLeaf": false
}

We don’t see any error message from the response for handling exceptional value with the depth field when sending this request.

So we can report this as a bug to the developer.

API Testing Script Publishment

Move script from debug folder to public folder when we finished API positive and negative cases.

Then wait to be published to the GitHub specific repo combining with other scripts using the CICD tool.

Copyright

This work is licensed under CC BY-NC-ND 4.0 .