playwright

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2025 License: MIT Imports: 10 Imported by: 0

README

Playwright Service

Browser automation service using Playwright with comprehensive mock-based testing.

Testing

All tests use mocks for fast execution (~0.18s). No browser downloads required.

# Run all tests
go test -v ./internal/playwright

# Generate mocks (after interface changes)
go run github.com/maxbrunsfeld/counterfeiter/v6 -o internal/playwright/mocks/browser_automation.go internal/playwright BrowserAutomation

Files

  • playwright.go - Service implementation (generated, do not edit)
  • playwright_test.go - Basic unit tests
  • playwright_integration_test.go - Integration tests
  • mocks/browser_automation.go - Generated mock

Documentation

Index

Constants

View Source
const (
	DefaultSessionID = "default"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BrowserAutomation

type BrowserAutomation interface {
	// Browser management
	LaunchBrowser(ctx context.Context, config *BrowserConfig) (*BrowserSession, error)
	CloseBrowser(ctx context.Context, sessionID string) error
	GetSession(sessionID string) (*BrowserSession, error)
	GetOrCreateDefaultSession(ctx context.Context) (*BrowserSession, error)

	// Page operations
	NavigateToURL(ctx context.Context, sessionID, url string, waitUntil string, timeout time.Duration) error
	ClickElement(ctx context.Context, sessionID, selector string, options map[string]any) error
	FillForm(ctx context.Context, sessionID string, fields []map[string]any, submit bool, submitSelector string) error
	ExtractData(ctx context.Context, sessionID string, extractors []map[string]any, format string) (string, error)
	TakeScreenshot(ctx context.Context, sessionID, path string, fullPage bool, selector string, format string, quality int) error
	ExecuteScript(ctx context.Context, sessionID, script string, args []any) (any, error)
	WaitForCondition(ctx context.Context, sessionID, condition, selector, state string, timeout time.Duration, customFunction string) error
	HandleAuthentication(ctx context.Context, sessionID, authType, username, password, loginURL string, selectors map[string]string) error

	// Service management
	GetHealth(ctx context.Context) error
	Shutdown(ctx context.Context) error
	GetConfig() *config.Config
}

BrowserAutomation represents the playwright dependency interface Playwright service for browser automation and web testing

func NewPlaywrightService

func NewPlaywrightService(logger *zap.Logger, cfg *config.Config) (BrowserAutomation, error)

NewPlaywrightService creates a new instance of BrowserAutomation

type BrowserConfig

type BrowserConfig struct {
	Engine         BrowserEngine
	Headless       bool
	Timeout        time.Duration
	ViewportWidth  int
	ViewportHeight int
	Args           []string
}

BrowserConfig holds browser configuration options

func DefaultBrowserConfig

func DefaultBrowserConfig() *BrowserConfig

DefaultBrowserConfig returns default browser configuration

func NewBrowserConfigFromConfig

func NewBrowserConfigFromConfig(cfg *config.Config) *BrowserConfig

NewBrowserConfigFromConfig creates browser config from app configuration

type BrowserEngine

type BrowserEngine string

BrowserEngine represents the browser type

const (
	Chromium BrowserEngine = "chromium"
	Firefox  BrowserEngine = "firefox"
	WebKit   BrowserEngine = "webkit"
)

type BrowserSession

type BrowserSession struct {
	ID       string
	Browser  playwright.Browser
	Context  playwright.BrowserContext
	Page     playwright.Page
	Created  time.Time
	LastUsed time.Time
}

BrowserSession represents an active browser session

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL