All Free Tutorials 🟢 https://AutomationStepByStep.com/
Project Setup
Create a API Test Step-by-Step
How to run a GET API Request
How to run a POST API Request
How to run a PUT API Request
How to run a DELETE API Request
How to validate API Response
Check response, logs, errors in UI Mode
Check HTML Report
API Testing with Playwright - Project Setup
Step 1 - Create a new folder for API Testing Project
Step 2 - Open the folder in VS Code
Step 3 - Open terminal on the location of the folder and run command npm init playwright@latest
Step 4 - Check if demo tests are running fine using commands npx playwright test
npx playwright test --ui
npx playwright show-report
Step 5 - Create a new file for API Tests (api-tests.spec.js)
*How to run a GET API Request *
Step 1 - Add imports
import { test, expect } from '@playwright/test';
Step 2 - Create a test using request context
test.only('Demo API Test', async({request}) => { })
Step 3 - Send a GET request & store response in a variable
const response = await request.get('https://reqres.in/api/users/2')
Step 4 - Verify the status code of the response is 200
expect(response.status()).toBe(200);
Step 5 - Check response contains some text
const text = await response.text();
expect(text).toContain('Janet');
Step 6 - Write response on the console
console.log(await response.json());
Step 7 - Run and check results
How to run a POST API Request
Step 1 - Add imports
import { test, expect } from '@playwright/test';
Step 2 - Create a test using request context
test.only('Demo API POST request’, async({request}) => {...})
Step 3 - Send a POST request along with request body & store response in a variable
const response = await request.post("https://reqres.in/api/users", {
data: {
"name": "Raghav",
"job": "teacher"
}
})
Step 4 - Verify response status code is 201
expect(response.status()).toBe(201);
Step 5 - Check response contains some text
const text = await response.text();
expect(text).toContain(Raghav);
Step 6 - Write response on the console
console.log(await response.json());
Step 7 - Run and check results
How to run a PUT API Request
test('Demo API PUT Request', async ({ request }) => {
const response = await request.put("https://reqres.in/api/users/2", {
data: {
"name": "Raghav",
"job": "teacher"
}
})
expect(response.status()).toBe(200);
const text = await response.text();
expect(text).toContain('Raghav');
console.log(await response.json());
})
How to run a DELETE API Request
test('Demo API DELETE Request', async ({ request }) => {
const response = await request.delete("https://reqres.in/api/users/2")
expect(response.status()).toBe(204);
})
References
https://playwright.dev/docs/api-testi...
https://playwright.dev/docs/api/class...
https://playwright.dev/docs/api/class...
https://playwright.dev/docs/api/class...
https://reqres.in/
What is Package.json - • All you need to know about Package.js...
▬▬▬▬▬▬▬
Every Like & Subscription gives me great motivation to keep working for you
You can support my mission for education by sharing this knowledge and helping as many people as you can
If my work has helped you, consider helping any animal near you, in any way you can
Never Stop Learning
Raghav Pal
▬▬▬▬ USEFUL LINKS ▬▬▬▬
Ask Raghav - https://bit.ly/2CoJGWf
Shorts Eng - https://bit.ly/3H9bifV
Shorts Hindi - https://bit.ly/3XY7XqN
GitHub Repositories - https://github.com/Raghav-Pal
Udemy - https://automationstepbystep.com/udem...
Stories - https://automationstepbystep.com/stor...
—