"How to Get Started with Postman API Test Automation: A Beginner’s Guide"
Postman API Test Automation for Beginners
API Testing and Automation with Postman
APIs have become a crucial part of software development, making API testing an essential skill for developers and testers. In this course, Valentine Despo will teach you how to use Postman to write and automate API tests. From the fundamentals of API testing to the intricacies of automating these tests, this course covers it all.
Course Overview
Variables
Scripts
Debugging
JavaScript basics
JSON format
Assertions
Regular expressions
Integration in CI/CD
Collaboration within a Postman workspace
This course assumes that you are already familiar with APIs and know how to use Postman to create requests. If you are new to APIs, Free Code Camp offers other courses to help you get started. Throughout the course, you will work with an existing Postman collection, write tests, and automate their execution using various tools.
By completing all the assignments in this course, you will earn a Postman badge certifying your proficiency in test automation. Check the video description for course notes, additional resources, and troubleshooting tips.
If you enjoy the course, please consider liking and subscribing to Free Code Camp's YouTube channel. You can also connect with the instructor, Valentine Despo, on social media. Lastly, a big thank you to Postman for sponsoring this course.
Level 1: Manual Testing
In this level, we will use a Postman collection called "Valentino Artisan Coffee House" for manual testing. To get started, open Postman in your browser and log in to your account. Change the theme to a darker one if desired.
Open the course notes from the video description and click on the link to access the workspace. This workspace contains the API collection and assignments for the course. The "Valentino Artisan Coffee House" collection allows you to browse products, view details, and place orders.
To work with this collection, make a copy (fork) of it in your own workspace. Create a new workspace if necessary. Once you have made a copy, you can start exploring the endpoints and making changes as needed.
Level 2: Introduction to Test Automation
In this level, we will learn about JavaScript concepts and begin automating tests with Postman. Stay tuned for more updates on the course.
Postman API Testing
In this tutorial, we will explore how to use Postman for API testing. API testing is a type of software testing that checks the functionality, reliability, performance, and security of an API. The main goal of API testing is to find issues and defects in the API before it is released to users.
API Status
To start our API testing, we first need to check the status of the API. This can be done by sending a GET request to the status endpoint. If the API is up and running, we should receive a status 200 OK response.
Get All Products
Next, we can retrieve a list of all available products by sending a GET request to the "Get All Products" endpoint. This will return a paginated response with information about each product. We can navigate through the pages using query parameters.
Filter Products by Category
If we are only interested in products from a specific category, we can use the "category" query parameter to filter the results. For example, if we want to see only pastry products, we can set the "category" parameter to "pastry" and send the request.
Get Single Product
To retrieve additional details about a specific product, we can use the "Get Single Product" endpoint. This endpoint requires a path variable, which is the ID of the product we want to retrieve. By sending a GET request with the product ID, we can get more information about that product.
Create an Order
To place an order, we need to send a POST request to the "Create Order" endpoint. This endpoint requires a request body, which contains the details of the order. We can include information such as the product IDs, quantities, and customer details in the request body.
Placing an Order
To place an order using the API, we use a POST request. In the request body, we specify the customer name and the products to be ordered.
Customer Name: A random name is generated for each request.
Products: We need to specify the ID and quantity of each product.
When we submit the order, we receive a 403 Forbidden error with an "Invalid API Key" message. This means we need to authenticate ourselves to the API in order to place an order or view existing orders.
Authentication is a topic that will be covered in the upcoming lecture.
Registering as a Client
To obtain an API key, we need to register ourselves as a client with the API. This is done by sending a POST request with our email address.
After registering, we receive a token, which is a temporary password used by APIs for authentication.
We should not include the token in the request itself, as it is a secret. Instead, we store it as a collection variable in Postman.
To store the token, we go to the collection settings, navigate to the variables tab, and enter the token as the current value of the "origin entry api key" variable.
It is important to understand the difference between the initial value and the current value of a variable. The initial value is not visible to others, but the current value is used when running requests.
By storing the token as a collection variable, we can use it for authentication without exposing it to others.
Creating a New Order
Now that we have the valid API key, we can try submitting the order again.
If the request is successful, we receive a 201 Created status code, indicating that the order has been created.
The response body contains the order ID, customer name, and the products ordered.
Handling Secrets in Postman
A query param is typically something in a header. In Postman, we can manage headers and check for hidden headers. One important header is the x-api-key, which is the API key used for authentication. This header is managed using an "auth helper" in Postman, which is configured under the "Authorization" tab. The auth helper is inherited from the parent folder, so all requests within the folder can use the same API key without manual configuration.
Example: Get All Orders
We can test the API by sending a request to get all orders. If the request is configured correctly, we should see the order that was just submitted. This confirms that the API key and authorization are working properly.
Example: Get Order by ID
We can also test the API by sending a request to get an order by ID. If the request is configured correctly and the ID exists, we should see the details of the order, including the products that were ordered.
Writing Scripts in Postman
Postman allows us to write scripts in two places: the pre-request script and the test script.
Pre-request Script
The pre-request script is executed before the request is sent. It can be used to perform any necessary actions or set up variables before making the request.
Test Script
The test script is executed after the request has been sent and the response has been retrieved. It can be used to validate the response and perform any necessary actions based on the response.
In Postman, scripts are written in JavaScript. JavaScript is the only programming language supported by Postman for writing scripts. It is relatively easy to learn and there are resources available to help beginners get started.
To write a test script, we can use the JavaScript console.log function to print a message. The script will be executed after the request is sent, and any output will be visible in the test results.
It's important to note that JavaScript is case-sensitive, so we need to be careful with our code to avoid errors.
In this case, we will get an error because the script is not written correctly. However, if we write the script correctly, we won't see anything because the output will be printed to the Postman console. The Postman console is a tool that is important to know and use when writing scripts in Postman. To open the console, click on "Console" in the lower left corner. You can clear the console by clicking on the "Clear" button. The console displays information about the requests and responses in a chronological order. By using the console, you can debug your scripts and understand your requests.
To display a message before the request is sent, you can use the "pre-request" script. In the pre-request script, add the code "console.log('Hello from the pre-request script');" and click on the send button. The message will be displayed in the console before the request is sent. You can use the "console.clear()" function in the pre-request script to clear the console with every request.
In this lecture, we will write our first API test in Postman. We will use the Postman console to check if the status code of the response is 200. You can find useful scripts in the snippets panel on the right-hand side of the code editor. Scroll through the snippets until you find the "status code is 200" script. Click on it to generate the code. This code uses JavaScript and contains an assertion to check if the status code is 200. When you run the code, you will see the test results in the lower part of Postman. The test results will indicate if the test passed or not.
It is important to ensure that the code is working correctly. You can do this by testing with a status code that is not 200. For example, you can change the endpoint to "status2" which does not exist. When you send the request, you will see that the test fails because the status code is not 200.
You're going to get a taste of it real soon so we have created this code here by simply clicking one button. I already don't fully understand what exactly is going on here. How does this code work and how we can write maybe more advanced tests? So, to be able to understand this code, which is written in JavaScript, and even write more advanced code, we need to learn a few JavaScript basics.
In the upcoming lessons, we will cover some of the most important concepts in JavaScript that are relevant for writing API tests in Postman. We will cover JavaScript variables and their scope, data types including objects and arrays, functions, and many other things you should know. Now, keep in mind that this is not a full JavaScript course, and always check the course notes for additional resources and learning. However, with what you will learn in the upcoming lessons, you should have just enough information to get started writing code in Postman and understanding such code.
If you see some one of the fundamental building blocks of any programming language, including JavaScript, is the concept of variables. In this lecture, we will learn about the JavaScript variables, their declaration types, and how to use them in our code.
Variables are like containers that store data for us so that we can use them to store and manipulate information in our script. You can think of a variable like a jar. In a jar, you can put something and then put a label on it so that you know what's inside. So, for example, if you want to store a name like Jamie, that's the data, all we have to do is put Jamie in a jar and put a label on that jar, which is like "first name" or simply "name".
So let's do exactly that in Postman. First of all, I'm here inside the test script, so I'm going to go ahead and remove everything that is inside here. And in the pre-request script, I'm going to keep here console clear because that's going to be useful. Apart from this, because we're going to be focusing on scripts, I'm going to change a bit to the interface of Postman.
First of all, I'm going to collapse this panel here, so there's an option here right at the bottom "hide sidebar". This will give us a bit more space. And additionally, there's a different way on how we can organize everything in Postman, and one of this is this side by side pane. So we have this two pane view which we can click on it. It will move here a few things to the side, so we'll see here the test results and everything, but this is something that we actually don't need. So, we're going to move this all together. And we can also collapse this script so this gives us a bit more space to write this code. And in addition to this, you can open up here the console so that we have here the editor and the console in clear view. Perfect.
So let's go ahead by actually defining a variable which is called "jamie". I'm going to use the keyword "let" which is used to define a new variable. So, we're going to write here "let" and then we have to provide the variable name, which is like on the jar, the label. So, we can see here "name". And then I'm going to use the equal sign and between quotes, I'm going to put here the name "jamie". When we're working with strings like names or any text or something like that, it is always important to put this between quotes. That is when we have a string. You can use single quotes or double quotes as you prefer.
Now, if we simply run this code, nothing will happen. So, it's important to remember that if you want to check what's inside the "name" variable, then we'll have to use here "console.log" and what we're going to do here, we're going to pass "name" between the parentheses. We're going to write "name" in this case. It is important that you don't put quotes. So let me show you. We're going to click here on "send" and what we're going to get back is "jamie".
So, what is happening here is by not using "name" between quotes, we are actually referencing the variable "name". And then by referencing the variable "name", we're essentially trying to log this variable. Well, what's the value of this variable? Well, it's "jamie". Okay, let's write that. So, this is why "jamie" appears here in the console.
It's important to remember that this code that we are executing here in Postman doesn't store anything anywhere. This jar with
Variables and Constants in JavaScript
In JavaScript, variables and constants are essential for storing and manipulating data. Understanding their differences and how to use them correctly is crucial.
Variables
A variable is a named container that can hold a value. It can be changed during the execution of a script. Variables are written in camel case, with the first letter of each word capitalized except for the first word. For example, firstName and fullName are valid variable names.
Constants
A constant, on the other hand, is a variable that cannot be changed once it is defined. It is written in camel case and declared using the const keyword. Constants are useful for storing values that should not be modified, such as a person's year of birth or mathematical constants like the number of seconds in an hour.
Variable Scope
Scope refers to the accessibility of a variable. Variables declared outside of any code block have a global scope and can be accessed from anywhere in the script. However, variables declared inside a code block, such as a loop or conditional statement, have a local scope and can only be accessed within that code block.
Example:
let name = "Jamie";console.log(name); // Output: Jamie{ let name = "Jake"; console.log(name); // Output: Jake}console.log(name); // Output: Jamie
In the above example, we have a variable name declared in the global scope. Inside the code block, we have another variable with the same name. The code block has its own scope, allowing us to redefine the variable without affecting the global scope. However, outside the code block, the global variable is still accessible.
A variable called jig is being accessed. If we remove the first declaration, we get a reference error because the variable is being accessed from the global scope where it hasn't been defined. To define it only in the global scope, we can use latiname instead of let name. This way, we are not defining a new variable, but using one from the global scope. In JavaScript, code blocks act like one-way mirrors in an interrogation room. Variables defined inside the code block are private, while variables in the global space are accessible to everyone.
JavaScript has several data types such as strings, numbers, booleans, objects, and arrays. By understanding the usage of these data types, you will be better equipped to use them when writing scripts.
Strings and numbers are familiar data types. If you're not sure about the data type of a value, you can use the typeof keyword in JavaScript to check it. Anything between quotes, even if it's a number, is considered a string. To represent the state of something like on or off, you can use the boolean data type, which can only be true or false. The undefined data type represents something that is not defined.
If you have a group of related properties, you can use objects to group them together. Objects are defined using braces, and properties are added inside the braces using key-value pairs.
It's important to note that JavaScript objects are not the same as JSON. JSON is a representation of a JavaScript object, but they are not interchangeable.
There are some similarities between this and JSON, but it is simply a JavaScript object. To access a property, we use dot notation, like person.name. If we need to add a new property, we can do so by using person.email = "check@example.com". When accessing a property with special characters, we need to use square bracket notation, like person["email-with-dash"]. When a property does not exist, JavaScript returns undefined. To create a collection of elements, we can use an array. For example, we can create an array of hobbies: hobbies = ["reading", "traveling", "gardening", "cooking"].