Skip to content

Commit

Permalink
[fea] setup playwright (#159)
Browse files Browse the repository at this point in the history
* fea: run yarn create playwright

* fix: update lock file

* enh: set yarn e2e script

* enh: add initial tests for home page

* test(e2e): test toggle language

* enh: use TEST_URL to define the base URL

* oth: clean up

* ci(playwright): await for vercel preview url

* fix: update timeouts

* fix: job name

* test(playwright): validate if links are present

* fix: pr suggestions
  • Loading branch information
marco-souza authored Jun 26, 2024
1 parent b486ab1 commit ed3b543
Show file tree
Hide file tree
Showing 9 changed files with 2,894 additions and 1,401 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Playwright Tests

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

jobs:
integration-tests:
timeout-minutes: 300
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*

- name: Waiting for 200 from the Vercel Preview
uses: patrickedqvist/wait-for-vercel-preview@v1.3.1
id: waitFor200
with:
token: ${{ secrets.GITHUB_TOKEN }}
max_timeout: 300

# access preview url
- name: Inject testing URL
run: echo "TEST_URL=${{steps.waitFor200.outputs.url}}" >> $GITHUB_ENV

- name: Install dependencies
run: npm install -g yarn && yarn

- name: Install Playwright Browsers
run: yarn playwright install --with-deps

- name: Run Playwright tests
run: yarn playwright test

- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 2
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ yarn-error.log*

# dotenv
.env*
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"encrypt": "gpg -c .env",
"decrypt": "gpg -d .env.gpg > .env",
"lint": "next lint",
"e2e": "playwright test",
"fmt": "prettier --write . && next lint --fix --dir packages",
"prepare": "husky install"
},
Expand All @@ -32,7 +33,9 @@
"react-i18next": "^11.14.0"
},
"devDependencies": {
"@playwright/test": "^1.44.1",
"@types/lodash": "^4.14.178",
"@types/node": "^20.14.6",
"@types/react": "17.0.15",
"@typescript-eslint/eslint-plugin": "^5.5.0",
"eslint": "7.31.0",
Expand Down
1 change: 1 addition & 0 deletions packages/components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function NavBar() {
minW="5rem"
bg="purple.400"
colorScheme="purple"
data-testid="join-button"
_hover={{ bg: 'purple.500' }}
>
{t(`join`)}
Expand Down
1 change: 1 addition & 0 deletions packages/components/ToggleLanguage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function ToggleLanguage() {
return (
<Button
rounded="md"
data-testid="toggle-language"
aria-label="Toggle language button"
onClick={handleToggle}
>
Expand Down
1 change: 1 addition & 0 deletions packages/components/ToggleThemeButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function ToggleThemeButton() {
return (
<IconButton
rounded="md"
data-testid="toggle-theme"
aria-label="Toggle theme button"
icon={<Icon />}
onClick={toggleColorMode}
Expand Down
41 changes: 41 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { defineConfig, devices } from '@playwright/test';

/**
* 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: {
/* 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'] },
},
],
});
78 changes: 78 additions & 0 deletions tests/home.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { test, expect } from '@playwright/test';

import { links } from '@packages/config/site';

const PAGE_MAP: Record<string, string> = {
development: 'http://localhost:3000',
production: 'https://podcodar.org',
};

const HOMEPAGE =
process.env.TEST_URL ||
PAGE_MAP[process.env.NODE_ENV as string] ||
PAGE_MAP.development;

test('has title', async ({ page }) => {
await page.goto(HOMEPAGE);

// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/PodCodar/);
});

test('has navigation links', async ({ page }) => {
await page.goto(HOMEPAGE);

for (const link of Object.values(links)) {
expect(await page.$(`a[href="${link}"]`)).not.toBeNull();
}
});

test('Join button is disabled', async ({ page }) => {
await page.goto(HOMEPAGE);

// Get join button
const joinBtn = page.getByTestId('join-button');
expect(joinBtn).not.toBeNull();

// Expect join button to be disabled
expect(await joinBtn.isEnabled()).toBeFalsy();
});

test('Toggle theme is working', async ({ page }) => {
await page.goto(HOMEPAGE);

// Get join button
const toggleBtn = page.getByTestId('toggle-theme');
expect(toggleBtn).not.toBeNull();

// check initial theme value
const initialTheme = await page.getAttribute('html', 'data-theme');
expect(initialTheme).toBe('light');

// Click the toggle theme
await toggleBtn.click();
expect(await page.getAttribute('html', 'data-theme')).toBe('dark');

// Click the toggle theme
await toggleBtn.click();
expect(await page.getAttribute('html', 'data-theme')).toBe('light');
});

test('Toggle language is working', async ({ page }) => {
await page.goto(HOMEPAGE);

// Get join button
const toggleBtn = page.getByTestId('toggle-language');
expect(toggleBtn).not.toBeNull();

// check initial theme value
expect(await page.getByTestId('join-button').textContent()).toBe('Entrar');

// Click the toggle language
await toggleBtn.click();
expect(await page.getByTestId('join-button').textContent()).toBe('Join');

// Click the toggle language
await toggleBtn.click();
expect(await page.getByTestId('join-button').textContent()).toBe('Entrar');
});
Loading

0 comments on commit ed3b543

Please sign in to comment.