Skip to content

Commit

Permalink
Add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silversonicaxel committed Sep 9, 2024
1 parent 96f2544 commit f11947d
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 1 deletion.
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=
36 changes: 36 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: E2e tests

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

jobs:
test:
runs-on: ubuntu-latest

timeout-minutes: 60

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
env:
PLAYWRIGHT_BASE_URL: ${{ github.event.deployment_status.environment_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 @@ -36,3 +36,7 @@ yarn-error.log*

# editor
.vscode
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
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.
64 changes: 64 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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 Down
66 changes: 66 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { defineConfig, devices } from '@playwright/test'


export default defineConfig({
testDir: './src/',

testMatch: '**/*.e2e.ts',

/* 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: process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:3000',

screenshot: 'only-on-failure', // Capture screenshot after each test failure.
trace: 'retain-on-failure', // Record trace only when retrying a test for the first time.
video: 'retain-on-failure', // Record video only when retrying a test for the first time.
},

/* 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' },
// },
],
})
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)
}
})
22 changes: 22 additions & 0 deletions src/app/[locale]/app.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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('should check header', async ({ appPage }) => {
expect(appPage.header.getElement()).toBeInViewport()
})

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

test('should check navigation menu', async ({ appPage }) => {
expect(appPage.header.getNavigation()).toBeInViewport()
})
})
2 changes: 2 additions & 0 deletions src/tests/playwright/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from '@playwright/test'
export * from './page/BasePage'
28 changes: 28 additions & 0 deletions src/tests/playwright/fixtures/page/BasePage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Locator, Page } from 'playwright/test'


export class BasePage {
constructor(
public readonly page: Page,

public readonly header: Header = new Header(page),
) {}
}

class Header {
constructor(
private readonly page: Page,
) {}

getElement(): Locator {
return this.page.locator('header')
}

getNavigation(): Locator {
return this.page.getByRole('menubar', { name: /menu/i })
}

getLocales(): Locator {
return this.page.getByRole('menubar', { name: /language navigation/i })
}
}

0 comments on commit f11947d

Please sign in to comment.