cdp_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package cdp
  2. import (
  3. "context"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "clusterflow.dev/cfbrowser/api"
  8. "github.com/chromedp/cdproto/browser"
  9. )
  10. func TestDiscoveryAndCapabilities(t *testing.T) {
  11. if _, e := FindExecutable(filepath.Join(t.TempDir(), "missing")); e == nil {
  12. t.Fatal("missing executable accepted")
  13. }
  14. path, err := os.Executable()
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. if _, e := FindExecutable(path); e != nil {
  19. t.Fatal(e)
  20. }
  21. c, e := (&Driver{}).Probe(context.Background())
  22. if e != nil || !c.Supports("trustedInput") || c.Supports("crossOriginFrames") {
  23. t.Fatal(c, e)
  24. }
  25. ctx, cancel := context.WithCancel(context.Background())
  26. cancel()
  27. if _, e = (&Driver{}).Probe(ctx); e == nil {
  28. t.Fatal("canceled probe succeeded")
  29. }
  30. if _, e = (&Driver{}).Open(context.Background(), api.EngineOptions{Executable: "/missing/browser"}); e == nil {
  31. t.Fatal("missing browser started")
  32. }
  33. }
  34. func TestCanceledCommandsAndUnsupportedOperations(t *testing.T) {
  35. p := &enginePage{}
  36. ctx, cancel := context.WithCancel(context.Background())
  37. cancel()
  38. if e := p.run(ctx); e != context.Canceled {
  39. t.Fatal(e)
  40. }
  41. if _, e := p.Navigate(context.Background(), "", "bad"); e == nil {
  42. t.Fatal("invalid navigation accepted")
  43. }
  44. if e := p.Input(context.Background(), "unknown", "", 0, 0); e == nil {
  45. t.Fatal("unknown input accepted")
  46. }
  47. if e := p.Input(context.Background(), "press", "UnknownKey", 0, 0); e == nil {
  48. t.Fatal("invalid key accepted")
  49. }
  50. }
  51. func TestDownloadEventState(t *testing.T) {
  52. s := &session{downloads: map[string]api.Download{}, dir: t.TempDir(), limit: 1000}
  53. s.downloadEvent(&browser.EventDownloadWillBegin{GUID: "id", SuggestedFilename: "data.cif"})
  54. s.downloadEvent(&browser.EventDownloadProgress{GUID: "id", ReceivedBytes: 20, State: browser.DownloadProgressStateCompleted})
  55. s.downloadEvent(&browser.EventDownloadProgress{GUID: "missing", ReceivedBytes: 2})
  56. d := s.Downloads()
  57. if len(d) != 1 || d[0].State != "completed" || d[0].Bytes != 20 {
  58. t.Fatal(d)
  59. }
  60. }