WHAT is API Testing
API testing means sending HTTP requests (like GET, POST, PUT, DELETE) to application's backend and verifying the response (status code, body, headers)
We test:
Is the response correct?
Is the status code right? (e.g., 200 OK, 404 Not Found)
Is the returned response body valid?
-
DEMO - How to send API Requests
Step 1 - Create a file under tests folder test_api_get.py
Step 2 - Write first API test
Step 3 - Run the test pytest tests/test_api_get.py
can add -s to show the output
-
def test_api_get_request(playwright):
request = playwright.request.new_context()
response = request.get("https://jsonplaceholder.typicode.com/...")
assert response.status == 200
json_data = response.json()
print(json_data)
assert json_data["id"] == 1
request.dispose()
-
Send request with headers
response = request.get(
"https://jsonplaceholder.typicode.com/...",
headers={"Authorization": "Bearer your_token_here", "X-Api-Key": "abc123"},
)
-
Send create headers block separately
request = playwright.request.new_context(
extra_http_headers={
"Authorization": "Bearer your_token_here",
"Accept": "application/json",
"X-Custom-Header": "value123"
}
response = request.get("https://api.example.com/protected-end...")
-
How to assert Responses
Print response status
print("Status Code:", response.status)
Print the JSON response body
print("Response JSON:", response.json())
Validate values inside JSON
assert json_data["id"] == 1
assert json_data["userId"] == 1
print("ID and userId match expected values")
-
▬▬▬▬▬▬▬
Share with all who may need this
If my work has helped you, consider helping any animal near you, in any way you can
Never Stop Learning
Raghav Pal
▬▬▬▬ USEFUL LINKS ▬▬▬▬
✅ ALL TUTORIALS - https://AutomationStepByStep.com/
Tools & Tips - https://bit.ly/2oBfwoR
QnA Friday - https://bit.ly/2NgwGpw
Sunday Special - https://bit.ly/2wB23BO
🙌 Connect with Raghav:
Ask Raghav: https://bit.ly/2CoJGWf
GitHub: https://github.com/Raghav-Pal
Udemy: https://www.udemy.com/user/raghav-pal-3/
Shorts Eng - https://bit.ly/3H9bifV
➡️ Subscribe for more videos: / @raghavpal
—
On this page of the site you can watch the video online Playwright Python 8 | 1st API Test with a duration of hours minute second in good quality, which was uploaded by the user Automation Step by Step 01 July 2025, share the link with friends and acquaintances, this video has already been watched 14,249 times on youtube and it was liked by 149 viewers. Enjoy your viewing!