From 77d05e550e2aab6ef81405bc154ff9d1e6ae7ac4 Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Wed, 29 Jan 2025 18:28:34 +0300 Subject: [PATCH] fix tests --- client/package.json | 2 +- client/playwright.config.ts | 8 ++++++- client/tests/e2e/dhcp.spec.ts | 8 +++---- .../e2e/{install.spec.ts => globalSetup.ts} | 21 +++++++++++++------ client/tests/e2e/login.spec.ts | 5 ++++- 5 files changed, 31 insertions(+), 13 deletions(-) rename client/tests/e2e/{install.spec.ts => globalSetup.ts} (53%) diff --git a/client/package.json b/client/package.json index 5dd3365d..37b9c722 100644 --- a/client/package.json +++ b/client/package.json @@ -12,7 +12,7 @@ "lint:fix": "eslint './src/**/*.(ts|tsx)' --fix", "test": "jest", "test:watch": "jest --watch", - "test:e2e": "npx playwright test tests/e2e --headed", + "test:e2e": "npx playwright test tests/e2e", "test:e2e:interactive": "npx playwright test --ui", "test:e2e:debug": "npx playwright test --debug", "test:e2e:codegen": "npx playwright codegen", diff --git a/client/playwright.config.ts b/client/playwright.config.ts index 31bc7707..c0f5e47f 100644 --- a/client/playwright.config.ts +++ b/client/playwright.config.ts @@ -1,10 +1,13 @@ import { defineConfig, devices } from '@playwright/test'; +import path from 'path'; + /** * See https://playwright.dev/docs/test-configuration. */ export default defineConfig({ testDir: './tests/e2e', + globalSetup: path.resolve('./tests/e2e/globalSetup.ts'), /* Run tests in files in parallel */ fullyParallel: true, /* Fail the build on CI if you accidentally left test.only in the source code. */ @@ -22,6 +25,9 @@ export default defineConfig({ /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ trace: 'on-first-retry', + launchOptions: { + headless: true, + }, }, /* Configure projects for major browsers */ @@ -63,7 +69,7 @@ export default defineConfig({ ], webServer: { - command: 'sudo ./AdGuardHome --local-frontend -v', + command: 'rm -f AdGuardHome.yaml && sudo ./AdGuardHome --local-frontend -v', url: 'http://127.0.0.1:3000', cwd: '..', reuseExistingServer: !process.env.CI, diff --git a/client/tests/e2e/dhcp.spec.ts b/client/tests/e2e/dhcp.spec.ts index 6f44dd3c..9ef72fac 100644 --- a/client/tests/e2e/dhcp.spec.ts +++ b/client/tests/e2e/dhcp.spec.ts @@ -14,8 +14,9 @@ test.describe('DHCP Configuration', () => { await page.getByTestId('username').fill(ADMIN_USERNAME); await page.getByTestId('password').click(); await page.getByTestId('password').fill(ADMIN_PASSWORD); + await page.keyboard.press('Tab'); await page.getByTestId('sign_in').click(); - await page.getByText('Settings', { exact: true }).click(); + await page.waitForURL((url) => !url.href.endsWith('/login.html')); await page.goto(`/#dhcp`); }); @@ -45,8 +46,7 @@ test.describe('DHCP Configuration', () => { await page.getByTestId('v4_range_start').fill(RANGE_END); await page.getByTestId('v4_range_end').click(); await page.getByTestId('v4_range_end').fill(RANGE_START); - await page.locator('.col-12').first().click(); - await page.getByText('Must be greater than range').click(); + await page.keyboard.press('Tab'); expect(await page.getByText('Must be greater than range').isVisible()).toBe(true); }); @@ -55,7 +55,7 @@ test.describe('DHCP Configuration', () => { await page.getByTestId('interface_name').selectOption(INTERFACE_NAME); await page.getByTestId('v4_gateway_ip').click(); await page.getByTestId('v4_gateway_ip').fill('192.168.1.200s'); - await page.getByText('Invalid IPv4 address').click(); + await page.keyboard.press('Tab'); expect(await page.getByText('Invalid IPv4 address').isVisible()).toBe(true); }); diff --git a/client/tests/e2e/install.spec.ts b/client/tests/e2e/globalSetup.ts similarity index 53% rename from client/tests/e2e/install.spec.ts rename to client/tests/e2e/globalSetup.ts index 1be0e32e..05f18f3d 100644 --- a/client/tests/e2e/install.spec.ts +++ b/client/tests/e2e/globalSetup.ts @@ -1,18 +1,27 @@ -import { test } from '@playwright/test'; +import { chromium, FullConfig } from '@playwright/test'; -const ADMIN_USERNAME = 'admin'; -const ADMIN_PASSWORD = 'superpassword'; -const PORT = 3000; +import { ADMIN_USERNAME, ADMIN_PASSWORD, PORT } from '../constants'; + +async function globalSetup(config: FullConfig) { + const browser = await chromium.launch({ + slowMo: 100, + }); + const page = await browser.newPage({ baseURL: config.webServer?.url }); -test('install', async ({ page }) => { await page.goto('/'); await page.getByTestId('install_get_started').click(); await page.getByTestId('install_web_port').fill(PORT.toString()); await page.getByTestId('install_next').click(); await page.getByTestId('install_username').fill(ADMIN_USERNAME); await page.getByTestId('install_password').fill(ADMIN_PASSWORD); + await page.getByTestId('install_confirm_password').click(); await page.getByTestId('install_confirm_password').fill(ADMIN_PASSWORD); await page.getByTestId('install_next').click(); await page.getByTestId('install_next').click(); await page.getByTestId('install_open_dashboard').click(); -}); + await page.waitForURL((url) => !url.href.endsWith('/install.html')); + + await browser.close(); +} + +export default globalSetup; diff --git a/client/tests/e2e/login.spec.ts b/client/tests/e2e/login.spec.ts index 8c0553b3..776e9145 100644 --- a/client/tests/e2e/login.spec.ts +++ b/client/tests/e2e/login.spec.ts @@ -1,13 +1,16 @@ import { test } from '@playwright/test'; import { ADMIN_PASSWORD, ADMIN_USERNAME } from '../constants'; + test.describe('Login', () => { test('should successfully log in with valid credentials', async ({ page }) => { - await page.goto(`/login.html`); + await page.goto('/login.html'); await page.getByTestId('username').click(); await page.getByTestId('username').fill(ADMIN_USERNAME); await page.getByTestId('password').click(); await page.getByTestId('password').fill(ADMIN_PASSWORD); + await page.keyboard.press('Tab'); await page.getByTestId('sign_in').click(); + await page.waitForURL((url) => !url.href.endsWith('/login.html')); }); });