Playwright
  • Introduction
    • Playwright With TS
  • Automation the right things with Playwright
  • Setup
    • Setting Up Your Environment
    • Typescript fundamental
    • Setup Playwright TS Project in VS code
    • Record and Playback with Code Generation using VS Code Playwright Extension
    • Record and Playback using Playwright Codegen with Inspector
  • Playwright config file
  • Implement Test Cases
    • Open Browser
    • Fake Geolocation
    • Form Authentication
    • Checkboxes
    • Dropdown
    • Hyperlink
    • Web Table
    • Drag Drop
    • Frame
    • Horizontal Slide
    • Context Click - Right Click
  • Hover
  • Take Screenshot
  • Automatic screenshots on failure:
  • Page Object Model
    • Understanding the Page Object Model pattern
  • Implementing POM with Login Page
  • Combine POM with Playwright Fixture
  • API Testing
    • API Docs
  • GET
  • API POST
  • PUT
  • DELETE
  • CI/CD And Reporting
    • Setup Allure Report
    • Run in Github Actions
  • Playwright & AI
    • Debug & Generate Playwright Tests with AI
Powered by GitBook
On this page

API POST

import { expect, test } from '@playwright/test';
import { z } from 'zod';

test("[Basic] POST API call create token", async ({ request }) => {
    const response = await request.post('https://restful-booker.herokuapp.com/auth', {
        data: {
            username: 'admin',
            password: 'password123'
        }
    });
    const body = await response.json();
    expect(response.ok()).toBeTruthy();

    const schema = z.object({
        token: z.string()
    });

    expect(() => {
        schema.parse(body);
    }).not.toThrow();
})
test("[Basic] POST API call create booking", async ({ request }) => {
    const response = await request.post('https://restful-booker.herokuapp.com/booking', {
        data: {
            firstname: 'Jim',
            lastname: 'Brown',
            totalprice: 111,
            depositpaid: true,
            bookingdates: {
                checkin: '2025-06-01',
                checkout: '2025-06-02'
            },
            additionalneeds: 'Breakfast'
        }
    });
    const body = await response.json();
    expect(response.ok()).toBeTruthy();

    const schema = z.object({
        bookingid: z.number(),
        booking: z.object({
            firstname: z.string(),
            lastname: z.string(),
            totalprice: z.number(),
            depositpaid: z.boolean(),
            bookingdates: z.object({
                checkin: z.string(),
                checkout: z.string()
            }),
            additionalneeds: z.string()
        })
    });

    expect(() => {
        schema.parse(body);
    }).not.toThrow();
})
test("[Basic] POST API call create booking with token", async ({ request }) => {
    const response = await request.post('https://restful-booker.herokuapp.com/booking', {
        data: {
            firstname: 'Jim',
            lastname: 'Brown',
            totalprice: 111,
            depositpaid: true,
            bookingdates: {
                checkin: '2025-06--05',
                checkout: '2025-06-07'
            },
            additionalneeds: 'Breakfast'
        },
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${process.env.TOKEN}`
        }
    });
    const body = await response.json();
    expect(response.ok()).toBeTruthy();

    const schema = z.object({
        bookingid: z.number(),
        booking: z.object({
            firstname: z.string(),
            lastname: z.string(),
            totalprice: z.number(),
            depositpaid: z.boolean(),
            bookingdates: z.object({
                checkin: z.string(),
                checkout: z.string()
            }),
            additionalneeds: z.string()
        })
    });

    expect(() => {
        schema.parse(body);
    }).not.toThrow();
})
PreviousGETNextPUT

Last updated 14 days ago