dify/web/playwright.config.ts
CodingOnStar 7b968c6c2e feat: add Playwright E2E testing framework and initial test setup
- Introduced Playwright for end-to-end testing, including configuration in .
- Created global setup and teardown scripts for authentication and cleanup.
- Added page object models for key application pages (Apps, SignIn, Workflow).
- Implemented utility functions for API interactions and common test helpers.
- Updated  to exclude Playwright test results and auth files.
- Added initial E2E tests for the Apps page.
- Updated  with new test scripts for E2E testing.
2025-12-09 18:14:57 +08:00

153 lines
4.0 KiB
TypeScript

import path from 'node:path'
import { defineConfig, devices } from '@playwright/test'
/**
* Playwright Configuration for Dify E2E Tests
*
* Environment variables are loaded from web/.env.local
*
* E2E specific variables:
* - E2E_BASE_URL: Base URL for tests (default: http://localhost:3000)
* - E2E_SKIP_WEB_SERVER: Set to 'true' to skip starting dev server (for CI with deployed env)
* @see https://playwright.dev/docs/test-configuration
*/
// Load environment variables from web/.env.local
require('dotenv').config({ path: path.resolve(__dirname, '.env.local') })
// Base URL for the frontend application
// - Local development: http://localhost:3000
// - CI/CD with deployed env: set E2E_BASE_URL to the deployed URL
const BASE_URL = process.env.E2E_BASE_URL || 'http://localhost:3000'
// Whether to skip starting the web server
// - Local development: false (start dev server)
// - CI/CD with deployed env: true (use existing server)
const SKIP_WEB_SERVER = process.env.E2E_SKIP_WEB_SERVER === 'true'
export default defineConfig({
// Directory containing test files
testDir: './e2e/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 for stability
workers: process.env.CI ? 1 : undefined,
// Reporter to use
reporter: process.env.CI
? [['html', { open: 'never' }], ['github']]
: [['html', { open: 'on-failure' }], ['list']],
// Shared settings for all the projects below
use: {
// Base URL for all page.goto() calls
baseURL: BASE_URL,
// Collect trace when retrying the failed test
trace: 'on-first-retry',
// Take screenshot on failure
screenshot: 'only-on-failure',
// Record video on failure
video: 'on-first-retry',
// Default timeout for actions
actionTimeout: 10000,
// Default timeout for navigation
navigationTimeout: 30000,
},
// Global timeout for each test
timeout: 60000,
// Expect timeout
expect: {
timeout: 10000,
},
// Configure projects for major browsers
projects: [
// Setup project - runs before all tests to handle authentication
{
name: 'setup',
testDir: './e2e',
testMatch: /global\.setup\.ts/,
teardown: 'teardown',
},
{
name: 'teardown',
testDir: './e2e',
testMatch: /global\.teardown\.ts/,
},
// Main test project - uses authenticated state
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
// Use prepared auth state
storageState: 'e2e/.auth/user.json',
},
dependencies: ['setup'],
},
// Test in Firefox (optional, uncomment when needed)
// {
// name: 'firefox',
// use: {
// ...devices['Desktop Firefox'],
// storageState: 'e2e/.auth/user.json',
// },
// dependencies: ['setup'],
// },
// Test in WebKit (optional, uncomment when needed)
// {
// name: 'webkit',
// use: {
// ...devices['Desktop Safari'],
// storageState: 'e2e/.auth/user.json',
// },
// dependencies: ['setup'],
// },
// Test against mobile viewports (optional)
// {
// name: 'mobile-chrome',
// use: {
// ...devices['Pixel 5'],
// storageState: 'e2e/.auth/user.json',
// },
// dependencies: ['setup'],
// },
],
// Output folder for test artifacts
outputDir: 'e2e/test-results',
// Run your local dev server before starting the tests
// - Local: starts dev server automatically
// - CI with deployed env: set E2E_SKIP_WEB_SERVER=true to skip
...(SKIP_WEB_SERVER
? {}
: {
webServer: {
command: 'pnpm dev',
url: BASE_URL,
// Reuse existing server in local dev, start fresh in CI
reuseExistingServer: !process.env.CI,
timeout: 120000,
},
}),
})