Skip to content

Commit

Permalink
Add playwright create/login test for all db
Browse files Browse the repository at this point in the history
  • Loading branch information
Timshel committed Jul 8, 2024
1 parent fda77af commit 7ec697b
Show file tree
Hide file tree
Showing 17 changed files with 1,013 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ web-vault

# Vaultwarden Resources
resources

# Playwright tests
playwright
6 changes: 6 additions & 0 deletions playwright/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
logs
node_modules/
/test-results/
/playwright-report/
/playwright/.cache/
temp
27 changes: 27 additions & 0 deletions playwright/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM docker.io/library/debian:bookworm-slim

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN apt-get update \
&& apt-get install -y --no-install-recommends docker.io docker-compose git nodejs npm \
&& apt-get install -y --no-install-recommends \
ca-certificates \
curl \
libmariadb-dev-compat \
libpq5 \
openssl

RUN mkdir /playwright
WORKDIR /playwright

COPY package.json .
RUN npm install && npx playwright install-deps && npx playwright install firefox

COPY docker-compose.yml Dockerfile test.env ./
COPY webvault-resolver ./webvault-resolver

COPY *.ts test.env ./
COPY tests ./tests

ENTRYPOINT ["/usr/bin/npx", "playwright"]
CMD ["test"]
83 changes: 83 additions & 0 deletions playwright/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Integration tests

This allows running integration tests using [Playwright](https://playwright.dev/).
\
It usse its own [test.env](/test/scenarios/test.env) with different ports to not collide with a running dev instance.

## Install

This rely on `docker` and `docker-compose`.
Databases (`Mariadb`, `Mysql` and `Postgres`) and `Playwright` will run in containers.

### Running Playwright outside docker

It's possible to run `Playwright` outside of the container, this remove the need to rebuild the image for each change.
You'll additionally need `nodejs` then run:

```bash
npm install
npx playwright install-deps
npx playwright install firefox
```

## Usage

To run all the tests:

```bash
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright
```

To access the ui to easily run test individually and debug if needed (will not work in docker):

```bash
npx playwright test --ui
```

### DB

Projects are configured to allow to run tests only on specific database.
\
You can use:

```bash
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project mariadb
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project mysql
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project postgres
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project sqlite
```

### Running specific tests

To run a whole file you can :

```bash
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project sqlite tests/login.spec.ts
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project sqlite login
```

To run only a specifc test (It might fail if it has dependency):

```bash
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project sqlite -g "Account creation"
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project sqlite tests/login.spec.ts:16
```

## Writing scenario

When creating new scenario use the recorder to more easily identify elements (in general try to rely on visible hint to identify elements and not hidden ids).
This does not start the server, you will need to start it manually.

```bash
npx playwright codegen "http://127.0.0.1:8000"
```

## Override web-vault

It's possible to change the `web-vault` used by referencing a different `bw_web_builds` commit.

```bash
export PW_WV_REPO_URL=https://github.com/stefan0xC/bw_web_builds
export PW_WV_COMMIT_HASH=c5b5279a8478385cee66a8cdfc804690cffe0315
DOCKER_BUILDKIT=1 docker-compose --env-file test.env up --build WebVault
```
71 changes: 71 additions & 0 deletions playwright/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
version: '3.8'

services:
VaultWarden:
container_name: playwright_vaultwarden
image: playwright_vaultwarden
build:
context: ..
dockerfile: Dockerfile
entrypoint: /bin/bash
restart: "no"

WebVault:
container_name: playwright_web-vault_resolver
image: playwright_web-vault_resolver
build:
context: webvault-resolver
dockerfile: Dockerfile
args:
REPO_URL: ${PW_WV_REPO_URL} # Ex: https://github.com/stefan0xC/bw_web_builds
COMMIT_HASH: ${PW_WV_COMMIT_HASH} # Ex: c5b5279a8478385cee66a8cdfc804690cffe0315
restart: "no"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- VaultWarden

Playwright:
container_name: playwright_playwright
image: playwright_playwright
network_mode: "host"
build:
context: .
dockerfile: Dockerfile
restart: "no"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
depends_on: ["WebVault"]

Mariadb:
container_name: playwright_mariadb
image: mariadb:11.2.4
env_file: test.env
healthcheck:
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
start_period: 10s
interval: 10s
ports:
- ${MARIADB_PORT}:3306

Mysql:
container_name: playwright_mysql
image: mysql:8.4.1
env_file: test.env
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
start_period: 10s
interval: 10s
ports:
- ${MYSQL_PORT}:3306

Postgres:
container_name: playwright_postgres
image: postgres:16.3
env_file: test.env
healthcheck:
test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
start_period: 20s
interval: 30s
ports:
- ${POSTGRES_PORT}:5432
13 changes: 13 additions & 0 deletions playwright/global-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { firefox, type FullConfig } from '@playwright/test';
import { exec, execSync } from 'node:child_process';
import fs from 'fs';

async function globalSetup(config: FullConfig) {
fs.rmSync("temp/web-vault", { recursive: true, force: true });

execSync("mkdir -p temp/logs");
execSync("docker cp playwright_vaultwarden:/vaultwarden temp/vaultwarden", { stdio: "inherit" });
execSync("docker cp playwright_web-vault:/web-vault temp/web-vault", { stdio: "inherit" });
}

export default globalSetup;
Loading

0 comments on commit 7ec697b

Please sign in to comment.