Playwright API Testing Demo for Beginners 🚀

Learn how to set up and run API tests with Playwright in this beginner-friendly demo, covering project setup and execution steps.

Playwright API Testing Demo for Beginners 🚀
Automation Step by Step
98.9K views • Oct 5, 2023
Playwright API Testing Demo for Beginners 🚀

About this video

00:00 - Setting up API testing project with Playwright
02:41 - Setting up playwright in VS code using npm init playwright
08:04 - Setting up project and running tests in Playwright API
11:05 - Setting up project for API testing using Playwright
16:55 - Demonstrating API testing using Playwright methods
19:31 - Checking response content and handling errors
24:36 - Changing method from get to post
27:07 - Demonstration of API Post and Put Requests
32:19 - Using the delete API request in Playwright

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-testing#writing-api-test
https://playwright.dev/docs/api/class-apirequestcontext
https://playwright.dev/docs/api/class-response#methods
https://playwright.dev/docs/api/class-apiresponseassertions
https://reqres.in/

What is Package.json - https://youtu.be/04pqkGnAAkc?si=s9zi01ZYfJazP9zV






▬▬▬▬▬▬▬

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/udemy-discounts/
Stories - https://automationstepbystep.com/stories/



Tags and Topics

Browse our collection to discover more content in these categories.

Video Information

Views

98.9K

Likes

1.1K

Duration

34:31

Published

Oct 5, 2023

User Reviews

4.5
(19)
Rate:

Related Trending Topics

LIVE TRENDS

Related trending topics. Click any trend to explore more videos.