73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import { test, expect } from '@playwright/test';
|
|
|
|
test('Test all links on homepage', async ({ page }) => {
|
|
const errors: string[] = [];
|
|
const consoleMessages: { type: string; text: string }[] = [];
|
|
|
|
page.on('console', msg => {
|
|
consoleMessages.push({ type: msg.type(), text: msg.text() });
|
|
if (msg.type() === 'error') {
|
|
console.log('Console Error:', msg.text());
|
|
}
|
|
});
|
|
|
|
page.on('pageerror', error => {
|
|
console.log('Page Error:', error.message);
|
|
errors.push(error.message);
|
|
});
|
|
|
|
await page.goto('http://localhost:8000/');
|
|
|
|
// Wait for page to load
|
|
await page.waitForLoadState('networkidle');
|
|
|
|
// Get all links
|
|
const links = await page.locator('a[href]').all();
|
|
|
|
console.log(`Found ${links.length} links`);
|
|
|
|
for (let i = 0; i < links.length; i++) {
|
|
const link = links[i];
|
|
const text = await link.textContent();
|
|
const href = await link.getAttribute('href');
|
|
|
|
console.log(`Clicking link ${i + 1}: "${text?.trim() || 'No text'}" -> ${href}`);
|
|
|
|
try {
|
|
// Clear previous errors
|
|
const currentErrors = [...errors];
|
|
errors.length = 0;
|
|
|
|
await link.click({ force: true });
|
|
|
|
// Wait a bit for navigation or error
|
|
await page.waitForTimeout(1000);
|
|
|
|
// Check for new errors
|
|
const newErrors = errors.filter(e => !currentErrors.includes(e));
|
|
if (newErrors.length > 0) {
|
|
console.log(`Error on link "${text?.trim() || 'No text'}":`, newErrors);
|
|
expect(newErrors).toHaveLength(0);
|
|
}
|
|
|
|
// Check console for errors after click
|
|
const recentConsole = consoleMessages.filter(msg => msg.text.includes('TypeError') || msg.text.includes('href') || msg.type === 'error');
|
|
if (recentConsole.length > 0) {
|
|
console.log(`Console error on link "${text?.trim() || 'No text'}":`, recentConsole);
|
|
expect(recentConsole).toHaveLength(0);
|
|
}
|
|
|
|
// If navigated, go back
|
|
if (page.url() !== 'http://localhost:8000/') {
|
|
await page.goBack();
|
|
await page.waitForLoadState('networkidle');
|
|
}
|
|
|
|
} catch (error: unknown) {
|
|
console.log(`Error clicking link "${text?.trim() || 'No text'}":`, (error as Error).message);
|
|
expect(error).toBeUndefined();
|
|
}
|
|
}
|
|
|
|
expect(errors).toHaveLength(0);
|
|
}); |