Oct 8, 2020

API Testing 'A Beginners View': How to use JavaScript Arraylist in POSTMAN using counter? & How to reset the counter after run?

Q. 1 How to use JavaScript Arraylist in POSTMAN

Let assume that you have an array (or any collection) of items, each of which you need to pass into the request one by one. How do we do this? 


This was one of my major questions that were running through my mind these days after knowing how to store a list of response values as variables. On doing some R&D and checking with friends I got to know about the usage of counters and I was able to solve my problem. So today let’s check out how I tackled this.


You need to do the below things as a start in POSTMAN:





Now in the Request on «Pre-request Script» tab you need:
  • Get the array of items from the variable that you prepared to hold the array of records.
  • Get an index for current request from variable;
  • Set an item according to an index into another variable for the current request.
  • Increment index and save a new value after current request. It will be used for request a new item on the next iteration;
Example is below:

let value = pm.environment.get("countArr");

let item = pm.environment.get("itemOfArray");

pm.environment.set("ABC", value[item]);

pm.environment.set("itemOfArray", Number(item) + 1);


Now run your single request in Collection Runner. Before starting you need:

  • Set a number of iterations = quantity of your itemOfArray;
  • Iterations = array.length


Q.2 How to reset the counter after a run?

The next thing while working popped my mind was about the reset of the counter, why to manually reset it after every run, then on doing some googling work got an idea of creating a pre-request script in the previous request which pushes the response value to an array to reset the counter after the whatever static iterations we want the counter to be reset. In my case I wanted the counter to be reset after 10 iterations so used the Pre-Request script option and tried the below snippet:

//resetting back to 0 the counter

let count_array = pm.environment.get("itemOfArray");

console.log(count_array );

    if ((count_array  == 10)) 

    {

        pm.environment.set("itemOfArray","0");

        console.log(count_array);

    }


A simple way of resetting 😊 and it worked for me.


Oct 7, 2020

API Testing 'A Beginners View': How to push list of values into a array and store as variable in POSTMAN using javascripting

In API testing sometimes we need to store a list of response values from one request, that needs to be used in another request without any usage of a data-driven approach. In such a situation, Javascript has an awesome method Array.push() Method. 

Recently at work, I had a situation to store a list of values wherein got to know about the Array.push() method, so thought of sharing with all who are new to API testing like me :-)


The JavaScript Array.push() method adds a new element to the end of the array. When doing so, the array’s length property increases by one. After adding the new element to the end of the array, this method returns the new length of the array.


Example # Used in my daily work


var resp = JSON.parse(responseBody); //parsing the JSON response

console.log(resp); //printing the response in console

countArr = []; //array that would store the list of values

for (var i=0; i<resp.length; i++) //this will loop in through the data in response

{

//pushing the attribute value from response to the array

countArr.push(`${resp[i].attributes.value}`);

console.log(countArr);

//storing the array as an environment variable for further use

pm.environment.set('countArr', (countArr));

}


In Example, our array starts out with zero elements. The first console.log(resp) allows you to print the parsed JSON response in the console. We then loop in through the response and use the Array object’s push() method to add elements to the array ‘countArr’. Notice that when we do this, we wrap the call in a console.log() call, allowing us to see that the push() method has returned the list of records as expected. Then, we have used the pm.environment.set("variable_key", "variable_value"); to store the array as an variable for further use in the next requests.

Oct 5, 2020

API Testing 'A Beginners View': JavaScript push() Method

The push method adds values to the end of an array. The push method depends on the length property to determine where to start inserting the given elements. It changes the length of the array by the number of values added to the array. Syntax: arr.push(value1, value2 ....., valueN]]) Parameters: "valueN" - The value1(s) to add to the end of the array. Return value: This method returns the new length of the array after inserting the values into the array. Explanation with Example:
Response Body:












Test Script:
var resp = JSON.parse(responseBody);

//API Testing 'A Beginners View': JavaScript push() Method
accept_encoding = [];
var split = "gzip, deflate, br";
var split_value = split.split(",");
console.log("accept-encoding is: " + split_value);
accept_encoding.push(`${split_value[0]}`);
accept_encoding.push(`${split_value[1]}`);
accept_encoding.push(`${split_value[2]}`);
console.log(accept_encoding);
pm.environment.set("accept_encoding", (accept_encoding));

Console Output:





In the above example, I have used the previously published posts split() method and continued to add the push() method to store a list of values in an array.
The accept_encoding.push() method is used to push the split values into the "accept_encoding" array. Later I’m storing the array as an environment variable.

Environment Variable after the script looks as below:



Oct 2, 2020

API Testing 'A Beginners View': JavaScript split() Method

strg.split() method is used to split the given string into an array of strings i.e as substrings using a specified separator provided in the argument.

Syntax:

strg.split(separator, limit)


  • separator: (Optional) It is used to specify the regular expression that is being used for splitting the string. If the separator is unspecified then the entire string becomes one single array element. The same also happens when the separator is not present in the string. If the separator is an empty string (“”) then every character of the string is separated.

  • limit: (Optional) Defines the limit on the number of splits to be done in the given string. 

  • Return value: This function returns an array of strings that is formed after splitting the given string at each point where the separator occurs.


Example:

Response Body:











Test Script:

var resp = JSON.parse(responseBody);

//API Testing 'A Beginners View': JavaScript split() Method
    var str = "PostmanRuntime/7.26.8";
    var split_value = str.split("/");
    console.log("user-agent is: " + split_value[1]);
    //Store the split value in an environment variable
    pm.environment.set("user-agent", (split_value[1])); 

Console Output:

user-agent is: 7.26.8


In this example, the function split() splits str wherever ”/“ occurs and returns the index 1 value of the string in variable str.


Oct 1, 2020

API Testing 'A Beginners View': Lesson Learnt - Rest API Response Codes

In this post, I have shared the list of Response Codes that I learnt and that can be seen while performing REST API testing over POSTMAN.

  • 200 Codes: When a request is accepted and processed successfully at the server end 200 series are triggered.

200 – OK

201 – Created

202 – Accepted

203 – Non-Authoritative Information

204 – No Content

205 – Reset Content

206 – Partial Content

207 – Multi-Status

208 – Already Reported

226 – IM Used

  • 300 Codes: These codes are related to URL Redirection.

300 – Multiple Choices

301 – Moved Permanently

302 – Found

303 – Check Other

304 – Not Modified

305 – Use Proxy

306 – Switch Proxy

307 – Temporary Redirect

308 – Permanent Redirect

  • 400 Codes: These codes denote the client-side error.

400 – Bad Request

401 – Unauthorised

402 – Payment Required

403 – Forbidden

404 – Not Found

405 – Method Not Allowed

406 – Not Acceptable

407 – Proxy Authentication Required

408 – Request Timeout

409 – Conflict

410 – Gone

411 – Length Required

412 – Precondition Failed

413 – Payload Too Large

414 – URI Too Long

415 – Unsupported Media Type

416 – Range Not Satisfiable

417 – Expectation Failed

418 – I’m a teapot

421 – Misdirected Request

422 – Unprocessable Entity

423 – Locked

424 – Failed Dependency

426 – Upgrade Required

428 – Precondition Required

429 – Too Many Requests

431 – Request Header Fields Too Large

451 – Unavailable For Legal Reasons

  • 500 Codes: These codes denote the server-side error.

500 – Internal Server Error

501 – Not Implemented

502 – Bad Gateway

503 – Service Unavailable

504 – Gateway Timeout

505 – HTTP Version Not Supported

506 – Variant Also Negotiates

507 – Insufficient Storage

508 – Loop Detected

510 – Not Extended

511 –  Network Authentication Required


There are several other codes that exist but these are the common ones and as an API Tester we should be aware of this.