native_world_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package integration
  2. import (
  3. "clusterflow.dev/cfbrowser/api"
  4. "context"
  5. "encoding/json"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. func TestIsolatedWorldLifetime(t *testing.T) {
  14. engines(t, func(t *testing.T, driver api.Driver) {
  15. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  16. w.Header().Set("Content-Type", "text/html")
  17. _, _ = w.Write([]byte(`<html><body><button>Ready</button><script>window.cfPageMarker=7</script></body></html>`))
  18. }))
  19. defer server.Close()
  20. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  21. defer cancel()
  22. session, e := driver.Open(ctx, api.EngineOptions{Headless: true, NoSandbox: os.Getenv("CFBROWSER_TEST_NO_SANDBOX") == "1", Executable: os.Getenv("CFBROWSER_EXECUTABLE"), DownloadDir: t.TempDir(), ViewportWidth: 900, ViewportHeight: 600})
  23. if e != nil {
  24. t.Fatal(e)
  25. }
  26. defer session.Close()
  27. page, e := session.NewPage(ctx)
  28. if e != nil {
  29. t.Fatal(e)
  30. }
  31. defer page.Close()
  32. if _, e := page.Navigate(ctx, server.URL, "goto"); e != nil {
  33. t.Fatal(e)
  34. }
  35. evaluate := func(script, want string) {
  36. t.Helper()
  37. v, e := page.Evaluate(ctx, script)
  38. if e != nil || string(v) != want {
  39. t.Fatalf("evaluate: %s, want %s, err %v", v, want, e)
  40. }
  41. }
  42. evaluate(`(()=>{globalThis.cfSecret=41;globalThis.cfNode=document.querySelector('button');return typeof globalThis.cfPageMarker})()`, `"undefined"`)
  43. evaluate(`(()=>{globalThis.cfNode.textContent='Changed';globalThis.cfSecret++;return globalThis.cfSecret})()`, `42`)
  44. evaluate(`(()=>{const s=document.createElement('script');s.textContent="document.body.dataset.secret=typeof window.cfSecret";document.body.appendChild(s);return document.body.dataset.secret})()`, `"undefined"`)
  45. evaluate(`globalThis.cfNode===document.querySelector('button')`, `true`)
  46. evaluate(`document.querySelector('button').textContent`, `"Changed"`)
  47. if _, e := page.Evaluate(ctx, `(()=>{throw new Error('expected')})()`); e == nil {
  48. t.Fatal("JavaScript exception accepted")
  49. }
  50. evaluate(`({answer:42})`, `{"answer":42}`)
  51. if _, e := page.Navigate(ctx, server.URL+"/new", "goto"); e != nil {
  52. t.Fatal(e)
  53. }
  54. evaluate(`typeof globalThis.cfSecret`, `"undefined"`)
  55. raw, e := page.Evaluate(ctx, `({width:innerWidth,height:innerHeight})`)
  56. var size struct{ Width, Height int }
  57. caps, _ := driver.Probe(ctx)
  58. if e != nil || json.Unmarshal(raw, &size) != nil || size.Width <= 0 || size.Height <= 0 || strings.HasPrefix(caps.Engine, "native-") && (size.Width != 900 || size.Height != 600) {
  59. t.Fatal("viewport mismatch", string(raw), e)
  60. }
  61. })
  62. }