@@ -30,23 +30,22 @@ var (
30
30
}
31
31
)
32
32
33
+ // PlaywrightDriver wraps the Playwright CLI of upstream Playwright.
34
+ //
35
+ // It's required for playwright-go to work.
33
36
type PlaywrightDriver struct {
34
37
driverDirectory , Version string
35
38
options * RunOptions
36
39
}
37
40
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
46
45
}
47
46
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 ),
50
49
Version : playwrightCliVersion ,
51
50
}, nil
52
51
}
@@ -222,19 +221,26 @@ func (d *PlaywrightDriver) uninstallBrowsers() error {
222
221
223
222
// RunOptions are custom options to run the driver
224
223
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
225
230
DriverDirectory string
226
231
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
231
237
}
232
238
233
239
// Install does download the driver and the browsers.
234
240
//
235
241
// Use this before playwright.Run() or use playwright cli to install the driver and browsers
236
242
func Install (options ... * RunOptions ) error {
237
- driver , err := NewDriver (transformRunOptions ( options ) )
243
+ driver , err := NewDriver (options ... )
238
244
if err != nil {
239
245
return fmt .Errorf ("could not get driver instance: %w" , err )
240
246
}
@@ -249,7 +255,7 @@ func Install(options ...*RunOptions) error {
249
255
// Requires the driver and the browsers to be installed before.
250
256
// Either use Install() or use playwright cli.
251
257
func Run (options ... * RunOptions ) (* Playwright , error ) {
252
- driver , err := NewDriver (transformRunOptions ( options ) )
258
+ driver , err := NewDriver (options ... )
253
259
if err != nil {
254
260
return nil , fmt .Errorf ("could not get driver instance: %w" , err )
255
261
}
@@ -265,13 +271,23 @@ func Run(options ...*RunOptions) (*Playwright, error) {
265
271
return playwright , err
266
272
}
267
273
268
- func transformRunOptions (options [] * RunOptions ) * RunOptions {
274
+ func transformRunOptions (options ... * RunOptions ) ( * RunOptions , error ) {
269
275
option := & RunOptions {
270
276
Verbose : true ,
271
277
}
272
278
if len (options ) == 1 {
273
279
option = options [0 ]
274
280
}
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
+ }
275
291
if option .Stdout == nil {
276
292
option .Stdout = os .Stdout
277
293
}
@@ -280,7 +296,7 @@ func transformRunOptions(options []*RunOptions) *RunOptions {
280
296
} else {
281
297
logger .SetOutput (option .Stderr )
282
298
}
283
- return option
299
+ return option , nil
284
300
}
285
301
286
302
func getNodeExecutable (driverDirectory string ) string {
0 commit comments