-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowse.go
More file actions
50 lines (45 loc) · 1.55 KB
/
Copy pathbrowse.go
File metadata and controls
50 lines (45 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Package browse provides a Gin-like API for browser automation using pure CDP over WebSocket.
//
// browse-go applies Gin's middleware/context/group patterns to browser automation,
// giving Go developers a familiar, composable way to script browser interactions.
//
// engine := browse.Default(browse.WithHeadless(true))
// engine.MustLaunch()
// defer engine.Close()
//
// engine.Task("search", func(c *browse.Context) {
// c.MustNavigate("https://example.com")
// c.El("input[name=q]").MustInput("hello")
// c.El("button[type=submit]").MustClick()
// })
//
// engine.Run("search")
package browse
// Browser is the interface for browser lifecycle management.
// Engine implements this. The agent package depends on this interface
// rather than on *Engine directly, enabling testing without a real browser.
type Browser interface {
NewPage() (*Page, error)
NewPageAt(url string) (*Page, error)
ExistingPage() (*Page, error)
Close() error
}
// HandlerFunc defines the handler function signature for middleware and tasks.
type HandlerFunc func(*Context)
// HandlersChain is a slice of HandlerFunc used to build middleware chains.
type HandlersChain []HandlerFunc
// New creates a new Engine with no middleware attached.
func New(opts ...Option) *Engine {
e := &Engine{
opts: applyOptions(opts),
groups: make(map[string]*Group),
tasks: make(map[string]*taskEntry),
}
return e
}
// Default creates a new Engine with Logger and Recovery middleware attached.
func Default(opts ...Option) *Engine {
e := New(opts...)
e.Use(Logger(), Recovery())
return e
}