Рет қаралды 32,252
All Free Tutorials 🟢 AutomationStep...
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('reqres.in/api/...)
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("reqres.in/api/...", {
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("reqres.in/api/...", {
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("reqres.in/api/...")
expect(response.status()).toBe(204);
})
References
playwright.dev...
playwright.dev...
playwright.dev...
playwright.dev...
reqres.in/
What is Package.json - • All you need to know a...
▬▬▬▬▬▬▬
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 - bit.ly/2CoJGWf
Shorts Eng - bit.ly/3H9bifV
Shorts Hindi - bit.ly/3XY7XqN
GitHub Repositories - github.com/Rag...
Udemy - automationstep...
Stories - automationstep...
-