How to Execute PATCH Requests in Playwright

HOME

npm install playwright

import { defineConfig } from '@playwright/test';

export default defineConfig({
  reporter: 'html',
});

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

test('API Testing - PATCH with Playwright', async ({ request }) => {

const response = await request.get('https://jsonplaceholder.typicode.com/posts/1');
      
    // Check the response status code 
    expect(response.status()).toBe(200);

    // Parse the response data 
    const responseBody = await response.json();
    console.log(responseBody);

  
     // Perform a PATCH request
     const patchResponse = await request.patch('https://jsonplaceholder.typicode.com/posts/1', {
       data: {
         title: 'Manager'
       }
     });

    // Check the response status code 
    expect(patchResponse.status()).toBe(200);

    // Parse the response data
    const patchResponseBody = await patchResponse.json();
    console.log(patchResponseBody);

    // Validate the response 
    expect(patchResponseBody).toHaveProperty('title', 'Manager');

   });

This line imports the `test` and `expect` functions from the Playwright testing module. They provide a structure for creating tests and validating outcomes.

import { test, expect } from '@playwright/test';
test('API Testing - PATCH with Playwright', async ({ request }) => {
const patchResponse = await request.patch('https://jsonplaceholder.typicode.com/posts/1', {
      data: {
        title: 'Manager'
      }
    });
expect(patchResponse.status()).toBe(200);
const patchResponseBody = await patchResponse.json();
console.log(patchResponseBody);
expect(patchResponseBody).toHaveProperty('title', 'Manager');

npx playwright test api_patch_tests.spec.ts

npx playwright show-report

How to Execute PUT Requests in Playwright

HOME

npm install playwright

import { defineConfig } from '@playwright/test';

export default defineConfig({
  reporter: 'html',
});

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

 test('API Testing - PUT with Playwright', async ({ request }) => {

    // Perform a GET request
    const response = await request.get('https://jsonplaceholder.typicode.com/posts/1');
      
     // Check the response status code
     expect(response.status()).toBe(200);

     // Parse the response data
     const responseBody = await response.json();
     console.log(responseBody);
  
     // Perform a PUT request
     const putResponse = await request.put('https://jsonplaceholder.typicode.com/posts/1', {
       data: {
         title: 'Manager',
         body: 'Test',
         userId: 1
       }
     });

     // Check the response status code
     expect(putResponse.status()).toBe(200);

     // Parse the response data
     const putResponseBody = await putResponse.json();
     console.log(putResponseBody);

     // Validate the response
     expect(putResponseBody).toHaveProperty('title', 'Manager');
     expect(putResponseBody).toHaveProperty('body', 'Test');
     expect(putResponseBody).toHaveProperty('userId', 1);
   });
import { test, expect } from '@playwright/test';
test('API Testing - PUT with Playwright', async ({ request }) 
 // Perform a GET request
    const response = await request.get('https://jsonplaceholder.typicode.com/posts/1');
      
     // Check the response status code
     expect(response.status()).toBe(200);

     // Parse the response data
     const responseBody = await response.json();
     console.log(responseBody);

const putResponse = await request.put('https://jsonplaceholder.typicode.com/posts/1', {
       data: {
         title: 'Manager',
         body: 'Test',
         userId: 1
       }
 });
expect(putResponse.status()).toBe(200);
const putResponseBody = await putResponse.json();
console.log(putResponseBody);
expect(putResponseBody).toHaveProperty('title', 'Manager');
expect(putResponseBody).toHaveProperty('body', 'Test');
expect(putResponseBody).toHaveProperty('userId', 1);

npx playwright test api_put_tests.spec.ts

npx playwright show-report

How to Execute POST Requests in Playwright

HOME

What is POST Request?

npm install playwright

import { defineConfig } from '@playwright/test';

export default defineConfig({
  reporter: 'html',
});

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

 test('API Testing - POST with Playwright', async ({ request }) => {

     // Perform a POST request
     const postResponse = await request.post('https://jsonplaceholder.typicode.com/users', {
       data: {
         title: 'Manager',
         body: 'Test',
         userId: 10,
       }
     }); 

     // Check the response status code
     expect(postResponse.status()).toBe(201);

      // Parse the response data
     const postResponseBody = await postResponse.json();
     console.log(postResponseBody);

     // Validate the response
     expect(postResponseBody).toHaveProperty('title', 'Manager');
     expect(postResponseBody).toHaveProperty('body', 'Test');
     expect(postResponseBody).toHaveProperty('userId', 10);
   });
import { test, expect } from '@playwright/test';
test('API Testing - POST with Playwright', async ({ request }) 
 const postResponse = await request.post('https://jsonplaceholder.typicode.com/users', {
       data: {
         title: 'Manager',
         body: 'Test',
         userId: 10,
       }
     }); 
expect(postResponse.status()).toBe(201);
const postResponseBody = await postResponse.json();
console.log(postResponseBody);

The assertions verify that the response contains expected data.

expect(postResponseBody).toHaveProperty('title', 'Manager');
expect(postResponseBody).toHaveProperty('body', 'Test');
expect(postResponseBody).toHaveProperty('userId', 10);

npx playwright test api_post_tests.spec.ts

npx playwright show-report

How to Execute GET Requests in Playwright

HOME

npm install playwright

import { defineConfig } from '@playwright/test';

export default defineConfig({
  reporter: 'html',
});

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

test('API Testing - GET with Playwright', async ({ request }) => {

     // Perform a GET request
     const response = await request.get('https://jsonplaceholder.typicode.com/users/1');
      
     // Check the response status code
     expect(response.status()).toBe(200);

    // Parse the response data
     const responseBody = await response.json();
     console.log(responseBody);

     // Assertions based on expected response
     expect(responseBody).toHaveProperty('id', 1);
     expect(responseBody).toHaveProperty('name','Leanne Graham');
     expect(responseBody).toHaveProperty('address.zipcode','92998-3874');
     expect(responseBody).toHaveProperty('address.geo.lat','-37.3159');

   });
import { test, expect } from '@playwright/test';
test('API Testing - GET with Playwright', async ({ request }) => {
 const response = await request.get('https://jsonplaceholder.typicode.com/users/1');
 expect(response.status()).toBe(200);
 const responseBody = await response.json();
console.log(responseBody);
expect(responseBody).toHaveProperty('id', 1);
expect(responseBody).toHaveProperty('name','Leanne Graham');
expect(responseBody).toHaveProperty('address.zipcode','92998-3874');
expect(responseBody).toHaveProperty('address.geo.lat','-37.3159');

npx playwright test api_get_tests.spec.ts

npx playwright show-report

Playwright Tutorials

HOME