screenshot

package
v0.0.0-...-9474aff Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: BSD-3-Clause Imports: 31 Imported by: 0

Documentation

Overview

Package screenshot supports taking and examining screenshots; contains helper utils to work with screenshot files.

Index

Constants

This section is empty.

Variables

View Source
var ScreenDiffVars = []string{
	goldServiceAccountKeyVar,
	screendiffDebugVar,
	screendiffDryrunVar,
}

ScreenDiffVars contains a list of all variables used by the screendiff library.

Functions

func Capture

func Capture(ctx context.Context, path string) error

Capture takes a screenshot and saves it as a PNG image to the specified file path. It will use the CLI screenshot command to perform the screen capture.

func CaptureCDP

func CaptureCDP(ctx context.Context, conn *ash.DevtoolsConn, path string) error

CaptureCDP takes a screenshot and saves it as a PNG image at path, similar to CaptureChrome. The diff from CaptureChrome is that this function takes *cdputil.Conn, which is used by chrome.Conn. Thus, CaptureChrome records logs in case of error, while this does not. XXX

func CaptureChrome

func CaptureChrome(ctx context.Context, cr *chrome.Chrome, path string) error

CaptureChrome takes a screenshot of the primary display and saves it as a PNG image to the specified file path. It will use Chrome to perform the screen capture.

func CaptureChromeForDisplay

func CaptureChromeForDisplay(ctx context.Context, cr *chrome.Chrome, displayID, path string) error

CaptureChromeForDisplay takes a screenshot for a given displayID and saves it as a PNG image to the specified file path. It will use Chrome to perform the screen capture.

func CaptureChromeImage

func CaptureChromeImage(ctx context.Context, cr *chrome.Chrome) (image.Image, error)

CaptureChromeImage takes a screenshot of the primary display and returns it as an image.Image. It will use Chrome to perform the screen capture.

func CaptureChromeImageWithTestAPI

func CaptureChromeImageWithTestAPI(ctx context.Context, tconn *chrome.TestConn) (image.Image, error)

CaptureChromeImageWithTestAPI takes a screenshot of the primary display and returns it as an image.Image. It will use Test API to perform the screen capture.

func CaptureWithStderr

func CaptureWithStderr(ctx context.Context, path string) error

CaptureWithStderr differs from Capture in that it returns the stderr when capturing a screenshot fails. This is useful for verification on whether turning display on/off is successful by matching with the message, "CRTC not found. Is the screen on?".

func DiffPerConfig

func DiffPerConfig(ctx context.Context, state screenshotState, configs []Config, fn func(Differ)) error

DiffPerConfig takes a function that performs a set of screenshot diff tests, and a set of configurations to run it on, and runs that screenshot test on each configuration.

func GrabAndCropScreenshot

func GrabAndCropScreenshot(ctx context.Context, cr *chrome.Chrome, bounds coords.Rect) (image.Image, error)

GrabAndCropScreenshot grabs a screenshot and crops it to the specified bounds.

func GrabScreenshot

func GrabScreenshot(ctx context.Context, cr *chrome.Chrome) (image.Image, error)

GrabScreenshot creates a screenshot and returns an image.Image. The path of the image is generated ramdomly in /tmp.

func HasScreenshots

func HasScreenshots() (bool, error)

HasScreenshots returns whether Download folder has screenshots.

func RemoveScreenshots

func RemoveScreenshots() error

RemoveScreenshots removes screenshots from Download folder.

Types

type Config

type Config struct {
	// The set of default options to use for diff tests.
	DefaultOptions Options

	// The region chrome should be started in. Should correspond to an entry in
	// /usr/share/misc/cros-regions.json.
	Region string

	// The display.Info.ID for the display you want to take screenshots on.
	// Required iff you have multiple displays.
	DisplayID string

	// Whether to skip attempting to scale the DPI so that all images are
	// as similar as possible.
	SkipDpiNormalization bool

	// If DryRun is true, instead of running the goldctl imgtest, logs it.
	DryRun bool

	// Whether to output the UI tree for each screenshot.
	OutputUITrees bool

	// A suffix to add to the name. Might be a version number (eg. "V2"), or a
	// human-readable label.
	NameSuffix string
}

Config controls how the screen is rendered during screenshot tests.

func ThoroughConfigs

func ThoroughConfigs() []Config

ThoroughConfigs is a set of configurations that should test most use cases.

func WithBase

func WithBase(base Config, configs []Config) []Config

WithBase returns configs, but with any unfilled fields being filled with the value in base.

func (*Config) Suffix

func (c *Config) Suffix() string

Suffix should return a string representation of the suffix for the test It will contain only non-default options. eg. Region: "de" would be ".de"

type Differ

type Differ interface {
	Chrome() *chrome.Chrome
	Tconn() *chrome.TestConn
	Diff(context.Context, string, *nodewith.Finder, ...Option) uiauto.Action
	DiffWindow(context.Context, string, ...Option) uiauto.Action
	GetFailedDiffs() error
	DieOnFailedDiffs()
}

Differ is a type for running screendiffs.

func NewDiffer

func NewDiffer(ctx context.Context, state screenshotState, cfg Config) (Differ, error)

NewDiffer creates a differ for a new instance of chrome with configuration specified in cfg.

func NewDifferFromChrome

func NewDifferFromChrome(ctx context.Context, state screenshotState, cr *chrome.Chrome, cfg Config) (Differ, error)

NewDifferFromChrome creates a differ for an existing chrome instance.

type Option

type Option = func(*Options)

Option is a modifier to apply to Options.

func PixelDeltaThreshold

func PixelDeltaThreshold(pixelDeltaThreshold int) Option

PixelDeltaThreshold controls the screenshot test option PixelDeltaThreshold.

func RemoveElements

func RemoveElements(removeElements []*nodewith.Finder) Option

RemoveElements controls the screenshot test option RemoveElements.

func Retries

func Retries(retries int) Option

Retries controls the screenshot test option Retries.

func RetryInterval

func RetryInterval(retryInterval time.Duration) Option

RetryInterval controls the screenshot test option RetryInterval.

func SkipWindowMove

func SkipWindowMove(skipWindowMove bool) Option

SkipWindowMove controls the screenshot test option SkipWindowMove.

func SkipWindowResize

func SkipWindowResize(skipWindowResize bool) Option

SkipWindowResize controls the screenshot test option SkipWindowResize.

func Timeout

func Timeout(timeout time.Duration) Option

Timeout controls the screenshot test option Timeout.

func WindowBorderWidthDP

func WindowBorderWidthDP(windowBorderWidthDP int) Option

WindowBorderWidthDP controls the screenshot test option WindowBorderWidthDP.

func WindowHeightDP

func WindowHeightDP(windowHeightDP int) Option

WindowHeightDP controls the screenshot test option WindowHeightDP.

func WindowWidthDP

func WindowWidthDP(windowWidthDP int) Option

WindowWidthDP controls the screenshot test option WindowWidthDP.

type Options

type Options struct {
	// The time to spend looking for a node
	Timeout time.Duration

	// The minimum difference required to treat two pixels as different.
	// Specifically, this is dr + dg + db (the sum of the difference in	each channel).
	PixelDeltaThreshold int

	// The width and height requested of a window, in DP.
	// You probably don't want to set this yourself - the screen diffing
	// framework will tell you what to set this to.
	WindowWidthDP  int
	WindowHeightDP int
	// Whether to skip window resizing and moving respectively.
	// If SkipWindowResize is true, WindowHeightDP and WindowWidthDP won't be required.
	SkipWindowResize bool
	SkipWindowMove   bool

	// Density independent pixels within this distance to a border (top / bottom / sides)
	// of the window will not be considered when determining difference.
	WindowBorderWidthDP int

	// Elements that will be removed from the screenshot. For example, if you have
	// some dynamic content interlaced with static content (eg. file modification
	// times in the files app).
	RemoveElements []*nodewith.Finder

	// The number of times and interval between retries when taking screenshots.
	// We retry for two reasons:
	// 1) Check that any animations have completed (eg. attempting to move the
	//    window can be slow, and the ui tree updates before the window has
	//    finished moving).
	// 2) Try and pick up on any ongoing animations during execution rather
	//    than in gold.
	Retries       int
	RetryInterval time.Duration
}

Options provides all of the ways which you can configure the Diff method.

func (*Options) FillDefaults

func (o *Options) FillDefaults(d Options)

FillDefaults fills any unfilled fields in o with values from d.

Jump to

Keyboard shortcuts

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