Seite läuft wieder, menü bringt keine fehler mehr
This commit is contained in:
106
tests/e2e/homepage-links.test.ts
Normal file
106
tests/e2e/homepage-links.test.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
|
||||
test.describe('Homepage Links Test', () => {
|
||||
test('Click all links on homepage and check for errors', async ({ page }) => {
|
||||
// Listen for failed requests (e.g., 404s)
|
||||
const failedRequests: { url: string; status: number }[] = [];
|
||||
page.on('response', response => {
|
||||
if (response.status() >= 400) {
|
||||
failedRequests.push({
|
||||
url: response.url(),
|
||||
status: response.status()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Listen for console errors
|
||||
const consoleErrors: string[] = [];
|
||||
page.on('console', msg => {
|
||||
if (msg.type() === 'error') {
|
||||
consoleErrors.push(msg.text());
|
||||
}
|
||||
});
|
||||
|
||||
// Navigate to homepage
|
||||
await page.goto('/');
|
||||
|
||||
// Wait for page to load
|
||||
await page.waitForLoadState('networkidle');
|
||||
|
||||
// Get all links
|
||||
const links = page.locator('a');
|
||||
const linkCount = await links.count();
|
||||
console.log(`Found ${linkCount} links on homepage.`);
|
||||
|
||||
for (let i = 0; i < linkCount; i++) {
|
||||
const link = links.nth(i);
|
||||
const href = await link.getAttribute('href');
|
||||
const text = await link.textContent() || '';
|
||||
|
||||
if (!href || href.startsWith('#') || href.startsWith('mailto:') || href.startsWith('tel:')) {
|
||||
console.log(`Skipping non-navigational link: ${text} (${href})`);
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`Clicking link ${i + 1}/${linkCount}: "${text}" -> ${href}`);
|
||||
|
||||
// For each link, create temporary listeners
|
||||
const linkFailedRequests: { url: string; status: number }[] = [];
|
||||
const linkConsoleErrors: string[] = [];
|
||||
const linkResponseHandler = (response: any) => {
|
||||
if (response.status() >= 400) {
|
||||
linkFailedRequests.push({
|
||||
url: response.url(),
|
||||
status: response.status()
|
||||
});
|
||||
}
|
||||
};
|
||||
const linkConsoleHandler = (msg: any) => {
|
||||
if (msg.type() === 'error') {
|
||||
linkConsoleErrors.push(msg.text());
|
||||
}
|
||||
};
|
||||
page.on('response', linkResponseHandler);
|
||||
page.on('console', linkConsoleHandler);
|
||||
|
||||
let currentUrl = page.url();
|
||||
try {
|
||||
// Hover and click
|
||||
await link.hover();
|
||||
await link.click({ force: true });
|
||||
|
||||
// Wait for navigation or load
|
||||
await page.waitForTimeout(2000);
|
||||
|
||||
currentUrl = page.url();
|
||||
|
||||
// Remove temporary handlers
|
||||
page.removeListener('response', linkResponseHandler);
|
||||
page.removeListener('console', linkConsoleHandler);
|
||||
|
||||
// Check for errors during this click
|
||||
expect(linkFailedRequests.length).toBe(0);
|
||||
expect(linkConsoleErrors.length).toBe(0);
|
||||
|
||||
console.log(`✓ Link "${text}" successful: ${currentUrl}`);
|
||||
} catch (error: unknown) {
|
||||
// Remove handlers
|
||||
page.removeListener('response', linkResponseHandler);
|
||||
page.removeListener('console', linkConsoleHandler);
|
||||
|
||||
console.error(`✗ Error clicking link "${text}": ${(error as Error).message}`);
|
||||
}
|
||||
|
||||
// Go back to homepage if navigated away
|
||||
if (currentUrl !== page.url() && !currentUrl.includes('/')) {
|
||||
await page.goBack({ waitUntil: 'networkidle' });
|
||||
}
|
||||
}
|
||||
|
||||
// Final checks
|
||||
expect(failedRequests.length).toBe(0);
|
||||
expect(consoleErrors.length).toBe(0);
|
||||
|
||||
console.log('All links tested successfully.');
|
||||
});
|
||||
});
|
||||
@@ -1,73 +0,0 @@
|
||||
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);
|
||||
});
|
||||
Reference in New Issue
Block a user