browser_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Package integration exercises real engines against a deterministic local site.
  2. package integration
  3. import (
  4. "bytes"
  5. "context"
  6. "encoding/json"
  7. "errors"
  8. "image/png"
  9. "net/http/httptest"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "testing"
  14. "time"
  15. "clusterflow.dev/cfbrowser"
  16. "clusterflow.dev/cfbrowser/api"
  17. "clusterflow.dev/cfbrowser/driver/cdp"
  18. native "clusterflow.dev/cfbrowser/driver/native"
  19. "clusterflow.dev/cfbrowser/internal/testsite"
  20. "clusterflow.dev/cfbrowser/policy"
  21. )
  22. type harness struct {
  23. m *cfbrowser.Manager
  24. owner api.Owner
  25. s api.SessionInfo
  26. p api.PageInfo
  27. server *httptest.Server
  28. }
  29. func engines(t *testing.T, fn func(*testing.T, api.Driver)) {
  30. t.Helper()
  31. if os.Getenv("CFBROWSER_INTEGRATION") != "1" {
  32. t.Skip("set CFBROWSER_INTEGRATION=1 to run real browser tests")
  33. }
  34. engine := os.Getenv("CFBROWSER_TEST_ENGINE")
  35. if engine != "" && engine != "both" && engine != "native" && engine != "cdp" {
  36. t.Fatalf("invalid CFBROWSER_TEST_ENGINE: %s", engine)
  37. }
  38. if engine != "native" {
  39. t.Run("cdp", func(t *testing.T) { fn(t, &cdp.Driver{}) })
  40. }
  41. if engine == "native" || engine == "both" || os.Getenv("CFBROWSER_NATIVE") == "1" {
  42. t.Run("native", func(t *testing.T) {
  43. fn(t, &native.Driver{WorkerPath: os.Getenv("CFBROWSER_WORKER"), Diagnostics: os.Stderr})
  44. })
  45. }
  46. }
  47. func setup(t *testing.T, driver api.Driver) *harness {
  48. t.Helper()
  49. server := httptest.NewServer(testsite.Handler())
  50. t.Cleanup(server.Close)
  51. m, e := cfbrowser.New(cfbrowser.Config{Executable: os.Getenv("CFBROWSER_EXECUTABLE"), NoSandbox: os.Getenv("CFBROWSER_TEST_NO_SANDBOX") == "1", Timeout: 30 * time.Second, Policy: policy.URLPolicy{AllowOrigins: []string{server.URL}}}, driver)
  52. if e != nil {
  53. t.Fatal(e)
  54. }
  55. t.Cleanup(func() {
  56. if e := m.Close(); e != nil {
  57. t.Error(e)
  58. }
  59. })
  60. o := api.Owner{EnvironmentID: "ssh1", ThreadID: "thread1", RunID: t.Name(), SSHSessionID: "connection1"}
  61. ctx := context.Background()
  62. s, e := m.CreateSession(ctx, o)
  63. if e != nil {
  64. t.Fatal(e)
  65. }
  66. p, e := m.NewPage(ctx, o, s.ID)
  67. if e != nil {
  68. t.Fatal(e)
  69. }
  70. if _, e = m.Navigate(ctx, o, s.ID, p.PageID, server.URL, "goto"); e != nil {
  71. t.Fatal(e)
  72. }
  73. return &harness{m: m, owner: o, s: s, p: p, server: server}
  74. }
  75. func (h *harness) snapshot(t *testing.T) api.Snapshot {
  76. t.Helper()
  77. s, e := h.m.Snapshot(context.Background(), h.owner, h.s.ID, h.p.PageID, api.SnapshotRequest{})
  78. if e != nil {
  79. t.Fatal(e)
  80. }
  81. return s
  82. }
  83. func ref(t *testing.T, s api.Snapshot, role, name string) string {
  84. t.Helper()
  85. for _, e := range s.Elements {
  86. if e.Role == role && e.Name == name {
  87. return e.Ref
  88. }
  89. }
  90. t.Fatalf("missing %s %s in %+v", role, name, s)
  91. return ""
  92. }
  93. func (h *harness) act(t *testing.T, s api.Snapshot, role, name, action, value string) {
  94. t.Helper()
  95. r, e := h.m.Act(context.Background(), h.owner, h.s.ID, h.p.PageID, api.ActionRequest{SnapshotID: s.SnapshotID, Ref: ref(t, s, role, name), Action: action, Value: value})
  96. if e != nil || r.Effect != "dispatched" {
  97. t.Fatalf("%s %s: %+v %v", action, name, r, e)
  98. }
  99. }
  100. func TestRealDynamicWorkflow(t *testing.T) {
  101. engines(t, func(t *testing.T, d api.Driver) {
  102. h := setup(t, d)
  103. ctx := context.Background()
  104. s := h.snapshot(t)
  105. encoded, _ := json.Marshal(s)
  106. if strings.Contains(string(encoded), "hidden-secret") {
  107. t.Fatal("password leaked into snapshot")
  108. }
  109. h.act(t, s, "textbox", "Query", "fill", "La2O3 科学 \"\\ test")
  110. capabilities, _ := h.m.Capabilities(ctx)
  111. if capabilities.Supports("trustedInput") {
  112. h.act(t, s, "textbox", "Query", "type", "X")
  113. h.act(t, s, "textbox", "Query", "press", "Backspace")
  114. h.act(t, s, "button", "Search", "hover", "")
  115. }
  116. h.act(t, s, "button", "Search", "click", "")
  117. if e := h.m.Wait(ctx, h.owner, h.s.ID, h.p.PageID, api.WaitRequest{Kind: "text", Value: "Results for La2O3 科学", TimeoutMS: 3000}); e != nil {
  118. t.Fatal(e)
  119. }
  120. caps, e := h.m.Capabilities(ctx)
  121. if e != nil {
  122. t.Fatal(e)
  123. }
  124. want := "trusted=false"
  125. if caps.Supports("trustedInput") {
  126. want = "trusted=true"
  127. }
  128. if e = h.m.Wait(ctx, h.owner, h.s.ID, h.p.PageID, api.WaitRequest{Kind: "text", Value: want, TimeoutMS: 1000}); e != nil {
  129. t.Fatal("input trust contract mismatch", e)
  130. }
  131. table, e := h.m.Read(ctx, h.owner, h.s.ID, h.p.PageID, api.ReadRequest{Mode: "table"})
  132. if e != nil || len(table.Rows) != 2 || table.Rows[1][0] != "La2O3" {
  133. t.Fatal(table, e)
  134. }
  135. s = h.snapshot(t)
  136. h.act(t, s, "checkbox", "Enabled", "check", "")
  137. h.act(t, s, "checkbox", "Enabled", "uncheck", "")
  138. h.act(t, s, "combobox", "Format", "select", "xyz")
  139. small, e := h.m.Snapshot(ctx, h.owner, h.s.ID, h.p.PageID, api.SnapshotRequest{MaxBytes: 1024})
  140. if e != nil {
  141. t.Fatal(e)
  142. }
  143. b, _ := json.Marshal(small)
  144. if len(b) > 1024 {
  145. t.Fatal("wire budget exceeded")
  146. }
  147. if caps.Supports("screenshotViewport") {
  148. f, e := h.m.Capture(ctx, h.owner, h.s.ID, h.p.PageID)
  149. if e != nil || f.Size < 100 {
  150. t.Fatal(f, e)
  151. }
  152. dest := t.TempDir()
  153. if _, e = h.m.Export(ctx, h.owner, h.s.ID, f.ID, dest, "shot.png"); e != nil {
  154. t.Fatal(e)
  155. }
  156. if _, e = os.Stat(filepath.Join(dest, "shot.png")); e != nil {
  157. t.Fatal(e)
  158. }
  159. }
  160. })
  161. }
  162. func TestRealStaleRefsCancellationAndScope(t *testing.T) {
  163. engines(t, func(t *testing.T, d api.Driver) {
  164. h := setup(t, d)
  165. s := h.snapshot(t)
  166. search := ref(t, s, "button", "Search")
  167. h.act(t, s, "button", "Replace Search", "click", "")
  168. _, e := h.m.Act(context.Background(), h.owner, h.s.ID, h.p.PageID, api.ActionRequest{SnapshotID: s.SnapshotID, Ref: search, Action: "click"})
  169. var typed *api.Error
  170. if !errors.As(e, &typed) || typed.Code != "BROWSER_STALE_REF" {
  171. t.Fatal("replaced element was accepted", e)
  172. }
  173. other := h.owner
  174. other.RunID = "child-run"
  175. if _, e = h.m.Snapshot(context.Background(), other, h.s.ID, h.p.PageID, api.SnapshotRequest{}); e == nil {
  176. t.Fatal("cross-run page access")
  177. }
  178. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
  179. defer cancel()
  180. started := time.Now()
  181. e = h.m.Wait(ctx, h.owner, h.s.ID, h.p.PageID, api.WaitRequest{Kind: "text", Value: "never-appears"})
  182. if e == nil || time.Since(started) > 2*time.Second {
  183. t.Fatal("wait cancellation failed", e)
  184. }
  185. if _, e = h.m.Navigate(context.Background(), h.owner, h.s.ID, h.p.PageID, h.server.URL+"/cookie", "goto"); e != nil {
  186. t.Fatal(e)
  187. }
  188. _, e = h.m.Act(context.Background(), h.owner, h.s.ID, h.p.PageID, api.ActionRequest{SnapshotID: s.SnapshotID, Ref: search, Action: "click"})
  189. if e == nil {
  190. t.Fatal("navigation did not invalidate ref")
  191. }
  192. })
  193. }
  194. func TestRealCookieIsolationAndSessionLifecycle(t *testing.T) {
  195. engines(t, func(t *testing.T, d api.Driver) {
  196. h := setup(t, d)
  197. ctx := context.Background()
  198. p2, e := h.m.NewPage(ctx, h.owner, h.s.ID)
  199. if e != nil {
  200. t.Fatal(e)
  201. }
  202. if _, e = h.m.Navigate(ctx, h.owner, h.s.ID, p2.PageID, h.server.URL+"/cookie", "goto"); e != nil {
  203. t.Fatal(e)
  204. }
  205. read, e := h.m.Read(ctx, h.owner, h.s.ID, p2.PageID, api.ReadRequest{})
  206. if e != nil || !strings.Contains(read.Text, "cookie=active") {
  207. t.Fatal("same session lost cookie", read, e)
  208. }
  209. if _, e = h.m.Navigate(ctx, h.owner, h.s.ID, p2.PageID, h.server.URL, "goto"); e != nil {
  210. t.Fatal(e)
  211. }
  212. if _, e = h.m.Navigate(ctx, h.owner, h.s.ID, p2.PageID, "", "back"); e != nil {
  213. t.Fatal(e)
  214. }
  215. if _, e = h.m.Navigate(ctx, h.owner, h.s.ID, p2.PageID, "", "forward"); e != nil {
  216. t.Fatal(e)
  217. }
  218. if _, e = h.m.Navigate(ctx, h.owner, h.s.ID, p2.PageID, "", "reload"); e != nil {
  219. t.Fatal(e)
  220. }
  221. if e = h.m.ClosePage(ctx, h.owner, h.s.ID, p2.PageID); e != nil {
  222. t.Fatal(e)
  223. }
  224. other := h.owner
  225. other.RunID = "run2"
  226. s2, e := h.m.CreateSession(ctx, other)
  227. if e != nil {
  228. t.Fatal(e)
  229. }
  230. p3, e := h.m.NewPage(ctx, other, s2.ID)
  231. if e != nil {
  232. t.Fatal(e)
  233. }
  234. if _, e = h.m.Navigate(ctx, other, s2.ID, p3.PageID, h.server.URL+"/cookie", "goto"); e != nil {
  235. t.Fatal(e)
  236. }
  237. read, e = h.m.Read(ctx, other, s2.ID, p3.PageID, api.ReadRequest{})
  238. if e != nil || !strings.Contains(read.Text, "cookie=empty") {
  239. t.Fatal("cookie crossed session boundary", read, e)
  240. }
  241. if e = h.m.CloseSession(other, s2.ID); e != nil {
  242. t.Fatal(e)
  243. }
  244. if _, e = h.m.Read(ctx, other, s2.ID, p3.PageID, api.ReadRequest{}); e == nil {
  245. t.Fatal("closed session remained callable")
  246. }
  247. })
  248. }
  249. func TestRealAuthenticatedAndBlobDownloads(t *testing.T) {
  250. engines(t, func(t *testing.T, d api.Driver) {
  251. h := setup(t, d)
  252. caps, _ := h.m.Capabilities(context.Background())
  253. if !caps.Supports("downloads") {
  254. t.Skip("backend explicitly reports downloads unsupported")
  255. }
  256. s := h.snapshot(t)
  257. h.act(t, s, "link", "Download CIF", "click", "")
  258. h.act(t, s, "button", "Download blob", "click", "")
  259. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  260. defer cancel()
  261. var files []api.FileInfo
  262. for {
  263. var e error
  264. files, e = h.m.Files(ctx, h.owner, h.s.ID)
  265. if e != nil {
  266. t.Fatal(e)
  267. }
  268. if len(files) >= 2 {
  269. break
  270. }
  271. select {
  272. case <-ctx.Done():
  273. t.Fatal("downloads did not complete")
  274. case <-time.After(50 * time.Millisecond):
  275. }
  276. }
  277. dest := t.TempDir()
  278. contents := map[string]string{}
  279. for _, f := range files {
  280. if _, e := h.m.Export(ctx, h.owner, h.s.ID, f.ID, dest, f.Name); e != nil {
  281. t.Fatal(e)
  282. }
  283. b, e := os.ReadFile(filepath.Join(dest, f.Name))
  284. if e != nil {
  285. t.Fatal(e)
  286. }
  287. contents[f.Name] = string(b)
  288. }
  289. if !strings.Contains(contents["sample.cif"], "data_La2O3") || contents["blob.txt"] != "blob-data" {
  290. t.Fatalf("incorrect downloads: %+v", contents)
  291. }
  292. })
  293. }
  294. func TestRealOpenShadowDOM(t *testing.T) {
  295. engines(t, func(t *testing.T, d api.Driver) {
  296. h := setup(t, d)
  297. ctx := context.Background()
  298. caps, _ := h.m.Capabilities(ctx)
  299. if !caps.Supports("openShadowDOM") {
  300. t.Skip("openShadowDOM unsupported")
  301. }
  302. if _, err := h.m.Navigate(ctx, h.owner, h.s.ID, h.p.PageID, h.server.URL+"/shadow", "goto"); err != nil {
  303. t.Fatal(err)
  304. }
  305. s := h.snapshot(t)
  306. for _, e := range s.Elements {
  307. if e.Name == "Closed secret button" {
  308. t.Fatal("closed shadow root exposed")
  309. }
  310. }
  311. h.act(t, s, "textbox", "Shadow Query", "fill", "La2O3")
  312. h.act(t, s, "button", "Shadow Search", "click", "")
  313. if err := h.m.Wait(ctx, h.owner, h.s.ID, h.p.PageID, api.WaitRequest{Kind: "text", Value: "Shadow result La2O3", TimeoutMS: 2000}); err != nil {
  314. t.Fatal(err)
  315. }
  316. if err := h.m.Wait(ctx, h.owner, h.s.ID, h.p.PageID, api.WaitRequest{Kind: "text", Value: "Shadow visible text", TimeoutMS: 2000}); err != nil {
  317. t.Fatal(err)
  318. }
  319. r, err := h.m.Read(ctx, h.owner, h.s.ID, h.p.PageID, api.ReadRequest{})
  320. if err != nil || (!strings.Contains(r.Text, "Shadow visible text") || !strings.Contains(r.Text, "Direct shadow text")) {
  321. t.Fatal(r, err)
  322. }
  323. table, err := h.m.Read(ctx, h.owner, h.s.ID, h.p.PageID, api.ReadRequest{Mode: "table"})
  324. if err != nil || len(table.Rows) != 1 || table.Rows[0][0] != "La2O3 shadow" {
  325. t.Fatal(table, err)
  326. }
  327. target := ref(t, s, "button", "Shadow Search")
  328. h.act(t, s, "button", "Replace shadow", "click", "")
  329. _, err = h.m.Act(ctx, h.owner, h.s.ID, h.p.PageID, api.ActionRequest{SnapshotID: s.SnapshotID, Ref: target, Action: "click"})
  330. var typed *api.Error
  331. if !errors.As(err, &typed) || typed.Code != "BROWSER_STALE_REF" {
  332. t.Fatal("replaced shadow ref accepted", err)
  333. }
  334. })
  335. }
  336. func TestRealBoundedFullPageCapture(t *testing.T) {
  337. engines(t, func(t *testing.T, d api.Driver) {
  338. h := setup(t, d)
  339. ctx := context.Background()
  340. caps, _ := h.m.Capabilities(ctx)
  341. if !caps.Supports("screenshotFullPage") {
  342. t.Skip("full-page capture unsupported on this backend")
  343. }
  344. if _, err := h.m.Navigate(ctx, h.owner, h.s.ID, h.p.PageID, h.server.URL+"/long", "goto"); err != nil {
  345. t.Fatal(err)
  346. }
  347. f, err := h.m.CapturePage(ctx, h.owner, h.s.ID, h.p.PageID, true)
  348. if err != nil {
  349. t.Fatal(err)
  350. }
  351. dest := t.TempDir()
  352. if _, err = h.m.Export(ctx, h.owner, h.s.ID, f.ID, dest, "full.png"); err != nil {
  353. t.Fatal(err)
  354. }
  355. data, err := os.ReadFile(filepath.Join(dest, "full.png"))
  356. if err != nil {
  357. t.Fatal(err)
  358. }
  359. img, err := png.Decode(bytes.NewReader(data))
  360. if err != nil || img.Bounds().Dy() < 2200 {
  361. t.Fatal("full document was not captured", err)
  362. }
  363. top := img.At(10, 100)
  364. bottom := img.At(10, img.Bounds().Dy()-10)
  365. if top == bottom {
  366. t.Fatal("blank capture")
  367. }
  368. if _, err = h.m.Navigate(ctx, h.owner, h.s.ID, h.p.PageID, h.server.URL+"/long?huge=1", "goto"); err != nil {
  369. t.Fatal(err)
  370. }
  371. _, err = h.m.CapturePage(ctx, h.owner, h.s.ID, h.p.PageID, true)
  372. var typed *api.Error
  373. if !errors.As(err, &typed) || typed.Code != "BROWSER_RESULT_LIMIT" {
  374. t.Fatal("oversized full page accepted", err)
  375. }
  376. })
  377. }