Guide For API
Guide For API
Guide For API
The tool used for writing API calls, and executing API test cases is Postman, and it's built in
tool Collection Runner. alt text
Download and install from https://www.getpostman.com/
An Intro to testing is presented here, with a lot of examples of tests you could use from the
interenet https://www.getpostman.com/docs/postman/scripts/test_scripts
Using Environment and Global Variables and .csv/.json files for testing
purposes
Postman can use environment and global variables, along with .csv or .json files for testing,
you can pass values in the call itself, the header, the call body and in the test cases you
write for your call.
An important thing to know before using all three of these is to understand the following -
if Postman encounters a variable with the same name in the file, in an environment and in
an global variable, it will prioritize it in this order:
1. File
2. Environment variable
3. Global variable
Environments are changeable on the fly via the dropdown menu in the upper right corner.
alt text
They contain predefined data useful for executing individual API calls, without changing
the contents of the calls header or body. It can also be used to store data recieved from a
response, and in many other cases.
Global variables are added with the "Global" button in the picture above, the same way as
you would add environment key-value paits. Global variables are accessible at any point as
soon as they are added in the collection, and are useful for data you won't frequently
change or it's value can be applied towards a number of diferent collections/calls.
How to call the environment and global variables value in an API call
-In the API call
-In the Pre-request Script Note: The Pre-request Script is executed before the actual API
call, it's useful if you want to always set a value of a variable, clear the variable, or execute
any json code block that you may requre.
Using them in Tests is described under the "Writing Tests" segment, along with how to use
csv or json files in testing.
Writing Tests
The tests are written under the "Tests" tab under the API link
Simply use data.columnName and you'll grab the value of that key (columnName being the
key, "open", "save" etc. being examples in the picture);
However, there is a small problem in the example above - All of our tests except one failed.
Because we didn't specify a file. This is where we should mention Postmans Collection
Runner.
The Collection Runner
Open up the collection runner by clicking on any collection and then clicking the Run
button
Let's try using a file in our next run, the csv file looks like this
language,open,claim,title,dateCreated,dueDate,assignedTo,save,complete,module
en,OPEN,CLAIM,Title,Date created,Due date,Assigned to,Save,Complete,task-
inbox
sr,OTVORI,PREUZMI,Naziv,Datum pravljenja,Datum
do,Dodeljeno,Sačuvaj,Završi,task-inbox
Click the New button, and navigate to a collection that uses data in it's Tests. Select your
environment, and click on the Select File button, then select the csv or json file you wish to
use. After you selected your file, the iteration number should automatically swap to the
amount of rows it read in the file. Click the preview button to see if your values are correct,
and click the Run button.
You should be met with a screen looking like this
The test that we ran earlier that failed is now successful, because it used data from a file in
order to complete it's tests. alt text