Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add e2e tests in Playwright #247

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
MONGO_DB_URL=
NEXT_PUBLIC_NODE_ENV=
PLAYWRIGHT_BASE_URL=
2 changes: 1 addition & 1 deletion .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
run: npm run lint

- name: Unit Tests
run: npm run test
run: npm run test:unit
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: E2e tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest

timeout-minutes: 60

steps:
- name: Wait for HTTP Status Code 200 from the Vercel Preview Deploy
uses: patrickedqvist/wait-for-vercel-preview@v1.3.2
id: waitForVercelPreviewDeploy
with:
token: ${{ secrets.GITHUB_TOKEN }}
max_timeout: 300

- 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
env:
PLAYWRIGHT_BASE_URL: ${{ steps.waitForVercelPreviewDeploy.outputs.url }}
run: npx playwright test

- uses: actions/upload-artifact@v4
if: always()
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 @@ -7,6 +7,10 @@

# testing
/coverage
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/

# next.js
/.next/
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ npm run fix

## Testing

Tests are built with [Vitest](https://vitest.dev/).
Unit Tests are built with [Vitest](https://vitest.dev/).

```bash
npm run test
npm run test:unit
```

E2e Tests are built with [Playwright](https://playwright.dev/).

```bash
npm run test:e2e
```

## Development configuration
Expand Down
2 changes: 1 addition & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
104 changes: 104 additions & 0 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"fix": "npm run fix:js && npm run fix:css",
"fix:js": "next lint --fix",
"fix:css": "stylelint \"**/*.css\" --fix",
"test": "vitest"
"test:unit": "vitest",
"test:e2e": "playwright test"
},
"dependencies": {
"accept-language": "^3.0.20",
Expand All @@ -31,6 +32,7 @@
"react-i18next": "^15.0.1"
},
"devDependencies": {
"@playwright/test": "^1.47.0",
"@testing-library/jest-dom": "^6.4.8",
"@testing-library/react": "^16.0.1",
"@types/exenv": "^1.2.2",
Expand All @@ -41,6 +43,7 @@
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.18.0",
"@vitejs/plugin-react": "4.3.1",
"dotenv-cli": "^7.4.2",
"eslint": "^8.56.0",
"eslint-config-next": "^14.2.6",
"eslint-plugin-import": "^2.29.1",
Expand Down
43 changes: 43 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import path from 'node:path'

import { defineConfig, devices } from '@playwright/test'
import dotenv from 'dotenv'


dotenv.config({ path: path.resolve(__dirname, '.env') })

const environment = process.env.NEXT_PUBLIC_NODE_ENV || 'development'

export default defineConfig({
forbidOnly: environment !== 'development',
fullyParallel: true,
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
{
name: 'Mobile Chrome',
use: { ...devices['Pixel 5'] },
},
{
name: 'Mobile Safari',
use: { ...devices['iPhone 12'] },
},
],
reporter: 'html',
retries: environment !== 'development' ? 2 : 0,
testDir: './src/',
testMatch: '**/*.e2e.ts',
use: {
baseURL: process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:3000',
screenshot: 'only-on-failure',
trace: 'retain-on-failure',
video: 'retain-on-failure',
},
workers: environment !== 'development' ? 1 : undefined,
})
2 changes: 2 additions & 0 deletions src/app/[locale]/__e2e__/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './page/AppPage'
export * from './test/testAppPage'
8 changes: 8 additions & 0 deletions src/app/[locale]/__e2e__/fixtures/page/AppPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BasePage } from 'src/tests/playwright/fixtures'


export class AppPage extends BasePage {
async goto(): Promise<void> {
await this.page.goto('/')
}
}
11 changes: 11 additions & 0 deletions src/app/[locale]/__e2e__/fixtures/test/testAppPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from '@playwright/test'

import { AppPage } from '../page/AppPage'


export const testAppPage = test.extend<{ appPage: AppPage }>({
async appPage({ page }, use) {
const appPage = new AppPage(page)
await use(appPage)
}
})
32 changes: 32 additions & 0 deletions src/app/[locale]/app.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect } from '@playwright/test'

import { testAppPage as test } from './__e2e__/fixtures'


test.beforeEach(async ({ appPage }) => {
await appPage.goto()
})

test.describe('src / app / [locale] > app', () => {
test('check header existance', async ({ appPage }) => {
expect(appPage.header.getElement()).toBeDefined()
expect(appPage.header.getElement()).toBeInViewport()

expect(appPage.header.getLogo()).toBeDefined()
expect(appPage.header.getLogo()).toBeInViewport()
})

test('check navigation menu', async ({ appPage }) => {
expect(appPage.navigation.getElement()).toBeInViewport()

expect(appPage.navigation.getMenu('')).toBeDefined()
expect(appPage.navigation.getMenu('places')).toBeDefined()
})

test('check locales menu', async ({ appPage }) => {
expect(appPage.locales.getElement()).toBeInViewport()

expect(appPage.locales.getLanguage('en')).toBeDefined()
expect(appPage.locales.getLanguage('it')).toBeDefined()
})
})
1 change: 1 addition & 0 deletions src/tests/playwright/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './page/BasePage'
Loading