Documentation
¶
Overview ¶
Package firefox provides tools to script a marionetted Firefox.
Index ¶
- Constants
- func Attribute[T any](e *WebElement, name string) (T, error)
- func Click(f Finder, by By, sel string) error
- func ElementIsNotPresent(by By, v string) func(f Finder) (bool, *WebElement, error)
- func ElementIsPresent(by By, v string) func(f Finder) (bool, *WebElement, error)
- func GetPref[T any](cli *Client, name string) (T, error)
- func InnerHTML(f Finder, by By, sel string) (string, error)
- func Property[T any](e *WebElement, name string) (T, error)
- func SendKeys(f Finder, by By, sel, keys string) error
- func SetPref[T any](cli *Client, name string, v T) error
- func TextContent(f Finder, by By, sel string) (string, error)
- func WaitNotPresent(f Finder, by By, sel string) error
- func WaitVisible(f Finder, by By, sel string) error
- type By
- type Client
- func (cli *Client) AddonInstall(xpi io.Reader) (string, error)
- func (cli *Client) AddonUninstall(xpi string) error
- func (cli *Client) Browser() *marionette.Client
- func (cli *Client) Close() (err error)
- func (cli *Client) Context() (ctx Context, err error)
- func (cli *Client) ExecuteScript(src Script, opts ...ScriptOption) (*Response, error)
- func (cli *Client) FindElement(by By, v string) (*WebElement, error)
- func (cli *Client) FindElements(by By, v string) ([]*WebElement, error)
- func (cli *Client) Navigate(v string) (*Response, error)
- func (cli *Client) SetContext(ctx Context) (*Response, error)
- type Context
- type Finder
- type Option
- type Response
- type Script
- type ScriptOption
- type WebElement
Examples ¶
Constants ¶
const ( ByID = marionette.ID ByQuery = marionette.CSS_SELECTOR )
Variables ¶
This section is empty.
Functions ¶
func ElementIsNotPresent ¶ added in v0.2.0
func ElementIsPresent ¶ added in v0.2.0
Types ¶
type By ¶
type By = marionette.By
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a Firefox client.
Client provides tools to script a marionetted Firefox instance.
Example (GetSetPrefs) ¶
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"git.sr.ht/~sbinet/firefox"
)
func main() {
dir, err := os.MkdirTemp("", "go-firefox-")
if err != nil {
log.Fatalf("could not create tmp dir: %+v", err)
}
defer os.RemoveAll(dir)
cli, err := firefox.New(firefox.WithFirefoxProfile(dir), firefox.WithMarionettePort(-1))
if err != nil {
log.Fatalf("could not create firefox marionette: %+v", err)
}
defer cli.Close()
{
k := "browser.bookmarks.defaultLocation"
v, err := firefox.GetPref[string](cli, k)
if err != nil {
log.Fatalf("could not get %q: %+v", k, err)
}
fmt.Printf("pref[%s]: %q\n", k, v)
err = firefox.SetPref(cli, k, v+"XXX")
if err != nil {
log.Fatalf("could not modify %q: %+v", k, err)
}
v, err = firefox.GetPref[string](cli, k)
if err != nil {
log.Fatalf("could not get %q: %+v", k, err)
}
fmt.Printf("pref[%s]: %q\n", k, v)
}
{
k := "browser.chrome.site_icons"
v, err := firefox.GetPref[bool](cli, k)
if err != nil {
log.Fatalf("could not get %q: %+v", k, err)
}
fmt.Printf("pref[%s]: %+v\n", k, v)
err = firefox.SetPref(cli, k, !v)
if err != nil {
log.Fatalf("pref: %+v", err)
}
v, err = firefox.GetPref[bool](cli, k)
if err != nil {
log.Fatalf("could not get %q: %+v", k, err)
}
fmt.Printf("pref[%s]: %+v\n", k, v)
}
{
k := "apz.android.chrome_fling_physics.friction"
err = firefox.SetPref(cli, k, "1.1")
if err != nil {
log.Fatalf("pref: %+v", err)
}
v, err := firefox.GetPref[json.Number](cli, k)
if err != nil {
log.Fatalf("could not get %q: %+v", k, err)
}
fmt.Printf("pref[%s]: %+v\n", k, v)
}
{
k := "browser.contentblocking.cfr-milestone.milestones"
v, err := firefox.GetPref[string](cli, k)
if err != nil {
log.Fatalf("could not get %q: %+v", k, err)
}
fmt.Printf("pref[%s]: %q\n", k, v)
err = firefox.SetPref(cli, k, v+"XXX")
if err != nil {
log.Fatalf("could not modify %q: %+v", k, err)
}
v, err = firefox.GetPref[string](cli, k)
if err != nil {
log.Fatalf("could not get %q: %+v", k, err)
}
fmt.Printf("pref[%s]: %q\n", k, v)
}
}
Output: pref[browser.bookmarks.defaultLocation]: "toolbar" pref[browser.bookmarks.defaultLocation]: "toolbarXXX" pref[browser.chrome.site_icons]: true pref[browser.chrome.site_icons]: false pref[apz.android.chrome_fling_physics.friction]: 1.1 pref[browser.contentblocking.cfr-milestone.milestones]: "[1000, 5000, 10000, 25000, 50000, 100000, 250000, 314159, 500000, 750000, 1000000, 1250000, 1500000, 1750000, 2000000, 2250000, 2500000, 8675309]" pref[browser.contentblocking.cfr-milestone.milestones]: "[1000, 5000, 10000, 25000, 50000, 100000, 250000, 314159, 500000, 750000, 1000000, 1250000, 1500000, 1750000, 2000000, 2250000, 2500000, 8675309]XXX"
func (*Client) AddonInstall ¶ added in v0.2.0
AddonInstall installs the provided xpi addon and returns its ID.
func (*Client) AddonUninstall ¶ added in v0.2.0
AddonUninstall uninstalls the xpi addon, referenced by name.
func (*Client) Browser ¶
func (cli *Client) Browser() *marionette.Client
Browser returns the marionette client connected to the underlying Firefox browser.
func (*Client) ExecuteScript ¶
func (cli *Client) ExecuteScript(src Script, opts ...ScriptOption) (*Response, error)
func (*Client) FindElement ¶
func (cli *Client) FindElement(by By, v string) (*WebElement, error)
FindElement finds an element using the indicated search strategy.
func (*Client) FindElements ¶
func (cli *Client) FindElements(by By, v string) ([]*WebElement, error)
FindElements finds elements using the indicated search strategy.
type Context ¶ added in v0.2.0
type Context = marionette.Context
var ( Chrome Context = marionette.CHROME Content Context = marionette.CONTENT )
type Finder ¶
type Finder = marionette.Finder
type Option ¶
Option configures how a Firefox client should be customized.
func WithCapabilities ¶ added in v0.2.0
func WithCapabilities(caps marionette.Capabilities) Option
WithCapabilities modifies the default capabilities the marionette client will use.
func WithFirefoxProfile ¶
WithFirefoxProfile provides the path to the Firefox profile to use.
func WithHeadless ¶
WithHeadless instructs a Firefox client whether it should start with a GUI.
func WithMarionettePort ¶ added in v0.2.0
WithMarionettePort modifies the port to use for marionette. If the provided port is 0, the default marionette port is used. If the provided port is negative, a random one is used.
func WithTor ¶ added in v0.2.0
WithTor modifies the Firefox client to use Tor (through SOCK5 proxy).
func WithUserAgent ¶ added in v0.2.0
WithUserAgent modifies the default user agent.
type Response ¶
type Response = marionette.Response
type ScriptOption ¶
func WithScriptNewSandbox ¶
func WithScriptNewSandbox(v bool) ScriptOption
func WithScriptTimeout ¶
func WithScriptTimeout(d time.Duration) ScriptOption
type WebElement ¶
type WebElement = marionette.WebElement