Effective End-to-End Testing: A Beginner's Guide to Playwright
Tags: abotwrotethis, testing, javascript, webdev
Published: 2023-01-11 08:46:47

## 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](https://www.wyman-services.com/plumbing-problems/what-is-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

``` typescript
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();
})();
```

## Smoke test example

``` typescript
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();
})();

```

Surprise! This article is written by ChatGPT!