Skip to content

Commit

Permalink
feat(43): add e2e tests with playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoin95 committed Oct 29, 2024
1 parent ef58953 commit 1e0b04e
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 1 deletion.
43 changes: 43 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 10
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: db
POSTGRESQL_FSYNC: "off"
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npm run db:push && npx playwright test
env:
DATABASE_URL: postgres://test:test@localhost:5432/db
NEXTAUTH_SECRET: secret
NEXTAUTH_URL: http://localhost:3000
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 30
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ yarn-error.log*

.idea
.vscode

# playwright
playwright-report
test-results
60 changes: 60 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"lint:fix": "next lint --fix && prisma format",
"test": "SKIP_ENV_VALIDATION=true vitest",
"test:coverage": "SKIP_ENV_VALIDATION=true vitest run --coverage",
"test:e2e": "npx playwright test",
"test:e2e:ui": "npx playwright test --ui",
"start": "next start",
"ragequit": "rm -rf .next && npm run db:push && npm run dev",
"seed": "prisma db seed"
Expand Down Expand Up @@ -65,6 +67,7 @@
"zustand": "^4.5.2"
},
"devDependencies": {
"@playwright/test": "^1.48.2",
"@testing-library/dom": "^10.4.0",
"@testing-library/react": "^16.0.1",
"@types/eslint": "^8.56.2",
Expand Down
74 changes: 74 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { defineConfig, devices } from '@playwright/test';

// import dotenv from 'dotenv';
// dotenv.config({ path: './.env.e2e' });

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},

{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: 'npm run build',
url: 'http://127.0.0.1:3000',
reuseExistingServer: !process.env.CI,
},
});
16 changes: 16 additions & 0 deletions tests/pageObjects/basePage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, Page } from '@playwright/test'
export class BasePage {
page: Page

constructor(page: Page) {
this.page = page
}

async goto(path = '') {
await this.page.goto(path)
}

async hasH1() {
await expect(this.page.locator('h1')).toBeVisible()
}
}
8 changes: 8 additions & 0 deletions tests/pageObjects/signUpPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect } from "playwright/test";
import { BasePage } from "./basePage";

export class SignUpPage extends BasePage {
async hasPresidentQuote() {
await expect(this.page.locator('blockquote')).toBeVisible();
}
}
17 changes: 17 additions & 0 deletions tests/signup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { test } from '@playwright/test'
import { SignUpPage } from './pageObjects/signUpPage'

test.describe('Signup page', () => {
let signUpPage: SignUpPage

test.beforeEach(async ({ page }) => {
signUpPage = new SignUpPage(page)
})

test('Signup page is loading and has the basic elements', async ({ page }) => {
await signUpPage.goto('http://localhost:3000/signup')

await signUpPage.hasH1()
await signUpPage.hasPresidentQuote()
})
})
6 changes: 5 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from 'vitest/config'
import { configDefaults, defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
import path from 'path'

Expand All @@ -8,6 +8,10 @@ export default defineConfig({
environment: 'jsdom',
globals: true,
reporters: process.env.GITHUB_ACTIONS ? ['basic', 'github-actions'] : ['basic'],
exclude:[
...configDefaults.exclude,
'tests/*'
]
},
resolve: {
alias: {
Expand Down

0 comments on commit 1e0b04e

Please sign in to comment.