Skip to content

Commit 4b99890

Browse files
feat: add only-shell option to run options for install chromium headless (#518)
1 parent 81b2e8b commit 4b99890

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

run.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ func (d *PlaywrightDriver) installBrowsers() error {
205205
if d.options.Browsers != nil {
206206
additionalArgs = append(additionalArgs, d.options.Browsers...)
207207
}
208+
209+
if d.options.OnlyInstallShell {
210+
additionalArgs = append(additionalArgs, "--only-shell")
211+
}
212+
213+
if d.options.DryRun {
214+
additionalArgs = append(additionalArgs, "--dry-run")
215+
}
216+
208217
cmd := d.Command(additionalArgs...)
209218
cmd.Stdout = d.options.Stdout
210219
cmd.Stderr = d.options.Stderr
@@ -228,14 +237,18 @@ type RunOptions struct {
228237
// - Windows: %USERPROFILE%\AppData\Local
229238
// - macOS: ~/Library/Caches
230239
// - Linux: ~/.cache
231-
DriverDirectory string
240+
DriverDirectory string
241+
// OnlyInstallShell only downloads the headless shell. (For chromium browsers only)
242+
OnlyInstallShell bool
232243
SkipInstallBrowsers bool
233244
// if not set and SkipInstallBrowsers is false, will download all browsers (chromium, firefox, webkit)
234245
Browsers []string
235246
Verbose bool // default true
236247
Stdout io.Writer
237248
Stderr io.Writer
238249
Logger *slog.Logger
250+
// DryRun does not install browser/dependencies. It will only print information.
251+
DryRun bool
239252
}
240253

241254
// Install does download the driver and the browsers.

run_test.go

+52-13
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,7 @@ func TestRunOptionsRedirectStderr(t *testing.T) {
2020
r, w := io.Pipe()
2121
var output string
2222
wg := &sync.WaitGroup{}
23-
wg.Add(1)
24-
go func() {
25-
defer wg.Done()
26-
buf := bufio.NewReader(r)
27-
for {
28-
line, _, err := buf.ReadLine()
29-
if err == io.EOF {
30-
break
31-
}
32-
output += string(line)
33-
}
34-
_ = r.Close()
35-
}()
23+
readIOAsyncTilEOF(t, r, wg, &output)
3624

3725
driverPath := t.TempDir()
3826
options := &RunOptions{
@@ -59,6 +47,40 @@ func TestRunOptionsRedirectStderr(t *testing.T) {
5947
require.Contains(t, output, fmt.Sprintf("path=%s", driverPath))
6048
}
6149

50+
func TestRunOptions_OnlyInstallShell(t *testing.T) {
51+
if getBrowserName() != "chromium" {
52+
t.Skip("chromium only")
53+
return
54+
}
55+
56+
r, w := io.Pipe()
57+
var output string
58+
wg := &sync.WaitGroup{}
59+
readIOAsyncTilEOF(t, r, wg, &output)
60+
61+
driverPath := t.TempDir()
62+
driver, err := NewDriver(&RunOptions{
63+
Stdout: w,
64+
DriverDirectory: driverPath,
65+
Browsers: []string{getBrowserName()},
66+
Verbose: true,
67+
OnlyInstallShell: true,
68+
DryRun: true,
69+
})
70+
require.NoError(t, err)
71+
browserPath := t.TempDir()
72+
73+
t.Setenv("PLAYWRIGHT_BROWSERS_PATH", browserPath)
74+
75+
err = driver.Install()
76+
require.NoError(t, err)
77+
require.NoError(t, w.Close())
78+
wg.Wait()
79+
80+
assert.Contains(t, output, "browser: chromium-headless-shell version")
81+
assert.NotContains(t, output, "browser: chromium version")
82+
}
83+
6284
func TestDriverInstall(t *testing.T) {
6385
driverPath := t.TempDir()
6486
driver, err := NewDriver(&RunOptions{
@@ -187,3 +209,20 @@ func getBrowserName() string {
187209
}
188210
return "chromium"
189211
}
212+
213+
func readIOAsyncTilEOF(t *testing.T, r *io.PipeReader, wg *sync.WaitGroup, output *string) {
214+
t.Helper()
215+
wg.Add(1)
216+
go func() {
217+
defer wg.Done()
218+
buf := bufio.NewReader(r)
219+
for {
220+
line, _, err := buf.ReadLine()
221+
if err == io.EOF {
222+
break
223+
}
224+
*output += string(line)
225+
}
226+
_ = r.Close()
227+
}()
228+
}

0 commit comments

Comments
 (0)