# Playwright

## Installation

In order to use Playwright, you need to install the browser binaries. You can do this by running the following command:

```bash
yarn test:playwright:install
```

## Running

Run the tests using the following command:

```bash
yarn test:playwright
```

Running this script will:

- Start a local instance of songwhip-api and an empty postgres database.
- Start the Next.js server in dev mode
- Run the Playwright tests
- Then stop the Next.js server and services

### Running in UI mode

If you want to see the browser while the tests are running, you can run the following command:

```bash
yarn test:playwright:open
```

Note that it will take a few seconds for the tests to load, since the Next.js server and the services need to start first.

### Running in production mode

The tests are automatically run in production mode when a CI environment is detected. You can force the tests to run in production mode by running the following command:

```bash
CI=true yarn test:playwright
```

### Running against a local songwhip-api instance

If you have made changes to songwhip-api and need to test those changes against songwhip-web, you can point your tests at your songwhip-api instance.
To do so, you first need to start your songwhip-api instance using the following command:

```bash
# in the songwhip-api repository
yarn dev:test
```

This starts the api with an empty test database and mocked environment variables.
Then you can run the tests using the following command:

```bash
# in this repository
yarn test:playwright:dev-api
```

## Authentication

Tests are automatically started with an authenticated context. This is done by the "auth.setup.ts" file which is run before each test project.
The "auth.setup.ts" stores the authentication state in the "test/playwright/.auth" folder and is reused for each test run. Saving time spent on logging in for each test.
