form-validation.spec.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { expect, test } from "@playwright/test"
  2. test("connection validation stays inline without browser invalid events", async ({ page }) => {
  3. await page.goto("/e2e/paas/index.html?ux")
  4. await page.evaluate(() => {
  5. Object.assign(window, { nativeInvalidEvents: 0 })
  6. document.addEventListener(
  7. "invalid",
  8. () => {
  9. const state = window as unknown as { nativeInvalidEvents: number }
  10. state.nativeInvalidEvents++
  11. },
  12. true,
  13. )
  14. })
  15. const key = page.getByLabel("API Key", { exact: true })
  16. const url = page.getByLabel("平台地址", { exact: true })
  17. const submit = page.getByRole("button", { name: "连接平台", exact: true })
  18. await submit.click()
  19. await expect(page.getByText("请输入平台访问凭证。", { exact: true })).toBeVisible()
  20. await expect(key).toHaveAttribute("aria-invalid", "true")
  21. await key.fill("form-check")
  22. await expect(page.getByText("请输入平台访问凭证。", { exact: true })).toHaveCount(0)
  23. await url.fill("not-a-platform-url")
  24. await submit.click()
  25. await expect(page.getByText("请输入以 http:// 或 https:// 开头的平台地址。", { exact: true })).toBeVisible()
  26. await expect(url).toHaveAttribute("aria-invalid", "true")
  27. await expect(key).toHaveValue("form-check")
  28. await page.screenshot({
  29. path: test.info().outputPath("inline-validation.png"),
  30. })
  31. expect(await page.evaluate(() => (window as unknown as { nativeInvalidEvents: number }).nativeInvalidEvents)).toBe(0)
  32. await url.fill("http://127.0.0.1:8000")
  33. await submit.click()
  34. await expect(page.getByRole("textbox", { name: "消息", exact: true })).toBeEnabled()
  35. expect(await page.locator("form").evaluateAll((forms) => forms.every((form) => form.noValidate))).toBe(true)
  36. })
  37. test("invalid research settings show a field error and valid settings still save", async ({ page }) => {
  38. await page.goto("/e2e/paas/index.html?ux")
  39. await page.getByLabel("API Key", { exact: true }).fill("form-settings")
  40. await page.getByRole("button", { name: "连接平台", exact: true }).click()
  41. await page.getByRole("button", { name: "研究设置", exact: true }).click()
  42. const rounds = page.getByLabel("最多研究轮次", { exact: true })
  43. await rounds.fill("9")
  44. await page.getByRole("button", { name: "保存修改", exact: true }).click()
  45. await expect(rounds).toBeFocused()
  46. await expect(rounds).toHaveAttribute("aria-invalid", "true")
  47. await expect(page.getByRole("alert")).toContainText("最多研究轮次")
  48. expect(await page.evaluate(() => window.paasTestCalls.filter((call) => call.action === "update-agent"))).toHaveLength(
  49. 0,
  50. )
  51. await rounds.fill("6")
  52. await expect(page.getByRole("alert")).toHaveCount(0)
  53. await page.getByRole("button", { name: "保存修改", exact: true }).click()
  54. await expect
  55. .poll(() => page.evaluate(() => window.paasTestCalls.filter((call) => call.action === "update-agent").length))
  56. .toBe(1)
  57. })