Trade Me-5-Remove a listing from your watchlist API testing | Postman API automation testing

API Function

Removes a listing from the authenticated user’s watchlist.

API - Remove a listing from 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

  • 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.

More API specification refers to Remove a listing from your watchlist .

  • Example request

DELETE https://api.tmsandbox.co.nz/v1/MyTradeMe/WatchList/0000000000.JSON

  • Example response

{
    "Success": true,
    "Description": "Success"
}

Business Presentation

  • Front-end page - Watchlist page

Login > Watchlist > View Watchlist > Delete > Confirm to delete

  • Front-end page - Confirm to remove items page

  • Business scope selection

Chained combination transactions

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

Fetch all ‘ListingId’ from the response of upstream transaction ‘Retrieve your watchlist ’ as the request fields.

Pre_repquest ‘Remove a listing from your watchlist ’ removes all ListingId but leave the last one from the whatchlist.

Removes the last ListingId from the whatchlist.

  • Business verification points

Checking all items in the watch list have been removed.

API Testing Script Debugging

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

  1. Pre-request the API - Retrieve your watchlist to fetch all items in the watch list.
console.log("Pre-Transaction: RetrieveYourWatchlist Start");

// oauth_signature = Consumer Secret%26OAuth Token Secret
const pre_request_retrieveyourwatchlist = {
    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: {
    }

};
...

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

  1. All the fetched items should exclude those fields is ‘BidCount’ = 1 to get all the items that allow being removed from the watch list. The ‘BidCount’ field means that the item has been bidden, couldn’t be deleted from the watch list.
pm.sendRequest(pre_request_retrieveyourwatchlist, function (err, response) {

    if (err) {

        console.log(err);

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

    }

    else {

        // Get all ListingId on the Watchlist
        pm.test("Get all ListingId on the Watchlist", function () {

            var jsonData = response.json();

            var Listdata = jsonData['List'];

            var arr = [];

            for (i = 0; i < Listdata.length; i++) {
                
                // bidding items can't be deleted, need to exclude them
                if (Listdata[i].BidCount != 1) {

                    arr.push(Listdata[i].ListingId);

                }

            }

            postman.setEnvironmentVariable('WatchlistListingIdCount', arr.length);

            if(arr.length < 1){

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

                console.log("WatchlistListingIdCount: " +WatchlistListingIdCount + ", nothing items need to be deleted.");

            }

            else {

                postman.setEnvironmentVariable('WatchlistListingId', arr);

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

                console.log(All the can be deleted ListingId on the Watchlist: ");

                console.log(WatchlistListingId);

            }
        
        console.log("Pre-Transaction: RetrieveYourWatchlist Pass");
        
        });
...
  1. If fetched items from the watch list <=0, there are no items in the watchlist. Just initiate a meaningless ‘ListingId’.
        var WatchlistListingIdCount = pm.environment.get("WatchlistListingIdCount");

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

        if (WatchlistListingIdCount <= 0) {

            //Grant a meaningless value to LastListingId
            postman.setEnvironmentVariable('LastListingId', "0000000000");

        }
...        
  1. If fetched items from the watch list =1, store the ‘ListingId’ for later API request of ‘RemoveAListingFromYourWatchlist’ to use.
        else if (WatchlistListingIdCount == 1) {

            postman.setEnvironmentVariable('LastListingId', WatchlistListingId[(Number(WatchlistListingIdCount) - 1)]);

        }
...        
  1. If fetched items from the watch list >1, assembling the pre-request ‘Remove a listing from your watchlist’ ’ to remove all the items but the last item in the watch list, then leave the last item to be removed by the later transaction ‘Remove a listing from your watchlist ’.
        else {

            //Delete ListingId from Watchlist, but leave the last ListingId for later transaction to delete
            console.log("Pre-Transaction: RemoveAListingFromYourWatchlist Start");

            postman.setEnvironmentVariable('LastListingId', WatchlistListingId[(Number(WatchlistListingIdCount) - 1)]);

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

            console.log("LastListingId: " + LastListingId);

            //Exclude the last ListingId
            for (var i = 0; i < (WatchlistListingId.length - 1); i++) {

                const pre_request_removealistingfromyourwatchlist = {

                    url: pm.environment.get("BaseUrl") + '/v1/MyTradeMe/WatchList/' + WatchlistListingId[i] + '.JSON',
                    method: 'DELETE',

                    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(pre_request_removealistingfromyourwatchlist, function (err, response) {

                    if (err) {

                        console.log(err);

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

                    }

                    else {

                        // Output the ListingId that has been deleted
                        pm.test("Delete ListingId from the Watchlist, but leave the LastListingId: " + LastListingId, function () {

                            // console.log(response.json());
                        });
                    }

                });

            }

            console.log("Pre-Transaction: RemoveAListingFromYourWatchlist Pass");

        }

    }

});

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

  • 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 delete request - Remove a listing from your watchlist

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

And the listingId origin from the response of pre_request ‘Retrieve your watchlist ’, and it has already been set to environment variables: {{LastListingId}}.

So the request should be DELETE https://api.tmsandbox.co.nz/v1/MyTradeMe/WatchList/{{LastListingId}}.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 it still has items on the watchlist that can be deleted.

Check whether the functionality of pre_request ‘Retrieve your watchlist ’ works;

and it can exclude those items that cannot execute the delete operation from its response for another pre_request transaction ‘Remove a listing from your watchlist ’ to use;

and it can remain the last item from its response for the downstream transaction ‘Remove a listing from your watchlist ’ to use.

Check whether the functionality of ‘Remove a listing from your watchlist ’ works, and it can remove the last item from watchlist.

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

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

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: {
  }
  
};

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

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

    if (err) {

        console.log(err);

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

    } 

    else {

        // Confirm whether all ListingId have been deleted from the Watchlist
        pm.test("Checking LastListingId: " +LastListingId +" has been deleted", function () {

            var jsonData = response.json();

            var Listdata = jsonData['List'];

            var arr = [];

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

                //bidding items can't be deleted, need to exclude them
                if(Listdata[i].BidCount != 1){

                    arr.push(Listdata[i].ListingId);

                }

            }
            
            if (arr.length == 0){

                console.log("Current count of ListingId that can be deleted in the watchlist: " +arr.length);

            }

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

    } 

});

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.

Display on the watchlist page before removing operation.

Display on the watchlist page after removing operation.

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 .