Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PLAYWRIGHT_DRIVER_PATH environment variable as driver cache directory #485

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,22 @@ var (
}
)

// PlaywrightDriver wraps the Playwright CLI of upstream Playwright.
//
// It's required for playwright-go to work.
type PlaywrightDriver struct {
driverDirectory, Version string
options *RunOptions
}

func NewDriver(options *RunOptions) (*PlaywrightDriver, error) {
baseDriverDirectory := options.DriverDirectory
if baseDriverDirectory == "" {
var err error
baseDriverDirectory, err = getDefaultCacheDirectory()
if err != nil {
return nil, fmt.Errorf("could not get default cache directory: %w", err)
}
func NewDriver(options ...*RunOptions) (*PlaywrightDriver, error) {
transformed, err := transformRunOptions(options...) // get default values
if err != nil {
return nil, err
}
return &PlaywrightDriver{
options: options,
driverDirectory: filepath.Join(baseDriverDirectory, "ms-playwright-go", playwrightCliVersion),
options: transformed,
driverDirectory: filepath.Join(transformed.DriverDirectory, "ms-playwright-go", playwrightCliVersion),
Version: playwrightCliVersion,
}, nil
}
Expand Down Expand Up @@ -222,19 +221,26 @@ func (d *PlaywrightDriver) uninstallBrowsers() error {

// RunOptions are custom options to run the driver
type RunOptions struct {
// DriverDirectory is the directory where the playwright cli will be downloaded.
// Default depends on the platform:
// - Windows: %USERPROFILE%\AppData\Local
// - macOS: ~/Library/Caches
// - Linux: ~/.cache
// You can specify here or set the environment variable PLAYWRIGHT_DRIVER_PATH
DriverDirectory string
SkipInstallBrowsers bool
Browsers []string
Verbose bool // default true
Stdout io.Writer
Stderr io.Writer
// if not set and SkipInstallBrowsers is false, will download all browsers (chromium, firefox, webkit)
Browsers []string
Verbose bool // default true
Stdout io.Writer
Stderr io.Writer
}

// Install does download the driver and the browsers.
//
// Use this before playwright.Run() or use playwright cli to install the driver and browsers
func Install(options ...*RunOptions) error {
driver, err := NewDriver(transformRunOptions(options))
driver, err := NewDriver(options...)
if err != nil {
return fmt.Errorf("could not get driver instance: %w", err)
}
Expand All @@ -249,7 +255,7 @@ func Install(options ...*RunOptions) error {
// Requires the driver and the browsers to be installed before.
// Either use Install() or use playwright cli.
func Run(options ...*RunOptions) (*Playwright, error) {
driver, err := NewDriver(transformRunOptions(options))
driver, err := NewDriver(options...)
if err != nil {
return nil, fmt.Errorf("could not get driver instance: %w", err)
}
Expand All @@ -265,13 +271,23 @@ func Run(options ...*RunOptions) (*Playwright, error) {
return playwright, err
}

func transformRunOptions(options []*RunOptions) *RunOptions {
func transformRunOptions(options ...*RunOptions) (*RunOptions, error) {
option := &RunOptions{
Verbose: true,
}
if len(options) == 1 {
option = options[0]
}
if option.DriverDirectory == "" { // if user did not set it, try to get it from env
option.DriverDirectory = os.Getenv("PLAYWRIGHT_DRIVER_PATH")
}
if option.DriverDirectory == "" {
var err error
option.DriverDirectory, err = getDefaultCacheDirectory()
if err != nil {
return nil, fmt.Errorf("could not get default cache directory: %w", err)
}
}
if option.Stdout == nil {
option.Stdout = os.Stdout
}
Expand All @@ -280,7 +296,7 @@ func transformRunOptions(options []*RunOptions) *RunOptions {
} else {
logger.SetOutput(option.Stderr)
}
return option
return option, nil
}

func getNodeExecutable(driverDirectory string) string {
Expand Down
Loading