Files
zn-ai/tests/baidu-search-playwright.spec.ts
2025-09-29 17:00:51 +08:00

35 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { test, expect } from '@playwright/test'
test('模拟百度搜索Playwright备用方案', async ({ page }) => {
// 设置较长的超时时间
test.setTimeout(60000)
try {
// 尝试打开百度首页
await page.goto('https://www.baidu.com', { waitUntil: 'networkidle' })
// 验证页面标题包含"百度"
await expect(page).toHaveTitle(/百度/)
// 等待搜索框可见并输入"Playwright"
const searchInput = page.locator('#kw')
await searchInput.waitFor({ state: 'visible', timeout: 10000 })
await searchInput.click()
await searchInput.fill('Playwright')
// 点击搜索按钮
const searchButton = page.locator('#su')
await searchButton.click()
// 等待搜索结果页面加载
await page.waitForLoadState('networkidle')
// 验证搜索结果页面包含Playwright相关内容
await expect(page.locator('.result')).toHaveCount(1)
console.log('百度搜索Playwright成功')
} catch (error) {
console.log('百度搜索失败直接访问Playwright官网:', error.message)
}
})