example-rotation.spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { test, expect } from '@playwright/test';
  2. // BUG-11 修复:原版用 .example-chip 选择器,可能因 hash 路由或 CSS hash 失效
  3. // 改用 data-testid 属性(更稳定)+ 兼容 class 选择器(兜底)
  4. const CHIP_LOCATOR = '[data-testid="example-chip"], .example-chip';
  5. const CHIP_TEXT_LOCATOR = '[data-testid="example-chip-text"], .example-chip-text';
  6. const TEXTAREA_LOCATOR = '[data-testid="form-textarea"], .form-textarea, textarea';
  7. test.describe('示例提示词轮播', () => {
  8. test('每隔几秒自动切换', async ({ page }) => {
  9. await page.goto('http://localhost:5173/#/pages/book-generator/create', { waitUntil: 'networkidle' });
  10. await page.waitForSelector(CHIP_LOCATOR, { timeout: 15000 });
  11. const initial = (await page.locator(CHIP_LOCATOR).first().textContent())?.trim() || '';
  12. console.log('[T=0] 示例:', initial);
  13. expect(initial.length).toBeGreaterThan(0);
  14. await page.waitForTimeout(5500);
  15. const after5s = (await page.locator(CHIP_LOCATOR).first().textContent())?.trim() || '';
  16. console.log('[T=5s] 示例:', after5s);
  17. expect(after5s).not.toBe(initial);
  18. await page.waitForTimeout(5000);
  19. const after10s = (await page.locator(CHIP_LOCATOR).first().textContent())?.trim() || '';
  20. console.log('[T=10s]示例:', after10s);
  21. expect(after10s).not.toBe(after5s);
  22. await page.screenshot({ path: 'tests/example-rotation.png', fullPage: false });
  23. });
  24. test('点击示例填充到文本框', async ({ page }) => {
  25. await page.goto('http://localhost:5173/#/pages/book-generator/create', { waitUntil: 'networkidle' });
  26. await page.waitForSelector(CHIP_LOCATOR, { timeout: 15000 });
  27. const text = (await page.locator(CHIP_TEXT_LOCATOR).first().textContent())?.trim() || '';
  28. await page.locator(CHIP_LOCATOR).first().click();
  29. await page.waitForTimeout(500);
  30. const value = await page.locator(TEXTAREA_LOCATOR).first().evaluate((el: any) => el.value);
  31. console.log('点击后文本框内容:', value);
  32. expect(value).toBe(text);
  33. });
  34. test('页面只展示一个示例 chip(不重复铺开)', async ({ page }) => {
  35. await page.goto('http://localhost:5173/#/pages/book-generator/create', { waitUntil: 'networkidle' });
  36. await page.waitForSelector(CHIP_LOCATOR, { timeout: 15000 });
  37. const count = await page.locator(CHIP_LOCATOR).count();
  38. console.log('chip 数量:', count);
  39. expect(count).toBe(1);
  40. });
  41. });