A lightweight Go library for launching and controlling web browsers programmatically, designed for automation and development tools.
The main entry point is the New function, which creates a new browser controller:
import "github.com/cdvelop/devbrowser"
type myServerConfig struct{}
func (myServerConfig) GetServerPort() string { return "8080" }
type myUI struct{}
func (myUI) ReturnFocus() {}
func main() {
exitChan := make(chan bool)
browser := devbrowser.New(myServerConfig{}, myUI{}, exitChan)
err := browser.OpenBrowser()
if err != nil {
// handle error
}
// ... use browser ...
browser.CloseBrowser()
}-
New(sc serverConfig, ui userInterface, exitChan chan bool) *DevBrowser: Create a new DevBrowser instance. -
(*DevBrowser) OpenBrowser() error: Launch a new browser window. -
(*DevBrowser) CloseBrowser() error: Close the browser and clean up resources. -
(*DevBrowser) Reload() error: Reload the current page in the browser. -
(*DevBrowser) RestartBrowser() error: Restart the browser (close and reopen). -
(*DevBrowser) BrowserStartUrlChanged(fieldName, oldValue, newValue string) error: Handle changes to the start URL and restart the browser if open. -
(*DevBrowser) BrowserPositionAndSizeChanged(fieldName, oldValue, newValue string) error: Change the browser window's position and size, and restart the browser. -
(*DevBrowser) Name() stringand(*DevBrowser) Label() string: For UI integration, returns the component name and label. -
(*DevBrowser) Execute(progress func(msgs ...any)): For UI integration, toggles browser open/close and reports progress. -
(*DevBrowser) SetHeadless(headless bool): Configure whether the browser runs in headless mode (without a visible UI).- Signature:
func (b *DevBrowser) SetHeadless(headless bool) - Default:
false(shows the browser window). This is convenient for local development and debugging. - Tests: the test helper
DefaultTestBrowser()configures the returnedDevBrowserwithheadless = trueso unit tests run without requiring a graphical display. - Notes: Call this before
OpenBrowser()(or before the browser context is created) to ensure the headless flag is applied when launching Chrome/Chromium. - Example:
- Signature:
db := devbrowser.New(myServerConfig{}, myUI{}, exitChan)
// run with no UI (useful in CI/tests)
db.SetHeadless(true)
err := db.OpenBrowser()
if err != nil {
// handle error
}(*DevBrowser) GetConsoleLogs() ([]string, error): Capture console messages from the loaded page.- Signature:
func (b *DevBrowser) GetConsoleLogs() ([]string, error) - Behavior: injects a small script into the page that maintains
window.__consoleLogsand returns its contents as a slice of strings. Capturesconsole.log,console.error,console.warn, andconsole.infomessages. - Requirements: the browser context must be initialized (
OpenBrowser()called and context created). Returns an error if the context is not ready or the evaluation fails. - Example:
- Signature:
logs, err := db.GetConsoleLogs()
if err != nil {
// handle error
}
for _, l := range logs {
fmt.Println(l)
}(*DevBrowser) ClearConsoleLogs() error: Clear the in-page captured console log buffer.- Signature:
func (b *DevBrowser) ClearConsoleLogs() error - Behavior: executes a small script that resets
window.__consoleLogs = []if present. - Requirements: the browser context must be initialized. Returns an error if the evaluation fails.
- Example:
- Signature:
err := db.ClearConsoleLogs()
if err != nil {
// handle error
}