feat(test): add SEO journey tests for meta tags and structured data
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
|
||||
test.describe('SEO 关键指标验证 @journey @seo', () => {
|
||||
const pages = [
|
||||
{ url: '/', title: '四川睿新致远科技有限公司' },
|
||||
{ url: '/products', title: '产品服务' },
|
||||
{ url: '/cases', title: '成功案例' },
|
||||
{ url: '/news', title: '新闻动态' },
|
||||
{ url: '/about', title: '关于我们' },
|
||||
];
|
||||
|
||||
test('搜索引擎爬虫访问关键页面', async ({ page }) => {
|
||||
for (const pageInfo of pages) {
|
||||
await test.step(`验证 ${pageInfo.url} 的 SEO 元素`, async () => {
|
||||
await page.goto(pageInfo.url);
|
||||
|
||||
// 验证 title 标签
|
||||
await expect(page).toHaveTitle(new RegExp(pageInfo.title));
|
||||
|
||||
// 验证 meta description
|
||||
const metaDescription = page.locator('meta[name="description"]');
|
||||
await expect(metaDescription).toHaveAttribute('content', /.+/);
|
||||
|
||||
// 验证 meta keywords
|
||||
const metaKeywords = page.locator('meta[name="keywords"]');
|
||||
if (await metaKeywords.count() > 0) {
|
||||
await expect(metaKeywords).toHaveAttribute('content', /.+/);
|
||||
}
|
||||
|
||||
// 验证 canonical URL
|
||||
const canonical = page.locator('link[rel="canonical"]');
|
||||
if (await canonical.count() > 0) {
|
||||
await expect(canonical).toHaveAttribute('href', /.+/);
|
||||
}
|
||||
|
||||
// 验证 Open Graph 标签
|
||||
const ogTitle = page.locator('meta[property="og:title"]');
|
||||
if (await ogTitle.count() > 0) {
|
||||
await expect(ogTitle).toHaveAttribute('content', /.+/);
|
||||
}
|
||||
|
||||
// 验证结构化数据
|
||||
const structuredData = page.locator('script[type="application/ld+json"]');
|
||||
if (await structuredData.count() > 0) {
|
||||
const jsonContent = await structuredData.textContent();
|
||||
expect(() => JSON.parse(jsonContent!)).not.toThrow();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test('验证 sitemap.xml 可访问', async ({ page }) => {
|
||||
await page.goto('/sitemap.xml');
|
||||
const content = await page.content();
|
||||
expect(content).toContain('<?xml');
|
||||
expect(content).toContain('<urlset');
|
||||
});
|
||||
|
||||
test('验证 robots.txt 配置正确', async ({ page }) => {
|
||||
await page.goto('/robots.txt');
|
||||
const content = await page.content();
|
||||
expect(content).toContain('User-agent');
|
||||
expect(content).toContain('Sitemap');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user