DEV Community

Possawat Sanorkam
Possawat Sanorkam

Posted on • Updated on

Effective End-to-End Testing: A Beginner's Guide to Playwright

Introduction

We can ensure the reliability and functionality of a deployed service using health check and smoke tests.

Health Check

Health check is used to verify that all endpoints of a deployed service are accessible and return the expected status code of 200 (OK).

To implement health check tests using Playwright, you can create a script that sends requests to each endpoint of your service and asserts that the response has a status code of 200.

Smoke test

Smoke tests, on the other hand, cover all endpoints as well but with more extensive assertions.

Fun fact: In plumbling, smoke testing is just another tool used to find leaks or sources of odor in pipes and sewage systems. It is not actually smoke, but the same type substance that comes out of fog machines. It is used to detect sewer gas leaks caused by broken pipes, bad connections, venting issues, open pipes or fittings

To implement smoke tests using Playwright, you can create a script that sends requests to each endpoint of your service and asserts that the response has the expected status code, body and error message

Health check example

const { playwright } = require('playwright');

(async () => {
    // Start a browser
    const browser = await playwright.chromium.launch();

    // Create a new context (page)
    const context = await browser.newContext();

    // Create a new page
    const page = await context.newPage();

    // Define the endpoint URLs that you want to test
    const endpoints = [
        'https://example.com/api/users',
        'https://example.com/api/orders',
        'https://example.com/api/products',
    ];

    for (const endpoint of endpoints) {
        // Send a GET request to each endpoint
        const response = await page.goto(endpoint);

        // Assert that the status code is 200
        for (const endpoint of endpoints) {
        // Send a GET request to each endpoint
        const response = await page.goto(endpoint);

        // Assert that the status code is 200
        expect(code).toBe(200)
        }

    }

    // Close the browser
    await browser.close();
})();
Enter fullscreen mode Exit fullscreen mode

Smoke test example

const { playwright } = require('playwright');
const assert = require('assert');

(async () => {
    // Start a browser
    const browser = await playwright.chromium.launch();

    // Create a new context (page)
    const context = await browser.newContext();

    // Create a new page
    const page = await context.newPage();

    // Define the endpoint URLs that you want to test
    const endpoints = [
        { url: 'https://example.com/api/users', expectedStatusCode: 200, expectedBody: { id: 1, name: "user1" } },
        { url: 'https://example.com/api/orders', expectedStatusCode: 200, expectedBody: { id: 1, name: "order1" } },
        { url: 'https://example.com/api/products', expectedStatusCode: 404, expectedBody: { message: "product not found" } },
    ];

    for (const endpoint of endpoints) {
        // Send a GET request to each endpoint
        const response = await page.goto(endpoint.url);

        // Assert that the status code is as expected
        assert.strictEqual(response.status(), endpoint.expectedStatusCode);

        // Assert that the response body is as expected
        const responseBody = await page.evaluate(() => JSON.parse(document.body.textContent));
        assert.deepEqual(responseBody, endpoint.expectedBody);
    }

    // Close the browser
    await browser.close();
})();

Enter fullscreen mode Exit fullscreen mode

Surprise! This article is written by ChatGPT!

Top comments (1)

Collapse
 
parmcoder profile image
Possawat Sanorkam

Here are some example test cases that you could use as smoke tests to quickly identify major issues with a service (Service A) that performs CRUD operations:

Positive test case for creating a new resource: Test that a new resource can be successfully created and that the appropriate response code (e.g. 201) is returned.

Positive test case for reading a resource: Test that an existing resource can be successfully read and that the appropriate response code (e.g. 200) is returned.

Positive test case for updating a resource: Test that an existing resource can be successfully updated and that the appropriate response code (e.g. 200) is returned.

Positive test case for deleting a resource: Test that an existing resource can be successfully deleted and that the appropriate response code (e.g. 204) is returned.

These smoke tests would cover the basic CRUD operations and should quickly identify any major issues that would prevent the service from functioning correctly.

Here are some example test cases that you could use as health check to verify that the service is running and available:

Test that the service is up and responding to requests

Test that all the dependencies of the service are running

Test that the service has enough resources to handle the incoming requests

Test that the service's database is accessible and responding

Test that the service's cache is working and has enough memory

Test that the service's logs are being generated correctly

These health check tests would cover the availability and operational status of the service, they ensure that the service is fully operational and can handle incoming requests

Note that these test cases are just examples, the actual test cases will depend on the requirements and functionality of Service A.