link-validation.spec.template.ts•1.39 kB
// Generated by DocuMCP - Link Validation Tests
import { test, expect } from "@playwright/test";
test.describe("Link Validation", () => {
test("should load homepage", async ({ page }) => {
await page.goto("/");
await expect(page).toHaveTitle(/{{projectName}}/i);
});
test("all internal links should work", async ({ page }) => {
await page.goto("/");
const links = await page.locator('a[href^="/"], a[href^="./"]').all();
for (const link of links.slice(0, 20)) {
const href = await link.getAttribute("href");
if (!href || href.startsWith("#")) continue;
await link.click();
await page.waitForLoadState("networkidle");
await expect(page).not.toHaveTitle(/404/i);
await page.goBack();
}
});
test("external links should be valid", async ({ page, request }) => {
await page.goto("/");
const externalLinks = await page.locator('a[href^="http"]').all();
const urls = new Set<string>();
for (const link of externalLinks) {
const href = await link.getAttribute("href");
if (href) urls.add(href);
}
for (const url of Array.from(urls).slice(0, 10)) {
try {
const response = await request.head(url, { timeout: 10000 });
expect(response.status()).toBeLessThan(400);
} catch (error) {
console.warn(`Link check failed: ${url}`);
}
}
});
});