core

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package core provides a high-level UI driver DSL on top of Rod.

Design goals:

  • return errors from all operations (no hard Fatal inside core);
  • keep action/assert API fluent via Chain();
  • keep observability pluggable through Reporter;
  • keep diagnostics (screenshot/html/url/summary) enabled by default.

Index

Constants

View Source
const (
	MimeText = "text/plain"
	MimeHTML = "text/html"
	MimeJSON = "application/json"
	MimePNG  = "image/png"
)

Variables

View Source
var (
	ErrDriverNil         = errors.New("driver is nil")
	ErrBrowserNil        = errors.New("browser is nil")
	ErrPageNil           = errors.New("page is nil")
	ErrElementNotFound   = errors.New("element not found")
	ErrElementNotVisible = errors.New("element not visible")
	ErrElementUnstable   = errors.New("element not stable")
	ErrElementDisabled   = errors.New("element disabled")
)

Functions

This section is empty.

Types

type Chain

type Chain struct {
	// contains filtered or unexported fields
}

Chain provides fluent scenario building. First error short-circuits all next calls.

func (*Chain) AttachCurrentURL

func (c *Chain) AttachCurrentURL(name string) *Chain

AttachCurrentURL appends current URL attachment step.

func (*Chain) CapturePageHTML

func (c *Chain) CapturePageHTML(name string) *Chain

CapturePageHTML appends page HTML capture step.

func (*Chain) CaptureScreenshot

func (c *Chain) CaptureScreenshot(name string) *Chain

CaptureScreenshot appends screenshot capture step.

func (*Chain) Click

func (c *Chain) Click(el Element, opts ...WaitOption) *Chain

Click appends clicking an element.

func (*Chain) ClickByText

func (c *Chain) ClickByText(text string, opts ...WaitOption) *Chain

ClickByText appends clicking an element found by visible text.

func (*Chain) Do

func (c *Chain) Do(stepName string, fn func(page *rod.Page, browser *rod.Browser) error) *Chain

Do appends custom low-level operation with direct access to page and browser.

func (*Chain) Err

func (c *Chain) Err() error

Err returns first error accumulated in the chain.

func (*Chain) InputText

func (c *Chain) InputText(el Element, text string, sensitiveData bool, opts ...WaitOption) *Chain

InputText appends typing text into an element.

func (*Chain) Must

func (c *Chain) Must()

Must panics if chain has an error.

func (*Chain) OpenNewWindow

func (c *Chain) OpenNewWindow(url string) *Chain

OpenNewWindow appends opening a new window to the fluent chain.

func (*Chain) PressEnter

func (c *Chain) PressEnter(opts ...WaitOption) *Chain

PressEnter appends pressing Enter key.

func (*Chain) Reload

func (c *Chain) Reload(opts ...WaitOption) *Chain

Reload appends page reload to the fluent chain.

func (*Chain) ShouldBeVisible

func (c *Chain) ShouldBeVisible(el Element, opts ...WaitOption) *Chain

ShouldBeVisible appends visibility assertion for an element.

func (*Chain) ShouldCountByCSS

func (c *Chain) ShouldCountByCSS(selector string, expected int, opts ...WaitOption) *Chain

ShouldCountByCSS appends assertion for number of nodes matching CSS selector.

func (*Chain) ShouldElementContain

func (c *Chain) ShouldElementContain(el Element, substr string, opts ...WaitOption) *Chain

ShouldElementContain appends element substring presence assertion.

func (*Chain) ShouldElementNotContain

func (c *Chain) ShouldElementNotContain(el Element, substr string, opts ...WaitOption) *Chain

ShouldElementNotContain appends element substring absence assertion.

func (*Chain) ShouldEvalBool

func (c *Chain) ShouldEvalBool(stepName, js string, expected bool, opts ...WaitOption) *Chain

ShouldEvalBool appends JavaScript boolean assertion.

func (*Chain) ShouldEvalInt

func (c *Chain) ShouldEvalInt(stepName, js string, expected int, opts ...WaitOption) *Chain

ShouldEvalInt appends JavaScript integer assertion.

func (*Chain) ShouldHaveText

func (c *Chain) ShouldHaveText(text string, opts ...WaitOption) *Chain

ShouldHaveText appends page text presence assertion.

func (*Chain) ShouldNotBeVisible

func (c *Chain) ShouldNotBeVisible(el Element, opts ...WaitOption) *Chain

ShouldNotBeVisible appends negative visibility assertion for an element.

func (*Chain) ShouldNotHaveText

func (c *Chain) ShouldNotHaveText(text string, opts ...WaitOption) *Chain

ShouldNotHaveText appends page text absence assertion.

func (*Chain) Sleep

func (c *Chain) Sleep(seconds int) *Chain

Sleep appends explicit sleep step.

func (*Chain) SwitchToWindowByIndex

func (c *Chain) SwitchToWindowByIndex(idx int) *Chain

SwitchToWindowByIndex appends switching active browser tab by index.

func (*Chain) UseIncognito

func (c *Chain) UseIncognito() *Chain

UseIncognito appends switching to an incognito browser context.

func (*Chain) WaitIdle

func (c *Chain) WaitIdle(timeout time.Duration) *Chain

WaitIdle appends waiting for page idle state to the fluent chain.

func (*Chain) WaitLoad

func (c *Chain) WaitLoad(opts ...WaitOption) *Chain

WaitLoad appends waiting for page load to the fluent chain.

type DiagnosticsConfig

type DiagnosticsConfig struct {
	Enabled            bool
	AttachScreenshot   bool
	AttachPageHTML     bool
	AttachCurrentURL   bool
	AttachErrorSummary bool
}

DiagnosticsConfig controls artifacts attached on step failure.

type Driver

type Driver struct {
	Browser *rod.Browser
	Page    *rod.Page
	// contains filtered or unexported fields
}

Driver is a high-level DSL over Rod. Concurrent calls are serialized internally.

func Launch

func Launch(cfg LaunchConfig, opts ...Option) (*Driver, error)

Launch starts browser via Rod launcher and returns ready driver.

func New

func New(browser *rod.Browser, opts ...Option) *Driver

New creates a driver on top of existing browser.

func (*Driver) AttachCurrentURL

func (d *Driver) AttachCurrentURL(name string) error

AttachCurrentURL appends current page URL artifact.

func (*Driver) AttachText

func (d *Driver) AttachText(name, text string)

AttachText appends text artifact to current reporting context.

func (*Driver) CapturePageHTML

func (d *Driver) CapturePageHTML(name string) error

CapturePageHTML appends page HTML artifact.

func (*Driver) CaptureScreenshot

func (d *Driver) CaptureScreenshot(name string) error

CaptureScreenshot captures screenshot artifact from current page.

func (*Driver) Chain

func (d *Driver) Chain() *Chain

Chain creates fluent API wrapper over current driver.

func (*Driver) Click

func (d *Driver) Click(el Element, opts ...WaitOption) error

Click waits for an element and performs a left mouse click.

func (*Driver) ClickByText

func (d *Driver) ClickByText(text string, opts ...WaitOption) error

ClickByText clicks an element found by visible text selector.

func (*Driver) Close

func (d *Driver) Close() error

Close closes current browser instance.

func (*Driver) CloseWithIdle

func (d *Driver) CloseWithIdle(timeout time.Duration) error

CloseWithIdle waits page idle (if any) and then closes browser.

func (*Driver) CountByCSS

func (d *Driver) CountByCSS(selector string, opts ...WaitOption) (int, error)

CountByCSS returns number of elements matching CSS selector.

func (*Driver) CurrentURL

func (d *Driver) CurrentURL() (string, error)

CurrentURL returns URL of the currently active page.

func (*Driver) Do

func (d *Driver) Do(stepName string, fn func(page *rod.Page, browser *rod.Browser) error) error

Do executes custom Rod action while preserving unified step/diagnostics wrapping.

func (*Driver) EvalBool

func (d *Driver) EvalBool(stepName, js string, opts ...WaitOption) (bool, error)

EvalBool evaluates JavaScript and converts result to bool.

func (*Driver) EvalInt

func (d *Driver) EvalInt(stepName, js string, opts ...WaitOption) (int, error)

EvalInt evaluates JavaScript and converts result to int.

func (*Driver) EvalString

func (d *Driver) EvalString(stepName, js string, opts ...WaitOption) (string, error)

EvalString evaluates JavaScript and converts result to string.

func (*Driver) GetValueFromLocalStorage

func (d *Driver) GetValueFromLocalStorage(key string, opts ...WaitOption) (string, error)

GetValueFromLocalStorage returns localStorage value by key.

func (*Driver) InputText

func (d *Driver) InputText(el Element, text string, sensitiveData bool, opts ...WaitOption) error

InputText waits for an element and types text into it.

func (*Driver) Must

func (d *Driver) Must(err error)

Must panics on non-nil error. Useful for fluent tests without if err != nil blocks.

func (*Driver) OpenNewWindow

func (d *Driver) OpenNewWindow(url string) error

OpenNewWindow creates a new browser tab, navigates to URL and switches driver page to it.

func (*Driver) PressEnter

func (d *Driver) PressEnter(opts ...WaitOption) error

PressEnter sends Enter key to the active page.

func (*Driver) Reload

func (d *Driver) Reload(opts ...WaitOption) error

Reload refreshes current page and waits until load completes.

func (*Driver) Reporter

func (d *Driver) Reporter() Reporter

Reporter returns currently configured reporting backend.

func (*Driver) SetPage

func (d *Driver) SetPage(page *rod.Page) *Driver

SetPage sets active page used by driver operations.

func (*Driver) ShouldBeVisible

func (d *Driver) ShouldBeVisible(el Element, opts ...WaitOption) error

ShouldBeVisible asserts that element becomes visible within wait config.

func (*Driver) ShouldCountByCSS

func (d *Driver) ShouldCountByCSS(selector string, expected int, opts ...WaitOption) error

ShouldCountByCSS asserts number of elements matching CSS selector.

func (*Driver) ShouldElementContain

func (d *Driver) ShouldElementContain(el Element, substr string, opts ...WaitOption) error

ShouldElementContain asserts that element text contains provided substring.

func (*Driver) ShouldElementNotContain

func (d *Driver) ShouldElementNotContain(el Element, substr string, opts ...WaitOption) error

ShouldElementNotContain asserts that element text does not contain provided substring.

func (*Driver) ShouldEvalBool

func (d *Driver) ShouldEvalBool(stepName, js string, expected bool, opts ...WaitOption) error

ShouldEvalBool asserts that JavaScript evaluation returns expected bool value.

func (*Driver) ShouldEvalInt

func (d *Driver) ShouldEvalInt(stepName, js string, expected int, opts ...WaitOption) error

ShouldEvalInt asserts that JavaScript evaluation returns expected int value.

func (*Driver) ShouldHaveText

func (d *Driver) ShouldHaveText(text string, opts ...WaitOption) error

ShouldHaveText asserts that page body contains provided text.

func (*Driver) ShouldNotBeVisible

func (d *Driver) ShouldNotBeVisible(el Element, opts ...WaitOption) error

ShouldNotBeVisible asserts that element disappears or becomes not visible.

func (*Driver) ShouldNotHaveText

func (d *Driver) ShouldNotHaveText(text string, opts ...WaitOption) error

ShouldNotHaveText asserts that page body does not contain provided text.

func (*Driver) Sleep

func (d *Driver) Sleep(seconds int) error

Sleep explicit wait. Prefer waiting conditions instead.

func (*Driver) SwitchToWindowByIndex

func (d *Driver) SwitchToWindowByIndex(idx int) error

SwitchToWindowByIndex activates browser tab by index and sets it as current page.

func (*Driver) UseIncognito

func (d *Driver) UseIncognito() error

UseIncognito switches driver to a new incognito browser context. Current page is reset and should be opened again.

func (*Driver) WaitIdle

func (d *Driver) WaitIdle(timeout time.Duration) error

WaitIdle waits until page enters idle state.

func (*Driver) WaitLoad

func (d *Driver) WaitLoad(opts ...WaitOption) error

WaitLoad waits until page load event is completed.

func (*Driver) WithReporter

func (d *Driver) WithReporter(reporter Reporter) *Driver

WithReporter sets reporter implementation and returns the same driver.

type Element

type Element struct {
	Selector Selector
	Name     string
}

Element is a named UI element description.

type LaunchConfig

type LaunchConfig struct {
	Headless      bool
	RunCI         bool
	BrowserBinary string
}

LaunchConfig controls browser launcher options.

func DefaultLaunchConfig

func DefaultLaunchConfig() LaunchConfig

DefaultLaunchConfig returns baseline browser launch settings for CI usage.

type NoopReporter

type NoopReporter struct{}

NoopReporter runs steps without additional reporting.

func (NoopReporter) Attach

func (NoopReporter) Attach(_, _ string, _ []byte)

Attach ignores provided attachment.

func (NoopReporter) Step

func (NoopReporter) Step(_ string, run func() error) error

Step executes step body without additional reporting.

type Option

type Option func(*Driver)

Option configures Driver defaults.

func WithDefaultElementTimeout

func WithDefaultElementTimeout(timeout time.Duration) Option

WithDefaultElementTimeout overrides per-element search timeout.

func WithDefaultInterval

func WithDefaultInterval(interval time.Duration) Option

WithDefaultInterval overrides retry polling interval.

func WithDefaultReloadEachPoll

func WithDefaultReloadEachPoll(enabled bool) Option

WithDefaultReloadEachPoll enables page reload on every retry poll by default.

func WithDefaultTimeout

func WithDefaultTimeout(timeout time.Duration) Option

WithDefaultTimeout overrides default operation timeout.

func WithDefaultWaitStable

func WithDefaultWaitStable(waitStable time.Duration) Option

WithDefaultWaitStable overrides default wait-stable delay after element lookup.

func WithDiagnosticsEnabled

func WithDiagnosticsEnabled(enabled bool) Option

WithDiagnosticsEnabled toggles automatic failure artifact collection.

func WithFailureHTML

func WithFailureHTML(enabled bool) Option

WithFailureHTML toggles page HTML attachment on step failure.

func WithFailureScreenshot

func WithFailureScreenshot(enabled bool) Option

WithFailureScreenshot toggles screenshot attachment on step failure.

func WithFailureSummary

func WithFailureSummary(enabled bool) Option

WithFailureSummary toggles text summary attachment on step failure.

func WithFailureURL

func WithFailureURL(enabled bool) Option

WithFailureURL toggles current URL attachment on step failure.

func WithReporter

func WithReporter(reporter Reporter) Option

WithReporter sets reporter used by the driver.

type Reporter

type Reporter interface {
	Step(name string, run func() error) error
	Attach(name, mime string, content []byte)
}

Reporter is a pluggable step/attachments sink. Implementations may integrate with Allure, logs, or any custom reporter.

type Selector

type Selector struct {
	TestID string
	ID     string
	Name   string
	XPath  string
	CSS    string
	Text   string
}

Selector describes supported strategies for finding an element.

type WaitConfig

type WaitConfig struct {
	Timeout        time.Duration
	Interval       time.Duration
	ElementTimeout time.Duration
	WaitStable     time.Duration
	ReloadEachPoll bool
}

WaitConfig controls retry and wait behavior.

type WaitOption

type WaitOption func(*WaitConfig)

WaitOption overrides wait settings for a single call.

func WithElementTimeout

func WithElementTimeout(timeout time.Duration) WaitOption

WithElementTimeout overrides element lookup timeout for a single operation call.

func WithInterval

func WithInterval(interval time.Duration) WaitOption

WithInterval overrides retry interval for a single operation call.

func WithReloadEachPoll

func WithReloadEachPoll(enabled bool) WaitOption

WithReloadEachPoll enables page reload on each retry poll for a single call.

func WithTimeout

func WithTimeout(timeout time.Duration) WaitOption

WithTimeout overrides timeout for a single operation call.

func WithWaitStable

func WithWaitStable(waitStable time.Duration) WaitOption

WithWaitStable overrides stable wait delay for a single operation call.

Jump to

Keyboard shortcuts

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