const express = require('express');
const path = require('path');
const fs = require('fs');
const { spawn } = require('child_process');
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.use('/test-results', express.static(path.join(__dirname, 'test-results')));
app.use('/tests', express.static(path.join(__dirname, 'tests')));
app.get('/', (req, res) => {
res.send(`
Playwright Test Environment
🎠Playwright Test Environment
✅ Environment is ready!
Quick Actions
View Reports
Environment Info
Playwright Version: v1.55.0
Available Browsers: Chromium, Firefox, WebKit
Test Directory: ./tests
Reports Directory: ./test-results
Usage Instructions
- Upload test files to the
tests/ directory
- Configure tests in
playwright.config.ts
- Run tests using the button above or CLI command
- View HTML reports in the test-results directory
Example Test Code
import { test, expect } from '@playwright/test';
test('basic test', async ({ page }) => {
await page.goto('https://playwright.dev/');
await expect(page).toHaveTitle(/Playwright/);
});
`);
});
app.post('/api/run-tests', (req, res) => {
const testProcess = spawn('npx', ['playwright', 'test'], {
cwd: process.cwd(),
stdio: 'inherit'
});
res.json({ message: 'Tests execution started, check reports in 30 seconds' });
});
app.post('/api/generate-example', (req, res) => {
const exampleTest = `import { test, expect } from '@playwright/test';
test('playwright homepage test', async ({ page }) => {
await page.goto('https://playwright.dev/');
// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Playwright/);
// create a locator
const getStarted = page.getByRole('link', { name: 'Get started' });
// Expect an attribute "to be strictly equal" to the value.
await expect(getStarted).toHaveAttribute('href', '/docs/intro');
// Click the get started link.
await getStarted.click();
// Expects the URL to contain intro.
await expect(page).toHaveURL(/.*intro/);
});
test('example.com test', async ({ page }) => {
await page.goto('https://example.com');
await expect(page).toHaveTitle('Example Domain');
await expect(page.locator('h1')).toContainText('Example Domain');
});`;
if (!fs.existsSync('./tests')) {
fs.mkdirSync('./tests', { recursive: true });
}
fs.writeFileSync('./tests/example.spec.ts', exampleTest);
res.json({ message: 'Example test generated at ./tests/example.spec.ts' });
});
app.listen(port, '0.0.0.0', () => {
console.log(`🎠Playwright server running at http://0.0.0.0:${port}`);
console.log(`📊 Test reports will be available at http://0.0.0.0:${port}/test-results`);
});