API Testing with Postman - Notes
Postman Notes
Introduction to API Testing with Postman
- Course Overview: This course introduces API testing using Postman, a tool designed for interacting with APIs and writing tests. The instructor, Valentine, emphasizes hands-on experience and practical assignments throughout the course
Understanding APIs
- What is an API?: An API (Application Programming Interface) serves as an interface to a server, allowing for data exchange and actions. It can be compared to a power outlet, which provides a standardized way for devices to connect
- Postman as a Tool: Postman simplifies the process of sending and receiving data from APIs, functioning like a connector to the server
Getting Started with Postman
- Installation: Postman can be run in a browser or as a standalone application. The instructor recommends using the browser version for this course
- https://www.postman.com/ → can use web-based or install
- User Interface: Upon first opening Postman, users can customize settings, such as changing the theme
Making API Requests
Github example - https://github.com/vdespa/introduction-to-postman-course
- Understanding Endpoints: Each API has a specific endpoints that define how to interact with it. For example, the course uses an API for ordering books, which includes endpoints for checking the status and retrieving book lists
- Sending Requests: Users can send requests to the API using Postman. A successful request returns a status code (e.g., 200 OK), and the requested data
HTTP Basics
- Request and Response: The communication between Postman (client) and the API (server) involve HTTP messages. A request is sent from Postman, and a response is received from the server
- Headers and Body: Requests can include headers (metadata) and a body (data sent with POST requests). Understanding how configure these is crucial for successful API interactions
Using Query Parameters
- Filtering Data: Query parameters allow users to filter responses from the API. For example, specifying a type of book (fiction or non-fiction) can refine the results
- Error Handling: If an invalid query parameter is used, the API will return an error message, indicating what went wrong
Path Parameters and Ordering Books
- Path Variables: These are dynamic parts of the API endpoint that changes based on the request. For example, to get details about a specific book, the book ID is included in the URL
- Placing an Order: To order a book, users must send a POST request with the appropriate body format, including the book ID and customer information
Authentication
- API Client Registration: Some APIs require authentication, which involves registering the client (Postman) to obtain an access token. This token is then used in subsequent requests to authorize actions
- Using Access Tokens: The access token must be included in the request headers for authenticated endpoints
Conclusion
- Practice and Application: The course emphasizes the importance of practicing API testing using Postman. Students are encouraged to explore the API documentation and apply what they've learned through assignments
Study Guide: API Testing with Postman
Key Concepts
- JSON Syntax: JSON requires strict adherence to syntax rules, including the use of double quotes for keys and strings. For example, specifying keys without quotes or using single quotes is invalid
- Data Types in JSON: JSON can contain various data types, including strings, numbers, and booleans. For instance, a string can be represented as "John", while a number is simply written as 1
- Valid JSON Submission: When submitting data to an API, it's crucial to ensure that the JSON is valid. Invalid JSON will lead to errors, as APIs cannot process it
API Testing Workflow
- Creating a POST Request: When creating a POST request, ensure the JSON is valid. For example, submitting an order with valid JSON will return a success status
- Handling Out-of-Stock Items: If attempting to order a book that is out of stock, the API will return a 404 error, indicating the item is not available
- Using Postman Variables: Postman allows the use of random variables to generate unique data for requests, such as customer names. This helps avoid repetitive data submissions
- Debugging with Postman Console: The Postman console is essential for debugging. It allows you to view the request and response details, helping to identify issues when something goes wrong
- Testing API Responses
- Writing Tests: You can write tests in Postman to verify the status code of responses. For example, checking if the status code is 200 can be done using JavaScript snippets
- Parsing JSON Responses: To test specific values in the response body, parse the JSON response into a JavaScript object. This allows you to access properties and verify their values
- Dynamic Variables: Instead of hardcoding values, use Postman variables to dynamically reference data, such as order IDs. This reduces manual errors and improves efficiency
Common API Operations
- GET Requests: Use GET requests to retrieve data, such as fetching all orders or a specific order by ID
- PATCH Requests: PATCH requests are used to update existing resources. For example, updating a customer name in an order requires specifying the order ID and the new name
- DELETE Requests: To remove an order, send a DELETE request with the appropriate order ID. A successful deletion will return a 204 status code
ConclusionThis study guide provides a comprehensive overview of API testing using Postman, covering essential concepts, workflows, and operations. Understanding these principles will enhance your ability to effectively test and interact with APIs.
Study Guide: API Testing and Automation with Postman
1. Introduction to API Testing
- API testing involves verifying that an API meets expectations for functionality, reliability, performance, and security. It is essential to ensure that the API behaves as intended.
2. Working with JSON Responses
- When handling API responses, it's crucial to parse the JSON data correctly. Use
pm.response.json()
to convert the response into a JavaScript object for easier manipulation
3. Accessing Data in Arrays
- API responses often return data in arrays. To access specific elements, remember that JavaScript arrays are zero-indexed. For example, to get the first element, use
response[0]
4. Filtering Data
- Use the
filter
method to retrieve only the available books from an array of books. Define a condition to check for availability
5. Setting Global Variables
- Postman allows you to set global variables to store values like book IDs. This is helpful for reusing values across different requests
6. Writing Tests in Postman
- You can write tests to validate API responses. For example, use
pm.expect
to check if a book is available.
7. Automating API Tests
- Use the Collection Runner in Postman to execute multiple requests in sequence. This feature allows you to automate the testing process
8. Handling Request Order
- You can control the order of requests in the Collection Runner by specifying the next request to execute using
pm.setNextRequest
9. Monitoring API Performance
- Postman monitors can be set up to run collections at scheduled intervals, ensuring that your APIs are functioning correctly over time
10. Using Newman for CI/CD
- Newman is a command-line tool that allows you to run Postman collections in various CI/CD environments. It can generate reports and integrate with tools like Jenkins and GitLab CI
11. Generating Reports
- Newman can generate detailed HTML reports that provide insights into the execution of your API tests, helping you debug issues effectively
12. Conclusion and Further Learning
- Understanding the basics of JavaScript, API testing, and Postman is crucial for effective API development and testing. Consider exploring advanced topics like data-driven tests and OAuth2 authentication for deeper knowledge
This study guide provides a structured overview of key concepts and practices in API testing and automation using Postman, along with relevant citations for further reference.