browser

package
v0.0.0-...-a4f4c6b Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// SteamAppPage is the app page for an appid.
	SteamAppPage urlfmt.URL = "%s://store.steampowered.com/app/%d"
	// SteamAppDetails is the app details API from the Steam store. Can fetch the details, in JSON, for the given appid.
	SteamAppDetails urlfmt.URL = "%s://store.steampowered.com/api/appdetails/?appids=%d"
	// SteamAppReviews fetches a JSON object of the reviews for the given appid.
	SteamAppReviews urlfmt.URL = "" /* 204-byte string literal not displayed */
	// SteamCommunityPosts fetches a JSON object of the required number of community posts by the partner (publisher).
	SteamCommunityPosts urlfmt.URL = "" /* 181-byte string literal not displayed */
	// SteamGetAppList fetches a JSON object of all the names and IDs of the current apps on Steam.
	SteamGetAppList urlfmt.URL = "%s://api.steampowered.com/ISteamApps/GetAppList/v2/"
	// SteamSpyAppDetails is the URL for the app details API from Steamspy. Can fetch the details, in JSON, for the
	// given appid.
	SteamSpyAppDetails urlfmt.URL = "%s://steamspy.com/api.php?request=appdetails&appid=%d"
	// ItchIOGamePage is the game page for a given developer and game-title combo.
	ItchIOGamePage urlfmt.URL = "%s://%s.itch.io/%s"
	// ItchIOGameDevlogs is a page for an Itch.IO title listing all the developer logs/updates for that game. Like
	// ItchIOGamePage it also takes a developer and game-title slug.
	ItchIOGameDevlogs urlfmt.URL = "%s://%s.itch.io/%s/devlog"
	// ItchIOGameDevlog is a page for an individual devlog for the given developer-game slug pair for a title on
	// Itch.IO.
	ItchIOGameDevlog urlfmt.URL = "%s://%s.itch.io/%s/devlog/%d/%s"
)

Variables

View Source
var UserAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0"

Functions

This section is empty.

Types

type Browser

type Browser struct {
	Playwright *playwright.Playwright
	Browser    playwright.Browser
	Pages      []playwright.Page
	Type       BrowserType
}

Browser wraps the main instances needed for playwright.

func NewBrowser

func NewBrowser(headless bool) (browser *Browser, err error)

NewBrowser creates a new playwright.Playwright, playwright.Browser, and a playwright.Page and wraps these up in a new Browser instance. The browser that is launched will be Firefox be default. To launch a different browser use BrowserType.Browser or NewBrowserOfType.

Example
browser, err := NewBrowser(true)
if err != nil {
	fmt.Println("could not create Browser:", err)
}
if _, err = browser.Pages[0].Goto("https://github.com/playwright-community/playwright-go"); err != nil {
	fmt.Println("could not goto:", err)
}
var entry playwright.ElementHandle
if entry, err = browser.Pages[0].QuerySelector("title"); err != nil {
	fmt.Println("could not get title:", err)
}
fmt.Println(entry.InnerText())
if err = browser.Quit(); err != nil {
	fmt.Println("could not quit browser")
}
fmt.Println("Done!")
Output:

GitHub - playwright-community/playwright-go: Playwright for Go a browser automation library to control Chromium, Firefox and WebKit with a single API. <nil>
Done!

func NewBrowserOfType

func NewBrowserOfType(bt BrowserType, headless bool) (browser *Browser, err error)

func (*Browser) Flow

func (b *Browser) Flow(pageNumber int, commands ...Command) (innerTexts []string, err error)

Flow executes a series of Command, and returns a list of string outputs from the CommandResult.InnerTexts field.

func (*Browser) NewPage

func (b *Browser) NewPage() error

NewPage creates a new page in the Browser.

func (*Browser) Quit

func (b *Browser) Quit() (err error)

Quit the instance of the Browser. This will close the playwright.Browser instance and stop the playwright.Playwright instance.

type BrowserType

type BrowserType int
const (
	Firefox BrowserType = iota
	Chromium
	WebKit
)

func (BrowserType) Browser

func (bt BrowserType) Browser(headless bool) (*Browser, error)

Browser will create a Browser (the same as NewBrowser and NewBrowserOfType) by starting an instance of the given BrowserType.

func (BrowserType) Launch

func (bt BrowserType) Launch(browser *Browser, options ...playwright.BrowserTypeLaunchOptions) (playwright.Browser, error)

Launch will launch an instance of the given BrowserType.

type Command

type Command struct {
	// Type is the type of the command, and will determine what is executed on the playwright.Page.
	Type CommandType
	// Value is the selector that will be used when Type is Select or SelectAll and the text that will be input when
	// Type is Type.
	Value string
	// Options will be asserted to the correct type on execution.
	Options []any
	// Wait indicates whether the command should wait for the query selector to become visible. Thus, it is only
	// applicable when Type is Select or SelectAll.
	Wait bool
	// WaitDuration is how long to sleep when using a Sleep command, or wait when using any command with the Wait flag
	// set. Note: this will override the Timeout field of any playwright.PageWaitForSelectorOptions in Options if it is
	// not nil. Sleep will treat a nil WaitDuration as a time.Duration initialised to 0.
	WaitDuration *time.Duration
	// Optional indicates that this command can fail, and it will be skipped.
	Optional bool
	// FindFunc is the function to execute on each element in the input elements when the Type is Find or FindInnerText.
	// When the Type is Find, then element can be asserted to playwright.ElementHandle, otherwise it will be a string.
	FindFunc func(element any) bool
}

Command represents a command that can be passed to the Browser.Flow method.

func (*Command) Execute

func (c *Command) Execute(page playwright.Page, previousResult *CommandResult) (result *CommandResult, err error)

Execute wraps the Execute method for CommandType.

func (*Command) String

func (c *Command) String() string

String returns the string representation of the Command. This returns a string that is similar to Go's representation of structs, but it also includes the field names. The Selector field will only be included if the Type is Type, Select, or SelectAll and the Wait field will only be included if the Type is Select. FindFunc will never be included.

type CommandResult

type CommandResult struct {
	// Elements are the elements found by the previous Command. This will be overwritten when a Command that finds
	// elements finds new elements.
	Elements []playwright.ElementHandle
	// InnerTexts are the results of the InnerText CommandType. These will be accumulated over the lifespan of the
	// CommandResult.
	InnerTexts []string
}

CommandResult is the instance that is returned by the methods that evaluate a Command.

func CommandResultFrom

func CommandResultFrom(result *CommandResult) *CommandResult

CommandResultFrom creates a new CommandResult from an existing CommandResult copying the old Elements and InnerTexts by reference.

func NewCommandResult

func NewCommandResult() *CommandResult

NewCommandResult creates a new CommandResult.

type CommandType

type CommandType int

CommandType represents the type of Command.

const (
	// Select a single element using a selector stored in the Command.Value field and store it in the
	// CommandResult.Elements field.
	Select CommandType = iota
	// Type a string of text into the first element that is stored within the CommandResult.Elements field.
	Type
	// SelectAll will select all the elements that satisfy the selector in Command.Value and store it in the
	// CommandResult.Elements field.
	SelectAll
	// Find will run the Command.FindFunc against all elements that exist within the CommandResult.Elements field and
	// store each element that satisfies this predicate within the CommandResult.Elements field (overwriting the
	// previous).
	Find
	// FindInnerText will run the Command.FindFunc against the InnerText of all the elements that exist within the
	// CommandResult.Elements field and store each element that satisfies this predicate within the
	// CommandResult.Elements field (overwriting the previous).
	FindInnerText
	// Click the first element in the CommandResult.Elements field.
	Click
	// InnerText will find the innerText of all the elements in the CommandResult.Elements field and store them in the
	// CommandResult.InnerTexts field. Remember that CommandResult.InnerTexts is an accumulator that will not be cleared
	// until the user clears it for themselves.
	InnerText
	// Goto will browse to the URL given in Command.Value.
	Goto
	// Sleep will sleep for the given time.Duration in Command.WaitDuration. If Command.WaitDuration is nil, then Sleep
	// will still be called but with a 0 time.Duration.
	Sleep
)

func (CommandType) Execute

func (ct CommandType) Execute(page playwright.Page, command *Command, previousResult *CommandResult) (result *CommandResult, err error)

Execute will execute the CommandType when given a playwright.Page, a Command, and a CommandResult containing the previous result of a Command. Look at the CommandType enumeration to see the mutations upon each of these inputs and outputs.

func (CommandType) String

func (ct CommandType) String() string

String returns the name of the CommandType.

Jump to

Keyboard shortcuts

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