Documentation
¶
Overview ¶
Package ffctl implements the clientside of the marionette protocol for programmatically controling the firefox browser.
Index ¶
- type BoundingBox
- type Cookie
- type MarionetteError
- type MouseButton
- type PdfExportConfig
- type PdfExportMargins
- type PdfExportPageDimensions
- type Script
- type Session
- func (s *Session) AcceptAlert() error
- func (s *Session) AddCookie(cookie Cookie) error
- func (s *Session) ClearCookies() error
- func (s *Session) Close()
- func (s *Session) CloseCurrentTab() error
- func (s *Session) DeleteCookie(name string) error
- func (s *Session) DismissAlert() error
- func (s *Session) ExportPdf(config PdfExportConfig) ([]byte, error)
- func (s *Session) FindElement(selector string) (WebElement, error)
- func (s *Session) FindElements(selector string) ([]WebElement, error)
- func (s *Session) FocusTab(handle TabHandle) error
- func (s *Session) Fullscreen() error
- func (s *Session) GetActiveElement() (WebElement, error)
- func (s *Session) GetAlertMessage() (string, error)
- func (s *Session) GetAllTabs() ([]TabHandle, error)
- func (s *Session) GetBoundingBox() (BoundingBox, error)
- func (s *Session) GetCookies() ([]Cookie, error)
- func (s *Session) GetCurrentTab() (TabHandle, error)
- func (s *Session) GetPageSource() (string, error)
- func (s *Session) GetTimeouts() (Timeouts, error)
- func (s *Session) GetTitle() (string, error)
- func (s *Session) GetUrl() (string, error)
- func (s *Session) GoBack() error
- func (s *Session) GoForward() error
- func (s *Session) MakeScript(js string) Script
- func (s *Session) Maximize() error
- func (s *Session) Minimize() error
- func (s *Session) MoveMouse(deltaX float64, deltaY float64, duration time.Duration) error
- func (s *Session) MoveMouseFromElement(element WebElement, deltaX, deltaY float64, duration time.Duration) error
- func (s *Session) MoveMouseTo(x float64, y float64, duration time.Duration) error
- func (s *Session) Navigate(url string) error
- func (s *Session) OpenPrivateWindow() (TabHandle, error)
- func (s *Session) OpenTab() (TabHandle, error)
- func (s *Session) OpenWindow() (TabHandle, error)
- func (s *Session) PressKey(key rune) error
- func (s *Session) PressMouse(mouseButton MouseButton) error
- func (s *Session) ReleaseActions() error
- func (s *Session) ReleaseKey(key rune) error
- func (s *Session) ReleaseMouse(mouseButton MouseButton) error
- func (s *Session) Reload() error
- func (s *Session) ScrollAtPointer(deltaX, deltaY float64, duration time.Duration) error
- func (s *Session) ScrollDocument(deltaX, deltaY float64, duration time.Duration) error
- func (s *Session) SendKeysToAlert(text string) error
- func (s *Session) SetBoundingBox(rect BoundingBox) error
- func (s *Session) SetTimeouts(timeouts Timeouts) error
- func (s *Session) TakeScreenshot() ([]byte, error)
- type SessionConfig
- type ShadowRoot
- type TabHandle
- type Timeouts
- type WebElement
- func (w WebElement) Clear() error
- func (w WebElement) Click() error
- func (w WebElement) FindElement(selector string) (WebElement, error)
- func (w WebElement) FindElements(selector string) ([]WebElement, error)
- func (w WebElement) GetAccessibilityLabel() (string, error)
- func (w WebElement) GetAccessibilityRole() (string, error)
- func (w WebElement) GetAttribute(name string) (string, bool, error)
- func (w WebElement) GetBoundingBox() (BoundingBox, error)
- func (w WebElement) GetCssPropertyValue(name string) (string, error)
- func (w WebElement) GetShadowRoot() (ShadowRoot, error)
- func (w WebElement) GetTabName() (string, error)
- func (w WebElement) GetText() (string, error)
- func (w WebElement) IsDisplayed() (bool, error)
- func (w WebElement) IsEnabled() (bool, error)
- func (w WebElement) IsSelected() (bool, error)
- func (w WebElement) SendKeyPresses(text string) error
- func (w WebElement) TakeScreenshot() ([]byte, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BoundingBox ¶
type Cookie ¶
type Cookie struct {
Name string `json:"name"`
Value string `json:"value"`
Domain string `json:"domain,omitzero"`
Path string `json:"path,omitzero"`
HttpOnly bool `json:"httpOnly,omitzero"`
Secure bool `json:"secure,omitzero"`
SameSite string `json:"sameSite,omitzero"`
// The timestamp the cookie will expire at in seconds
Expiry uint64 `json:"expiry,omitzero"`
}
A browser cookie
type MarionetteError ¶
type MarionetteError struct {
Code string `json:"error"`
Message string `json:"message"`
Stacktrace string `json:"stacktrace"`
}
func (*MarionetteError) Error ¶
func (e *MarionetteError) Error() string
type MouseButton ¶
type MouseButton byte
var ( MouseButtonLeft MouseButton = 0 MouseButtonRight = 1 )
type PdfExportConfig ¶
type PdfExportConfig struct {
Margin PdfExportMargins `json:"margin"`
Dimensions PdfExportPageDimensions `json:"page"`
// Scale of the webpage rendering.
Scale float64 `json:"scale"`
// If `true`, overrides CSS to make the content fit the page size.
ShrinkToFit bool `json:"shrinkToFit"`
Orientation string `json:"orientation"`
// If `true`, the export will include background colors and images.
PrintBackground bool `json:"background"`
// Page ranges to print.
// Each element should either define a page number (e.g. `3`) or a range of pages (e.g. `1-10`)
PageRanges []string `json:"pageRanges"`
}
func DefaultPdfExportConfig ¶
func DefaultPdfExportConfig() PdfExportConfig
Returns default configuration for exporting a page as a pdf
type PdfExportMargins ¶
type PdfExportPageDimensions ¶
type Script ¶
type Script struct {
// Arguments to pass to the script.
//
// In the script, these are accessible using implicit [arguments object].
//
// [arguments object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
Args []any
// contains filtered or unexported fields
}
Javascript code to be executed in the browser.
func (Script) ExecuteAndWaitForCallback ¶
Executes the script in the browser and returns the result.
A callback is automatically added as the last element of the implicit arguments array to the script. This function returns whatever is passed to the callback.
This lets you call asynchronous javascript functions.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
An instance of the firefox browser being controlled.
A session must not be accessed from multiple goroutines.
func NewGraphicalSession ¶
Starts a new graphical browser session.
func NewHeadlessSession ¶
Starts a new headless browser session.
func NewSession ¶
func NewSession(config SessionConfig) (Session, error)
Starts a new browser session with the given configuration.
Call Session.Close to end it.
func (*Session) AcceptAlert ¶
Accepts a currently displayed alert modal.
func (*Session) AddCookie ¶
Adds a single cookie to the cookie store associated with the current document's address.
The only required fields are [Cookie.Name] and [Cookie.Value].
func (*Session) ClearCookies ¶
Deletes all cookies visible to the current document
func (*Session) Close ¶
func (s *Session) Close()
Ends the firefox browser session and cleans up associated resources.
func (*Session) CloseCurrentTab ¶
Close the currently selected tab.
When multiple open tabs are present, the currently selected tab will be closed. Otherwise, the window itself will be closed. If it is the last window open, this function is a no-op to prevent a shutdown of the session.
If a tab or window is closed, the browsing context is switches to one of the remaining open tabs.
func (*Session) DeleteCookie ¶
Deletes the cookie with the given name.
No-op if no such cookie exists.
func (*Session) DismissAlert ¶
Dismisses a currently displayed alert modal.
func (*Session) ExportPdf ¶
func (s *Session) ExportPdf(config PdfExportConfig) ([]byte, error)
Exports the current page as a PDF file.
Returns the raw PDF bytes, ready to be written to a file.
func (*Session) FindElement ¶
func (s *Session) FindElement(selector string) (WebElement, error)
Returns the first element matching the css selector.
func (*Session) FindElements ¶
func (s *Session) FindElements(selector string) ([]WebElement, error)
Returns a list of all elements matching the css selector.
func (*Session) Fullscreen ¶
Makes the current window full-screen, as if the user agent had done "View > Enter Full Screen".
func (*Session) GetActiveElement ¶
func (s *Session) GetActiveElement() (WebElement, error)
Returns the currently active element in the document.
func (*Session) GetAlertMessage ¶
Returns the message shown in a currently alert modal.
func (*Session) GetAllTabs ¶
Returns a list of handles to open browser windows & tabs.
func (*Session) GetBoundingBox ¶
func (s *Session) GetBoundingBox() (BoundingBox, error)
Returns the size and position of the current window.
func (*Session) GetCookies ¶
Returns all the cookies for the current domain.
This is equivalient to calling `document.cookie` and parsing the result.
func (*Session) GetCurrentTab ¶
Get the current tab's handle.
func (*Session) GetPageSource ¶
Returns a string representation of the DOM
func (*Session) GetTimeouts ¶
Returns the timeout values for the current session.
func (*Session) MakeScript ¶
/ Returns a script object that can be executed in the browser.
func (*Session) Maximize ¶
Maximizes the current window, as if the user had pressed the maximize button.
No-op if the window is already maximized.
func (*Session) Minimize ¶
Minimizes the current window, as if the user had pressed the minimize button.
No-op if the window is already minimized;
func (*Session) MoveMouseFromElement ¶
func (s *Session) MoveMouseFromElement(element WebElement, deltaX, deltaY float64, duration time.Duration) error
Simulates moving the pointer from the given element.
func (*Session) MoveMouseTo ¶
Simluates moving the pointer to the given CSS pixel coordinates.
func (*Session) OpenPrivateWindow ¶
Opens a new tab in a private browser window and returns a handle to it.
func (*Session) OpenWindow ¶
Opens a tab in a new browser window and returns a handle to it.
func (*Session) PressMouse ¶
func (s *Session) PressMouse(mouseButton MouseButton) error
/ Simulates pressing a mouse button.
func (*Session) ReleaseActions ¶
Releases all keys and pointers that are currently pressed.
func (*Session) ReleaseMouse ¶
func (s *Session) ReleaseMouse(mouseButton MouseButton) error
Simulates the release of mouse button.
func (*Session) ScrollAtPointer ¶
Simulates scrolling the document at the pointer's current position.
func (*Session) ScrollDocument ¶
Simulates scrolling the entires viewport.
func (*Session) SendKeysToAlert ¶
Sends keys to the input field of a currently displayed alert odal.
func (*Session) SetBoundingBox ¶
func (s *Session) SetBoundingBox(rect BoundingBox) error
Updates the position and size of the current window.
The supplied width and height values refer to the window outerWidth and outerHeight values, which include scroll bars, title bars, etc.
This is a "best effort" operation. The geometry will be adjusted to as close as possible to the requested bounding box. The new position might even be entirely disregarded.
func (*Session) SetTimeouts ¶
Sets the timeouts for the current session.
func (*Session) TakeScreenshot ¶
Takes a screenshot of the contents of current page.
This includes items not currently in view.
The picture is returned as raw PNG bytes, ready to be written to a file.
type SessionConfig ¶
type SessionConfig struct {
// The browser will navigate to this URL on startup
// If it is empty, the browser loads `about:blank`.
Url string
// If `true`, the firefox GUI is displayed.
Graphical bool
// If empty, the firefox binary is searched for in the PATH environment variable.
FirefoxBin string
// If `true`, the firefox process' STDOUT is inherited from the parent process.
// Otherwise, it goes to /dev/null
FirefoxStdout bool
// If `true`, the firefox process' STDOUT is inherited from the parent process
// Otherwise, it goes to /dev/null
FirefoxStderr bool
}
Configuration for starting a firefox session.
It's zero value is ready to use and represents the default configuration.
type ShadowRoot ¶
type ShadowRoot struct {
// contains filtered or unexported fields
}
An opaque sever-assigned identifier that uniquely identifies a shadow root element.
type TabHandle ¶
type TabHandle string
An opaque server-assigned identifier that uniquely identifies a tab in the current session.
type Timeouts ¶
type Timeouts struct {
// Timeout for locating elements on the page
Find uint64 `json:"implicit"`
// Timeout for page load
PageLoad uint64 `json:"pageLoad"`
// Timeout for script execution
Script uint64 `json:"script"`
}
Session-wide timeouts
All timeouts are in milliseconds. A timeout of `0` means "no timeout".
type WebElement ¶
type WebElement struct {
// contains filtered or unexported fields
}
An opaque server-assigned identifier that uniquely identifies an element in the current session.
func (WebElement) FindElement ¶
func (w WebElement) FindElement(selector string) (WebElement, error)
Returns the first descendant element matching the css selector.
func (WebElement) FindElements ¶
func (w WebElement) FindElements(selector string) ([]WebElement, error)
Returns a list of all descendant elements matching css selector.
func (WebElement) GetAccessibilityLabel ¶
func (w WebElement) GetAccessibilityLabel() (string, error)
/ Returns the element's accessibility label.
func (WebElement) GetAccessibilityRole ¶
func (w WebElement) GetAccessibilityRole() (string, error)
/ Returns the element's accessibility role.
func (WebElement) GetAttribute ¶
func (w WebElement) GetAttribute(name string) (string, bool, error)
Returns the value of the given attribute on the element.
If the element does not have the given value, the function returns "", false, nil
If an error occurs, the function returns "", false, <error>
func (WebElement) GetBoundingBox ¶
func (w WebElement) GetBoundingBox() (BoundingBox, error)
Returns the bounding box for the given element.
func (WebElement) GetCssPropertyValue ¶
func (w WebElement) GetCssPropertyValue(name string) (string, error)
Returns the resolved value of the CSS property with the given name.
func (WebElement) GetShadowRoot ¶
func (w WebElement) GetShadowRoot() (ShadowRoot, error)
Returns the element's shadow root.
func (WebElement) GetTabName ¶
func (w WebElement) GetTabName() (string, error)
Returns element's tag name.
func (WebElement) GetText ¶
func (w WebElement) GetText() (string, error)
Returns the text of the element, if any.
Includes text of all descendant elements.
func (WebElement) IsDisplayed ¶
func (w WebElement) IsDisplayed() (bool, error)
Returns `true` if the element is currently displayed.
func (WebElement) IsEnabled ¶
func (w WebElement) IsEnabled() (bool, error)
Returns `true` if the element is currently enabled.
func (WebElement) IsSelected ¶
func (w WebElement) IsSelected() (bool, error)
Returns `true` if the element is currently selected.
func (WebElement) SendKeyPresses ¶
func (w WebElement) SendKeyPresses(text string) error
Sends key presses to the element after focusing it.
func (WebElement) TakeScreenshot ¶
func (w WebElement) TakeScreenshot() ([]byte, error)
Takes a screenshot of the element.
The picture is returned as raw PNG bytes, ready to be written to a file.