Skip to content

Commit 8ce48a3

Browse files
feat: use PLAYWRIGHT_DRIVER_PATH environment variable as driver cache directory (#485)
Co-authored-by: Can Stand <70889873+canstand@users.noreply.github.com>
1 parent 7530f86 commit 8ce48a3

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

run.go

+34-18
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,22 @@ var (
3030
}
3131
)
3232

33+
// PlaywrightDriver wraps the Playwright CLI of upstream Playwright.
34+
//
35+
// It's required for playwright-go to work.
3336
type PlaywrightDriver struct {
3437
driverDirectory, Version string
3538
options *RunOptions
3639
}
3740

38-
func NewDriver(options *RunOptions) (*PlaywrightDriver, error) {
39-
baseDriverDirectory := options.DriverDirectory
40-
if baseDriverDirectory == "" {
41-
var err error
42-
baseDriverDirectory, err = getDefaultCacheDirectory()
43-
if err != nil {
44-
return nil, fmt.Errorf("could not get default cache directory: %w", err)
45-
}
41+
func NewDriver(options ...*RunOptions) (*PlaywrightDriver, error) {
42+
transformed, err := transformRunOptions(options...) // get default values
43+
if err != nil {
44+
return nil, err
4645
}
4746
return &PlaywrightDriver{
48-
options: options,
49-
driverDirectory: filepath.Join(baseDriverDirectory, "ms-playwright-go", playwrightCliVersion),
47+
options: transformed,
48+
driverDirectory: filepath.Join(transformed.DriverDirectory, "ms-playwright-go", playwrightCliVersion),
5049
Version: playwrightCliVersion,
5150
}, nil
5251
}
@@ -222,19 +221,26 @@ func (d *PlaywrightDriver) uninstallBrowsers() error {
222221

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

233239
// Install does download the driver and the browsers.
234240
//
235241
// Use this before playwright.Run() or use playwright cli to install the driver and browsers
236242
func Install(options ...*RunOptions) error {
237-
driver, err := NewDriver(transformRunOptions(options))
243+
driver, err := NewDriver(options...)
238244
if err != nil {
239245
return fmt.Errorf("could not get driver instance: %w", err)
240246
}
@@ -249,7 +255,7 @@ func Install(options ...*RunOptions) error {
249255
// Requires the driver and the browsers to be installed before.
250256
// Either use Install() or use playwright cli.
251257
func Run(options ...*RunOptions) (*Playwright, error) {
252-
driver, err := NewDriver(transformRunOptions(options))
258+
driver, err := NewDriver(options...)
253259
if err != nil {
254260
return nil, fmt.Errorf("could not get driver instance: %w", err)
255261
}
@@ -265,13 +271,23 @@ func Run(options ...*RunOptions) (*Playwright, error) {
265271
return playwright, err
266272
}
267273

268-
func transformRunOptions(options []*RunOptions) *RunOptions {
274+
func transformRunOptions(options ...*RunOptions) (*RunOptions, error) {
269275
option := &RunOptions{
270276
Verbose: true,
271277
}
272278
if len(options) == 1 {
273279
option = options[0]
274280
}
281+
if option.DriverDirectory == "" { // if user did not set it, try to get it from env
282+
option.DriverDirectory = os.Getenv("PLAYWRIGHT_DRIVER_PATH")
283+
}
284+
if option.DriverDirectory == "" {
285+
var err error
286+
option.DriverDirectory, err = getDefaultCacheDirectory()
287+
if err != nil {
288+
return nil, fmt.Errorf("could not get default cache directory: %w", err)
289+
}
290+
}
275291
if option.Stdout == nil {
276292
option.Stdout = os.Stdout
277293
}
@@ -280,7 +296,7 @@ func transformRunOptions(options []*RunOptions) *RunOptions {
280296
} else {
281297
logger.SetOutput(option.Stderr)
282298
}
283-
return option
299+
return option, nil
284300
}
285301

286302
func getNodeExecutable(driverDirectory string) string {

0 commit comments

Comments
 (0)