Trade Me-7-Add a listing to your watchlist API testing | Postman API automation testing

API Function

Adds a listing to the authenticated user’s watchlist.

API - Add a listing to your watchlist 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

More refer to: Add a listing to your watchlist

Key response fields - Details on whether the operation succeeded.

Attempting to remove an auction that doesn’t exist or is not on the watchlist will not produce an error.

Success Boolean Indicates whether the operation was successful.
Description String or null The description of the error, if the operation failed.

Example request

POST https://api.tmsandbox.co.nz/v1/MyTradeMe/WatchList/2149294825.JSON

  • Example response:
{
    "Success": true,
    "Description": "Success"
}

Business Presentation

  • Front-end page - Closing soon item detail page

Login > View all Closing soon in all regions > Add a listing to your watchlist

  • Front-end page - Watchlist page

  • Business scope selection

Chained combination transactions

Retrieve your watchlist -> Remove a listing from your watchlist -> Retrieve closing soon listings -> Add a listing to your watchlist(I’m here)

Fetch a random item on the closing soon listings from upstream transaction ‘Retrieve closing soon listings ’, and add this item to the watchlist.

  • Business verification points

Checking selected items can add to the watchlist.

API Testing Script Debugging

  • Assembling the pre-request ‘Retrieve closing soon listings’

The pre-request script’s main coding logic is as below:

  1. Pre-request the API - ‘Retrieve closing soon listings’ to fetch all items on the 1st page of closing soon listings.
console.log("Pre-Transaction: RetrieveClosingSoonListings Start");

const pre_request = {

  url:  pm.environment.get("BaseUrl")+'/v1/Listings/closing.JSON?rows=1000',

  method: 'GET',

  header:['Content-Type:application/json', 'Authorization:OAuth oauth_consumer_key="your Consumer Key",oauth_token="your OAuth Token",oauth_signature_method="PLAINTEXT",oauth_timestamp="1623834029",oauth_nonce="Zr5dzIAuVON",oauth_version="1.0",oauth_signature="your Consumer Secret%26your OAuth Token Secret"'],

  body: {
  }

};  
...

​ Note: how to get Consumer Key, Consumer Secret, OAuth Token, and OAuth Token Secret, please refer to Preparation before API testing demo

  1. Randomly choose 1 item from all the fetched items, and store the item as a parameter for later API - ‘Add a listing to your watchlist’ to use.
pm.sendRequest(pre_request, function (err, response) {

    if (err) {

        console.log(err);

        console.log("Pre-Transaction: RetrieveClosingSoonListings Failed");

    } 

    else {

        pm.test("Get all the closing soon ListingId", function () {      

            var jsonData = response.json();

            var List = jsonData.List;

            var ListingIdsCount = jsonData.TotalCount;// Get the 250 closing soon ListingIds from 1st page

            var PageSize = jsonData.PageSize;

            var arr = [];

            // Traversal json object to get all the ListingId
            if (List != ""){

                for (i in List){

                    var ListingId = List[i].ListingId;

                    arr.push(ListingId);

                }

            }

            // Store all the ListingId as a variable
            if(i == (Number(PageSize)-1)){

            postman.setEnvironmentVariable('ListingIds', arr);

            postman.setEnvironmentVariable('ListingIdsCount', ListingIdsCount);

            }

        });
       
        // Randomly get a ListingId from ListingIds for later transaction use
        var ListingIdsCount = pm.environment.get("ListingIdsCount");  

        var arrListingIds=pm.environment.get("ListingIds").split(",");

        postman.setEnvironmentVariable('ListingIdAddToWatchList', arrListingIds[Math.floor(Math.random() * Number(ListingIdsCount))]);

        var ListingIdAddToWatchList = pm.environment.get("ListingIdAddToWatchList");

        console.log("From "+ListingIdsCount +" closing soon items randomly choose the: "+ListingIdAddToWatchList+" waiting to be added to the watchlist");

        console.log("Pre-Transaction: RetrieveClosingSoonListings Pass");
        
    }
  • 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:

How to get Consumer Key, Consumer Secret, OAuth Token, and OAuth Token Secret, please refer to Preparation before API testing demo

  • Assembling the request - ‘Add a listing to your watchlist’

Base on the API specification, parameter - listingId means the ID of the listing to add to the watchlist.

And the listingId origin from the response of pre_request ‘Retrieve closing soon listings ’, and it has already been set to environment variables: {{ListingIdAddToWatchList}}.

So the request should be POST https://api.tmsandbox.co.nz/v1/MyTradeMe/WatchList/{{ListingIdAddToWatchList}}.JSON

  • API connectivity

The connectivity test passes and gets a response example.

{
    "Success": true,
    "Description": "Success"
}
  • API checkpoint

Using the post_request: API - Retrieve your watchlist to check whether the given item has been added onto the watchlist.

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

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

var ListingIdAddToWatchList = pm.environment.get("ListingIdAddToWatchList");

const post_request = {

  url:  pm.environment.get("BaseUrl")+'/v1/MyTradeMe/Watchlist/All.JSON',

  method: 'GET',

  header:['Content-Type:application/json', 'Authorization:OAuth oauth_consumer_key="your Consumer Key",oauth_token="your OAuth Token",oauth_signature_method="PLAINTEXT",oauth_timestamp="1623834029",oauth_nonce="Zr5dzIAuVON",oauth_version="1.0",oauth_signature="your Consumer Secret%26your OAuth Token Secret"'],

  body: {
  }

};

pm.sendRequest(post_request, function (err, response) {

    if (err) {

        console.log(err);

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

    } 

    else {

        // Confirm whether ListingIdAddToWatchList existed in the Watchlist
        pm.test("Checking ListingId " +ListingIdAddToWatchList +" has been added", function () {

            console.log(response.json());

            var jsonData = response.json();

            var Listdata = jsonData['List'];

            var count = 0;

            for(i = 0; i < Listdata.length; i++) {

                if (ListingIdAddToWatchList == Listdata[i].ListingId){

                    console.log("ListingId " +ListingIdAddToWatchList + " has been added to the watchlist");

                    console.log("Post-Transaction: CheckingAddToWatchList Pass");

                    count = 1;

                }

            }

            if (count == 0){
                    
                console.log("ListingId " +ListingIdAddToWatchList + " hasn't been added to the watchlist");

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

        });

    }
    
});

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.

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

After the ‘Add a listing to your watchlist’ operation, see the watchlist.

API Testing Script Negative Cases Extension

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.

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 .